import { zodResolver } from '@hookform/resolvers/zod' import { useEffect } from 'react' import { SubmitHandler, useForm } from 'react-hook-form' import { toast } from 'sonner' import { Button, Dialog, DialogContent, 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 { useSchemaCreateMutation } from '@/data/database/schema-create-mutation' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' interface SchemaEditorProps { visible: boolean onSuccess: (schema: string) => void closePanel: () => void } const formSchema = z.object({ name: z.string().min(1, 'Please provide a name for your schema'), }) type FormSchema = z.infer export const SchemaEditor = ({ visible, onSuccess, closePanel }: SchemaEditorProps) => { const { data: project } = useSelectedProjectQuery() const { mutateAsync: createSchema, isPending } = useSchemaCreateMutation() const onSubmit: SubmitHandler = async (values) => { if (project === undefined) return console.error('Project is required') try { await createSchema({ projectRef: project.ref, connectionString: project.connectionString, name: values.name, }) onSuccess(values.name) toast.success(`Successfully created schema "${values.name}"`) } catch (error) { toast.error(`Failed to create schema: ${error}`) } } const form = useForm({ resolver: zodResolver(formSchema as any), defaultValues: { name: '', }, }) const { reset } = form useEffect(() => { if (visible) { reset() } }, [reset, visible]) const formId = 'schema-form' return ( Create a new schema
( )} />
) }