2026-10-05 18:10:18 -04:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useStore } from '@tanstack/react-form';
|
|
|
|
|
import { useAppForm, Form } from '@/components/form';
|
|
|
|
|
import type { ApiKeyResource } from '@/api/admin/api-keys/queries';
|
|
|
|
|
import { API_KEY_RESOURCES, createAdminApiKeyInput, useCreateAdminApiKey } from '@/api/admin/api-keys/queries';
|
|
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
|
import { NewButton } from '@/components/elements/NewButton';
|
|
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2026-11-23 14:16:00 +00:00
|
|
|
import { useTranslation, type Translation } from '@/sdk/localization';
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
// AdminAcl bitmask values (Read = 1, Write = 2).
|
|
|
|
|
const PERMISSION_NONE = 0;
|
|
|
|
|
const PERMISSION_READ = 1;
|
|
|
|
|
const PERMISSION_READ_WRITE = 3;
|
|
|
|
|
|
2026-11-23 14:16:00 +00:00
|
|
|
const resourceLabels = (t: Translation['t']) =>
|
|
|
|
|
({
|
|
|
|
|
servers: t('Servers'),
|
|
|
|
|
nodes: t('Nodes'),
|
|
|
|
|
allocations: t('Allocations'),
|
|
|
|
|
users: t('Users'),
|
|
|
|
|
locations: t('Locations'),
|
|
|
|
|
eggs: t('Eggs'),
|
|
|
|
|
database_hosts: t('Database Hosts'),
|
|
|
|
|
server_databases: t('Server Databases'),
|
|
|
|
|
}) satisfies Record<ApiKeyResource, string>;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
const initialPermissions = {
|
|
|
|
|
servers: PERMISSION_NONE,
|
|
|
|
|
nodes: PERMISSION_NONE,
|
|
|
|
|
allocations: PERMISSION_NONE,
|
|
|
|
|
users: PERMISSION_NONE,
|
|
|
|
|
locations: PERMISSION_NONE,
|
|
|
|
|
eggs: PERMISSION_NONE,
|
|
|
|
|
database_hosts: PERMISSION_NONE,
|
|
|
|
|
server_databases: PERMISSION_NONE,
|
|
|
|
|
} satisfies Record<ApiKeyResource, number>;
|
|
|
|
|
|
2026-11-23 14:16:00 +00:00
|
|
|
const validateMemo = (t: Translation['t'], value: string): string | undefined => {
|
2026-10-08 16:53:20 -04:00
|
|
|
if (value.length < 1) {
|
2026-11-23 14:16:00 +00:00
|
|
|
return t('A description must be provided.');
|
2026-10-08 16:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value.length > 500) {
|
2026-11-23 14:16:00 +00:00
|
|
|
return t('The description must not exceed 500 characters.');
|
2026-10-08 16:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
type CreateApiKeyDialogProps = {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
onCreatedSecret: (secret: string) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function CreateApiKeyDialog({ open, onClose, onCreatedSecret }: CreateApiKeyDialogProps) {
|
2026-11-23 14:16:00 +00:00
|
|
|
const { t } = useTranslation();
|
2026-10-05 18:10:18 -04:00
|
|
|
const createApiKey = useCreateAdminApiKey();
|
2026-11-23 14:16:00 +00:00
|
|
|
const labels = resourceLabels(t);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
const form = useAppForm({
|
|
|
|
|
defaultValues: { memo: '', permissions: initialPermissions },
|
|
|
|
|
onSubmit: async ({ value }) => {
|
|
|
|
|
try {
|
|
|
|
|
const apiKey = await createApiKey.mutateAsync(createAdminApiKeyInput(value));
|
2026-10-08 16:53:20 -04:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
onClose();
|
|
|
|
|
onCreatedSecret(apiKey.meta?.secret_token ?? '');
|
|
|
|
|
} catch {
|
|
|
|
|
// Error toast is handled by the mutation.
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
2026-11-23 14:16:00 +00:00
|
|
|
title={t('Create application API key')}
|
2026-10-05 18:10:18 -04:00
|
|
|
preventExternalClose={isSubmitting}
|
|
|
|
|
hideCloseIcon={isSubmitting}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
>
|
|
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
2026-10-08 16:53:20 -04:00
|
|
|
<Form form={form} className='m-0'>
|
2026-10-05 18:10:18 -04:00
|
|
|
<form.AppField
|
2026-10-08 16:53:20 -04:00
|
|
|
name='memo'
|
2026-10-05 18:10:18 -04:00
|
|
|
validators={{
|
2026-11-23 14:16:00 +00:00
|
|
|
onChange: ({ value }) => validateMemo(t, value),
|
2026-10-05 18:10:18 -04:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.TextField
|
2026-10-08 16:53:20 -04:00
|
|
|
type='text'
|
|
|
|
|
id='memo'
|
2026-11-23 14:16:00 +00:00
|
|
|
label={t('Description')}
|
|
|
|
|
description={t(
|
|
|
|
|
'A short note describing what this key is used for. You cannot edit a key after creating it.'
|
|
|
|
|
)}
|
2026-10-05 18:10:18 -04:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</form.AppField>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='mt-6'>
|
2026-11-23 14:16:00 +00:00
|
|
|
<Label>{t('Permissions')}</Label>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='mt-2 space-y-3'>
|
2026-10-05 18:10:18 -04:00
|
|
|
{API_KEY_RESOURCES.map((resource) => (
|
2026-10-08 16:53:20 -04:00
|
|
|
<div key={resource} className='flex items-center'>
|
2026-11-23 14:16:00 +00:00
|
|
|
<p className='flex-1 text-sm text-foreground'>{labels[resource]}</p>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='w-48'>
|
2026-10-05 18:10:18 -04:00
|
|
|
<form.AppField name={`permissions.${resource}`}>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.SelectField
|
|
|
|
|
id={`r_${resource}`}
|
|
|
|
|
options={[
|
2026-11-23 14:16:00 +00:00
|
|
|
{ value: PERMISSION_NONE, label: t('None') },
|
|
|
|
|
{ value: PERMISSION_READ, label: t('Read') },
|
|
|
|
|
{ value: PERMISSION_READ_WRITE, label: t('Read & Write') },
|
2026-10-05 18:10:18 -04:00
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</form.AppField>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='flex flex-wrap justify-end mt-6'>
|
|
|
|
|
<Button type='button' isSecondary className='w-full sm:w-auto sm:mr-2' onClick={onClose}>
|
2026-11-23 14:16:00 +00:00
|
|
|
{t('Cancel')}
|
2026-10-05 18:10:18 -04:00
|
|
|
</Button>
|
|
|
|
|
<form.AppForm>
|
2026-11-23 14:16:00 +00:00
|
|
|
<form.SubmitButton className='w-full mt-4 sm:w-auto sm:mt-0'>
|
|
|
|
|
{t('Create Key')}
|
|
|
|
|
</form.SubmitButton>
|
2026-10-05 18:10:18 -04:00
|
|
|
</form.AppForm>
|
|
|
|
|
</div>
|
|
|
|
|
</Form>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function CreateApiKeyButton() {
|
2026-11-23 14:16:00 +00:00
|
|
|
const { t } = useTranslation();
|
2026-10-05 18:10:18 -04:00
|
|
|
const [secret, setSecret] = useState('');
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-11-23 14:16:00 +00:00
|
|
|
<Dialog open={secret.length > 0} title={t('Your new API key')} onClose={() => setSecret('')}>
|
2026-10-08 16:53:20 -04:00
|
|
|
<p className='text-sm mb-6'>
|
2026-11-23 14:16:00 +00:00
|
|
|
{t(
|
|
|
|
|
'The application API key shown below will not be displayed again. Store it somewhere safe before closing this dialog.'
|
|
|
|
|
)}
|
2026-10-05 18:10:18 -04:00
|
|
|
</p>
|
2026-10-08 16:53:20 -04:00
|
|
|
<pre className='text-sm bg-muted rounded-sm py-2 px-4 font-mono'>
|
2026-10-05 18:10:18 -04:00
|
|
|
<CopyOnClick text={secret}>
|
2026-10-08 16:53:20 -04:00
|
|
|
<code className='font-mono'>{secret}</code>
|
2026-10-05 18:10:18 -04:00
|
|
|
</CopyOnClick>
|
|
|
|
|
</pre>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='flex justify-end mt-6'>
|
|
|
|
|
<Button type='button' onClick={() => setSecret('')}>
|
2026-11-23 14:16:00 +00:00
|
|
|
{t('Close')}
|
2026-10-05 18:10:18 -04:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</Dialog>
|
2026-11-23 14:16:00 +00:00
|
|
|
<Dialog.Trigger trigger={({ onClick }) => <NewButton onClick={onClick}>{t('New API key')}</NewButton>}>
|
2026-10-05 18:10:18 -04:00
|
|
|
{({ open, onClose }) => (
|
|
|
|
|
<CreateApiKeyDialog open={open} onClose={onClose} onCreatedSecret={setSecret} />
|
|
|
|
|
)}
|
|
|
|
|
</Dialog.Trigger>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|