import React, { useState, useCallback, useEffect } 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, 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 EditSpaceDialogProps { open: boolean; onOpenChange: (open: boolean) => void; spaceId: string; initialName: string; initialDescription?: string | null; onSuccess?: () => void; } export function EditSpaceDialog({ open, onOpenChange, spaceId, initialName, initialDescription, onSuccess, }: EditSpaceDialogProps) { const [name, setName] = useState(""); const [editor, setEditor] = useState(); const fetcher = useFetcher(); const isLoading = fetcher.state === "submitting"; // Initialize form with existing data when dialog opens useEffect(() => { if (open) { setName(initialName); // Set the initial description in the editor when it's ready if (editor && initialDescription) { editor.commands.setContent(initialDescription); } } }, [open, initialName, initialDescription, editor]); 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/${spaceId}`, method: "PUT", encType: "application/json", }, ); }, [name, editor, fetcher, spaceId], ); // Handle successful update useEffect(() => { if (fetcher.data && fetcher.state === "idle") { onOpenChange(false); onSuccess?.(); } }, [fetcher.data, fetcher.state, onOpenChange, onSuccess]); return ( Edit Space
setName(e.target.value)} placeholder="Enter space name" required disabled={isLoading} maxLength={100} />
{ setEditor(editor); }} onCreate={({ editor }) => { setEditor(editor); // Set initial content when editor is created if (initialDescription) { editor.commands.setContent(initialDescription); } }} >
); }