"use client"; import * as React from "react"; import { Eye, Pencil } from "lucide-react"; import { invalidateProjects } from "@/components/app/store"; import { api } from "@/lib/client/api"; import { errorMessage } from "@/lib/client/humanize"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/input"; import { Segmented } from "@/components/ui/segmented"; import { toast } from "@/components/ui/toast"; import { Markdown } from "@/components/markdown/markdown"; import { useIsMobile } from "@/lib/client/hooks"; import type { PublicProject } from "@/lib/client/types"; import { cn } from "@/lib/utils"; type Mode = "write" | "preview"; /** Free-form markdown notes: write / preview (side by side on desktop), explicit save, word count. */ export function ProjectNotes({ project, onSaved }: { project: PublicProject; onSaved: () => Promise | void }) { const isMobile = useIsMobile(); const [notes, setNotes] = React.useState(project.notes ?? ""); const [mode, setMode] = React.useState("write"); const [saving, setSaving] = React.useState(false); const last = React.useRef(project.updatedAt); React.useEffect(() => { if (last.current !== project.updatedAt) { last.current = project.updatedAt; setNotes(project.notes ?? ""); } }, [project]); const dirty = notes !== (project.notes ?? ""); const words = React.useMemo(() => (notes.trim() ? notes.trim().split(/\s+/).length : 0), [notes]); const save = async () => { setSaving(true); try { await api(`/api/projects/${project.id}`, { method: "PATCH", json: { notes: notes.trim() ? notes : null } }); await Promise.all([onSaved(), invalidateProjects()]); toast.success("Notes saved"); } catch (e) { toast.error("Could not save notes", errorMessage(e)); } finally { setSaving(false); } }; const editor =