"use client"; import { useState, useEffect, useRef } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { StickyNote, Pin, PinOff, Trash2, ChevronDown, ChevronUp, Save } from "lucide-react"; import { notesAPI } from "@/lib/api"; import { useAuthStore } from "@/stores/authStore"; interface NotesPanelProps { billId: string; } export function NotesPanel({ billId }: NotesPanelProps) { const token = useAuthStore((s) => s.token); const qc = useQueryClient(); const queryKey = ["note", billId]; const { data: note, isLoading } = useQuery({ queryKey, queryFn: () => notesAPI.get(billId), enabled: !!token, retry: false, // 404 = no note; don't retry throwOnError: false, }); const [open, setOpen] = useState(false); const [content, setContent] = useState(""); const [pinned, setPinned] = useState(false); const [saved, setSaved] = useState(false); const textareaRef = useRef(null); // Sync form from loaded note useEffect(() => { if (note) { setContent(note.content); setPinned(note.pinned); } }, [note]); // Auto-resize textarea useEffect(() => { const el = textareaRef.current; if (!el) return; el.style.height = "auto"; el.style.height = `${el.scrollHeight}px`; }, [content, open]); const upsert = useMutation({ mutationFn: () => notesAPI.upsert(billId, content, pinned), onSuccess: (updated) => { qc.setQueryData(queryKey, updated); setSaved(true); setTimeout(() => setSaved(false), 2000); }, }); const remove = useMutation({ mutationFn: () => notesAPI.delete(billId), onSuccess: () => { qc.removeQueries({ queryKey }); setContent(""); setPinned(false); setOpen(false); }, }); // Don't render for guests if (!token) return null; if (isLoading) return null; const hasNote = !!note; const isDirty = hasNote ? content !== note.content || pinned !== note.pinned : content.trim().length > 0; return (
{/* Header / toggle */} {open && (