import React from 'react'; import { useWorkspace } from '@/contexts/workspace'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { z } from 'zod'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Button } from '@/components/ui/button'; import { Spinner } from '@/components/ui/spinner'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { CreateNodeInput, Node } from '@/types/nodes'; import { NeuronId } from '@/lib/id'; import { generateKeyBetween } from 'fractional-indexing-jittered'; import { NodeTypes } from '@/lib/constants'; const formSchema = z.object({ name: z.string().min(3, 'Name must be at least 3 characters long.'), description: z.string(), }); interface SpaceCreateDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } export const SpaceCreateDialog = ({ open, onOpenChange, }: SpaceCreateDialogProps) => { const workspace = useWorkspace(); const [isPending, setIsPending] = React.useState(false); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { name: '', description: '', }, }); const handleCancel = () => { form.reset(); onOpenChange(false); }; const handleSubmit = async (values: z.infer) => { setIsPending(true); const spaceInput: CreateNodeInput = { id: NeuronId.generate(NeuronId.Type.Space), type: NodeTypes.Space, parentId: null, attrs: { name: values.name, description: values.description, }, }; const pageInput: CreateNodeInput = { id: NeuronId.generate(NeuronId.Type.Page), type: NodeTypes.Page, attrs: { name: 'Home', }, index: generateKeyBetween(null, null), parentId: spaceInput.id, }; const channelInput: CreateNodeInput = { id: NeuronId.generate(NeuronId.Type.Channel), type: NodeTypes.Channel, attrs: { name: 'Discussions', }, index: generateKeyBetween(pageInput.index, null), parentId: spaceInput.id, }; await workspace.createNodes([spaceInput, pageInput, channelInput]); setIsPending(false); onOpenChange(false); }; return ( Create space Create a new space to collaborate with your peers
( Name * )} /> ( Description