|
| 1 | +import { useCallback, useEffect, useState } from "react"; |
| 2 | +import { Link, useParams } from "react-router-dom"; |
| 3 | +import { MarkdownEditor } from "@/components/MarkdownEditor"; |
| 4 | +import { MarkdownPreview } from "@/components/MarkdownPreview"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { useAutoSave } from "@/hooks/use-auto-save"; |
| 7 | + |
| 8 | +type ViewMode = "edit" | "preview" | "split"; |
| 9 | + |
| 10 | +export function DocumentEditPage() { |
| 11 | + const { projectId, documentId } = useParams<{ |
| 12 | + projectId: string; |
| 13 | + documentId: string; |
| 14 | + }>(); |
| 15 | + const [content, setContent] = useState(""); |
| 16 | + const [isLoading, setIsLoading] = useState(true); |
| 17 | + const [error, setError] = useState<string | null>(null); |
| 18 | + const [viewMode, setViewMode] = useState<ViewMode>("split"); |
| 19 | + const [saveStatus, setSaveStatus] = useState<"saved" | "saving" | "unsaved">("saved"); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + async function loadDocument() { |
| 23 | + if (!projectId || !documentId) return; |
| 24 | + try { |
| 25 | + const response = await fetch(`/api/projects/${projectId}/documents/${documentId}`, { |
| 26 | + credentials: "include", |
| 27 | + }); |
| 28 | + if (!response.ok) throw new Error("Failed to load document"); |
| 29 | + const data = await response.json(); |
| 30 | + setContent(data.content ?? ""); |
| 31 | + } catch (err) { |
| 32 | + setError(err instanceof Error ? err.message : "Failed to load document"); |
| 33 | + } finally { |
| 34 | + setIsLoading(false); |
| 35 | + } |
| 36 | + } |
| 37 | + loadDocument(); |
| 38 | + }, [projectId, documentId]); |
| 39 | + |
| 40 | + const handleSave = useCallback( |
| 41 | + async (value: string) => { |
| 42 | + if (!projectId || !documentId) return; |
| 43 | + setSaveStatus("saving"); |
| 44 | + try { |
| 45 | + const response = await fetch(`/api/projects/${projectId}/documents/${documentId}`, { |
| 46 | + method: "PUT", |
| 47 | + headers: { "Content-Type": "application/json" }, |
| 48 | + credentials: "include", |
| 49 | + body: JSON.stringify({ content: value }), |
| 50 | + }); |
| 51 | + if (!response.ok) throw new Error("Failed to save document"); |
| 52 | + setSaveStatus("saved"); |
| 53 | + } catch { |
| 54 | + setSaveStatus("unsaved"); |
| 55 | + } |
| 56 | + }, |
| 57 | + [projectId, documentId], |
| 58 | + ); |
| 59 | + |
| 60 | + useAutoSave(content, handleSave); |
| 61 | + |
| 62 | + const handleChange = (value: string) => { |
| 63 | + setContent(value); |
| 64 | + setSaveStatus("unsaved"); |
| 65 | + }; |
| 66 | + |
| 67 | + if (isLoading) { |
| 68 | + return ( |
| 69 | + <div className="flex h-[calc(100vh-3.5rem)] items-center justify-center"> |
| 70 | + <div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" /> |
| 71 | + </div> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (error) { |
| 76 | + return ( |
| 77 | + <div className="flex h-[calc(100vh-3.5rem)] flex-col items-center justify-center gap-4"> |
| 78 | + <p className="text-destructive">{error}</p> |
| 79 | + <Button asChild variant="outline"> |
| 80 | + <Link to={`/projects/${projectId}`}>Back to Project</Link> |
| 81 | + </Button> |
| 82 | + </div> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="flex h-[calc(100vh-3.5rem)] flex-col"> |
| 88 | + {/* Toolbar */} |
| 89 | + <div className="flex items-center justify-between border-b px-4 py-2"> |
| 90 | + <div className="flex items-center gap-2 text-sm text-muted-foreground"> |
| 91 | + <Link to={`/projects/${projectId}`} className="hover:text-foreground"> |
| 92 | + Project |
| 93 | + </Link> |
| 94 | + <span>/</span> |
| 95 | + <span>Edit Document</span> |
| 96 | + <span className="ml-2 text-xs"> |
| 97 | + {saveStatus === "saving" && "(Saving...)"} |
| 98 | + {saveStatus === "saved" && "(Saved)"} |
| 99 | + {saveStatus === "unsaved" && "(Unsaved changes)"} |
| 100 | + </span> |
| 101 | + </div> |
| 102 | + <div className="flex items-center gap-1"> |
| 103 | + <Button |
| 104 | + variant={viewMode === "edit" ? "default" : "ghost"} |
| 105 | + size="sm" |
| 106 | + onClick={() => setViewMode("edit")} |
| 107 | + > |
| 108 | + Edit |
| 109 | + </Button> |
| 110 | + <Button |
| 111 | + variant={viewMode === "split" ? "default" : "ghost"} |
| 112 | + size="sm" |
| 113 | + onClick={() => setViewMode("split")} |
| 114 | + > |
| 115 | + Split |
| 116 | + </Button> |
| 117 | + <Button |
| 118 | + variant={viewMode === "preview" ? "default" : "ghost"} |
| 119 | + size="sm" |
| 120 | + onClick={() => setViewMode("preview")} |
| 121 | + > |
| 122 | + Preview |
| 123 | + </Button> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + |
| 127 | + {/* Editor / Preview */} |
| 128 | + <div className="flex min-h-0 flex-1"> |
| 129 | + {viewMode !== "preview" && ( |
| 130 | + <div className={`min-h-0 ${viewMode === "split" ? "w-1/2 border-r" : "w-full"}`}> |
| 131 | + <MarkdownEditor value={content} onChange={handleChange} /> |
| 132 | + </div> |
| 133 | + )} |
| 134 | + {viewMode !== "edit" && ( |
| 135 | + <div className={`min-h-0 overflow-auto ${viewMode === "split" ? "w-1/2" : "w-full"}`}> |
| 136 | + <MarkdownPreview content={content} /> |
| 137 | + </div> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments