import { zodResolver } from '@hookform/resolvers/zod' import { Plus } from 'lucide-react' import { useParams } from 'next/navigation' import { parseAsString, useQueryState } from 'nuqs' import { SubmitHandler, useForm } from 'react-hook-form' import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, DialogTrigger, Form, FormControl, FormField, Input_Shadcn_, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import * as z from 'zod' import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation' 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 params = useParams() const projectRef = params?.ref as string const [visible, setVisible] = useQueryState('new', parseAsString.withDefault('')) const onOpenChange = (value: boolean) => { if (value) setVisible('publishable') else setVisible('') } const defaultValues = { name: '', description: '' } const form = useForm>({ resolver: zodResolver(SCHEMA), defaultValues: { name: '', description: '', }, }) const { mutate: createAPIKey, isPending: isCreatingAPIKey } = useAPIKeyCreateMutation() const onSubmit: SubmitHandler> = async (values) => { createAPIKey( { projectRef, type: 'publishable', name: values.name, description: values.description, }, { onSuccess: () => { form.reset(defaultValues) onOpenChange(false) }, } ) } return ( 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.
( )} /> ( )} />
) }