import React, { useState, useCallback } from "react"; import { useFetcher } from "@remix-run/react"; import { Document } from "@tiptap/extension-document"; import HardBreak from "@tiptap/extension-hard-break"; import { History } from "@tiptap/extension-history"; import { Paragraph } from "@tiptap/extension-paragraph"; import { Text } from "@tiptap/extension-text"; import { type Editor } from "@tiptap/react"; import { EditorContent, Placeholder, EditorRoot } from "novel"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "../ui/dialog"; import { Button } from "../ui/button"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; import { LoaderCircle } from "lucide-react"; import { cn } from "~/lib/utils"; interface NewSpaceDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSuccess?: () => void; } export function NewSpaceDialog({ open, onOpenChange, onSuccess, }: NewSpaceDialogProps) { const [name, setName] = useState(""); const [editor, setEditor] = useState(); const fetcher = useFetcher(); const isLoading = fetcher.state === "submitting"; const handleSubmit = useCallback( (e: React.FormEvent) => { e.preventDefault(); if (!name.trim()) { return; } const descriptionText = editor?.getHTML() || ""; fetcher.submit( { name: name.trim(), description: descriptionText, }, { action: "/api/v1/spaces", method: "post", encType: "application/json", }, ); }, [name, editor, fetcher], ); // Handle successful creation React.useEffect(() => { if (fetcher.data && fetcher.state === "idle") { setName(""); editor?.commands.clearContent(true); onOpenChange(false); } }, [fetcher.data, fetcher.state, editor, onOpenChange]); return ( Create New Space
setName(e.target.value)} placeholder="Enter space name" required disabled={isLoading} maxLength={100} />
{ setEditor(editor); }} shouldRerenderOnTransaction={false} editorProps={{ attributes: { class: `prose prose-sm dark:prose-invert prose-headings:font-title font-default focus:outline-none max-w-full`, }, }} immediatelyRender={false} className={cn( "editor-container max-h-[200px] min-h-[80px] w-full overflow-auto rounded p-1 text-sm", )} />
); }