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';
|
|
|
|
|
|
|
|
|
|
// AdminAcl bitmask values (Read = 1, Write = 2).
|
|
|
|
|
const PERMISSION_NONE = 0;
|
|
|
|
|
const PERMISSION_READ = 1;
|
|
|
|
|
const PERMISSION_READ_WRITE = 3;
|
|
|
|
|
|
|
|
|
|
const labelFor = (resource: ApiKeyResource): string =>
|
|
|
|
|
resource
|
|
|
|
|
.split('_')
|
|
|
|
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
|
|
|
.join(' ');
|
|
|
|
|
|
|
|
|
|
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-10-08 16:53:20 -04:00
|
|
|
const validateMemo = (value: string): string | undefined => {
|
|
|
|
|
if (value.length < 1) {
|
|
|
|
|
return 'A description must be provided.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value.length > 500) {
|
|
|
|
|
return 'The description must not exceed 500 characters.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
const createApiKey = useCreateAdminApiKey();
|
|
|
|
|
|
|
|
|
|
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-10-08 16:53:20 -04:00
|
|
|
title='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-10-08 16:53:20 -04:00
|
|
|
onChange: ({ value }) => validateMemo(value),
|
2026-10-05 18:10:18 -04:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.TextField
|
2026-10-08 16:53:20 -04:00
|
|
|
type='text'
|
|
|
|
|
id='memo'
|
|
|
|
|
label='Description'
|
|
|
|
|
description='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-10-05 18:10:18 -04:00
|
|
|
<Label>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'>
|
|
|
|
|
<p className='flex-1 text-sm text-foreground'>{labelFor(resource)}</p>
|
|
|
|
|
<div className='w-48'>
|
2026-10-05 18:10:18 -04:00
|
|
|
<form.AppField name={`permissions.${resource}`}>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.SelectField
|
|
|
|
|
id={`r_${resource}`}
|
|
|
|
|
options={[
|
|
|
|
|
{ value: PERMISSION_NONE, label: 'None' },
|
|
|
|
|
{ value: PERMISSION_READ, label: 'Read' },
|
|
|
|
|
{ value: PERMISSION_READ_WRITE, label: 'Read & Write' },
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</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-10-05 18:10:18 -04:00
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<form.AppForm>
|
2026-10-08 16:53:20 -04:00
|
|
|
<form.SubmitButton className='w-full mt-4 sm:w-auto sm:mt-0'>Create Key</form.SubmitButton>
|
2026-10-05 18:10:18 -04:00
|
|
|
</form.AppForm>
|
|
|
|
|
</div>
|
|
|
|
|
</Form>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function CreateApiKeyButton() {
|
|
|
|
|
const [secret, setSecret] = useState('');
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-10-08 16:53:20 -04:00
|
|
|
<Dialog open={secret.length > 0} title='Your new API key' onClose={() => setSecret('')}>
|
|
|
|
|
<p className='text-sm mb-6'>
|
2026-10-05 18:10:18 -04:00
|
|
|
The application API key shown below will not be displayed again. Store it somewhere safe before
|
|
|
|
|
closing this dialog.
|
|
|
|
|
</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-10-05 18:10:18 -04:00
|
|
|
Close
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</Dialog>
|
|
|
|
|
<Dialog.Trigger trigger={({ onClick }) => <NewButton onClick={onClick}>New API key</NewButton>}>
|
|
|
|
|
{({ open, onClose }) => (
|
|
|
|
|
<CreateApiKeyDialog open={open} onClose={onClose} onCreatedSecret={setSecret} />
|
|
|
|
|
)}
|
|
|
|
|
</Dialog.Trigger>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|