import { zodResolver } from '@hookform/resolvers/zod' import { useParams } from 'common' import { PlusIcon } from 'lucide-react' import { parseAsString, useQueryState } from 'nuqs' import { useRef } from 'react' import { SubmitHandler, useForm } from 'react-hook-form' import { toast } from 'sonner' import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, Form, FormControl, FormField, Input, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import * as z from 'zod' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { Shortcut } from '@/components/ui/Shortcut' import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation' import { getWriteGrantToastMessage, getWriteGrantTooltip, useGrant } from '@/lib/grants' import { useTrack } from '@/lib/telemetry/track' import { SHORTCUT_IDS } from '@/state/shortcuts/registry' const FORM_ID = 'create-publishable-api-key' const SCHEMA = z.object({ name: z.string(), description: z.string().trim(), }) export interface CreatePublishableAPIKeyDialogProps { projectRef: string } export const CreatePublishableAPIKeyDialog = () => { const { ref: projectRef } = useParams() const formRef = useRef(null) const [visible, setVisible] = useQueryState('new', parseAsString.withDefault('')) const onOpenChange = (value: boolean) => { if (value) setVisible('publishable') else setVisible('') } const openDialog = () => { if (canCreateAPIKeys) setVisible('publishable') } const defaultValues = { name: '', description: '' } const form = useForm>({ resolver: zodResolver(SCHEMA), defaultValues: { name: '', description: '', }, }) const track = useTrack() const createGrant = useGrant('POST /v1/projects/{ref}/api-keys', { projectRef }) const canCreateAPIKeys = createGrant.status === 'granted' const { mutate: createAPIKey, isPending: isCreatingAPIKey } = useAPIKeyCreateMutation({ // Hook-level so the event still fires if the dialog unmounts before the request finishes onSuccess: (_data, variables) => track( 'api_key_created', { keyType: 'publishable', source: 'create_dialog' }, { project: variables.projectRef } ), }) const onSubmit: SubmitHandler> = async (values) => { if (!createGrant.grant) { toast.error(getWriteGrantToastMessage(createGrant.status, 'create', 'API keys')) return } createAPIKey( { projectRef, grant: createGrant.grant, type: 'publishable', name: values.name, description: values.description, }, { onSuccess: () => { form.reset(defaultValues) onOpenChange(false) }, } ) } return ( } onClick={openDialog} disabled={!canCreateAPIKeys} tooltip={{ content: { side: 'bottom', text: getWriteGrantTooltip(createGrant.status, 'create', 'API keys'), }, }} > New publishable key Create new publishable API key Publishable API keys are used to authorize requests to your project from the web, mobile or desktop apps, CLIs or other public components of your application. They are safe to be published online and embedded in code.
( )} /> ( )} />
formRef.current?.requestSubmit()} options={{ enabled: visible === 'publishable' && !isCreatingAPIKey }} side="top" >
) }