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'; import { useTranslation, type Translation } from '@/sdk/localization'; // AdminAcl bitmask values (Read = 1, Write = 2). const PERMISSION_NONE = 0; const PERMISSION_READ = 1; const PERMISSION_READ_WRITE = 3; 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; 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; const validateMemo = (t: Translation['t'], value: string): string | undefined => { if (value.length < 1) { return t('A description must be provided.'); } if (value.length > 500) { return t('The description must not exceed 500 characters.'); } return undefined; }; type CreateApiKeyDialogProps = { open: boolean; onClose: () => void; onCreatedSecret: (secret: string) => void; }; function CreateApiKeyDialog({ open, onClose, onCreatedSecret }: CreateApiKeyDialogProps) { const { t } = useTranslation(); const createApiKey = useCreateAdminApiKey(); const labels = resourceLabels(t); const form = useAppForm({ defaultValues: { memo: '', permissions: initialPermissions }, onSubmit: async ({ value }) => { try { const apiKey = await createApiKey.mutateAsync(createAdminApiKeyInput(value)); onClose(); onCreatedSecret(apiKey.meta?.secret_token ?? ''); } catch { // Error toast is handled by the mutation. } }, }); const isSubmitting = useStore(form.store, (state) => state.isSubmitting); return (
validateMemo(t, value), }} > {(field) => ( )}
{API_KEY_RESOURCES.map((resource) => (

{labels[resource]}

{(field) => ( )}
))}
{t('Create Key')}
); } export default function CreateApiKeyButton() { const { t } = useTranslation(); const [secret, setSecret] = useState(''); return ( <> 0} title={t('Your new API key')} onClose={() => setSecret('')}>

{t( 'The application API key shown below will not be displayed again. Store it somewhere safe before closing this dialog.' )}

                    
                        {secret}
                    
                
{t('New API key')}}> {({ open, onClose }) => ( )} ); }