import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Input, Modal, ModalClose, ModalContent } from "@app/components/v2"; import { useWorkspace } from "@app/context"; import { useCreateWsEnvironment } from "@app/hooks/api"; import { WorkspaceEnv } from "@app/hooks/api/workspace/types"; import { slugSchema } from "@app/lib/schemas"; type Props = { isOpen: boolean; onOpenChange: (isOpen: boolean) => void; onComplete?: (environment: WorkspaceEnv) => void; }; const schema = z.object({ environmentName: z .string() .min(1, { message: "Environment Name field must be at least 1 character" }), environmentSlug: slugSchema({ max: 64 }) }); export type FormData = z.infer; type ContentProps = { onComplete: (environment: WorkspaceEnv) => void; }; const Content = ({ onComplete }: ContentProps) => { const { currentWorkspace } = useWorkspace(); const { mutateAsync, isPending } = useCreateWsEnvironment(); const { control, handleSubmit } = useForm({ resolver: zodResolver(schema) }); const onFormSubmit = async ({ environmentName, environmentSlug }: FormData) => { try { if (!currentWorkspace?.id) return; const env = await mutateAsync({ workspaceId: currentWorkspace.id, name: environmentName, slug: environmentSlug }); createNotification({ text: "Successfully created environment", type: "success" }); onComplete(env); } catch (err) { console.error(err); createNotification({ text: "Failed to create environment", type: "error" }); } }; return (
( )} /> ( )} />
); }; export const AddEnvironmentModal = ({ onComplete, ...props }: Props) => { return ( { if (onComplete) onComplete(env); props.onOpenChange(false); }} /> ); };