"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { format } from "date-fns"; import { AlertTriangle, ArrowLeft, Link2, MessageSquareText } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { useToast } from "@/components/ui/use-toast"; import { cn } from "@/lib/utils"; import { parseDue } from "@/lib/feedback-parse"; import { commitFeedback, previewFeedback, type FeedbackPreview, type FeedbackPreviewNote } from "@/actions/feedback"; type NoteState = { include: boolean; text: string; versionId: string | null; /** Typed frame; null = use the selected version's converted frame */ frame: string | null; setChanges: boolean; }; type RoundState = { title: string; due: string; notes: Record }; const LOCAL_DT = "yyyy-MM-dd'T'HH:mm"; function initialState(preview: FeedbackPreview): RoundState[] { return preview.rounds.map((r) => { const due = parseDue(r.dueText); return { title: r.title || `Feedback ${format(new Date(), "d MMM")}`, due: due ? format(due, LOCAL_DT) : "", notes: Object.fromEntries( r.notes.map((n) => [ n.key, { include: true, text: n.text, versionId: n.defaultVersionId, frame: null, setChanges: true }, ]), ), }; }); } export function FeedbackPasteDialog({ projectId, projectName, open, onOpenChange, }: { projectId: string; projectName: string; open: boolean; onOpenChange: (open: boolean) => void; }) { const router = useRouter(); const { toast } = useToast(); const [input, setInput] = useState(""); const [preview, setPreview] = useState(null); const [rounds, setRounds] = useState([]); const [isPending, startTransition] = useTransition(); const reset = () => { setInput(""); setPreview(null); setRounds([]); }; const read = (text: string) => startTransition(async () => { try { const p = await previewFeedback(projectId, text); if (p.rounds.length === 0) { toast({ title: "No shot notes found", description: "Each note needs to start with a shot code.", variant: "destructive" }); return; } setPreview(p); setRounds(initialState(p)); } catch (e) { toast({ title: "Couldn't read feedback", description: (e as Error).message, variant: "destructive" }); } }); // Accepting a suggested code rewrites the paste and reads it again, so // everything downstream (versions, frames) comes from the real shot. const applySuggestion = (code: string, suggestion: string) => { const next = input.split(code).join(suggestion); setInput(next); read(next); }; const updateNote = (ri: number, key: string, patch: Partial) => setRounds((rs) => rs.map((r, i) => (i === ri ? { ...r, notes: { ...r.notes, [key]: { ...r.notes[key], ...patch } } } : r))); const frameFor = (n: FeedbackPreviewNote, s: NoteState): number | null => { if (s.frame !== null) return s.frame.trim() === "" ? null : Number(s.frame); return n.versions.find((v) => v.id === s.versionId)?.playerFrame ?? null; }; const included = preview ? preview.rounds.flatMap((r, ri) => r.notes.filter((n) => rounds[ri]?.notes[n.key]?.include && rounds[ri].notes[n.key].text.trim())) : []; const save = () => { if (!preview) return; const payload = preview.rounds .map((r, ri) => { const rs = rounds[ri]; return { title: rs.title, dueAt: rs.due ? new Date(rs.due).toISOString() : null, notes: r.notes .filter((n) => rs.notes[n.key].include && rs.notes[n.key].text.trim()) .map((n) => { const s = rs.notes[n.key]; const frame = frameFor(n, s); return { code: n.code, shotId: n.shot?.id ?? null, text: s.text.trim(), versionId: n.shot ? s.versionId : null, frame: frame != null && Number.isFinite(frame) && frame >= 0 ? Math.floor(frame) : null, setChanges: !!s.versionId && s.setChanges, }; }), }; }) .filter((r) => r.notes.length > 0); if (payload.length === 0) return; startTransition(async () => { try { const res = await commitFeedback({ projectId, rounds: payload }); toast({ title: `${res.notes} note${res.notes === 1 ? "" : "s"} logged`, description: `${res.comments} in the player · ${res.tasksChanged} task${res.tasksChanged === 1 ? "" : "s"} moved to Changes`, }); reset(); onOpenChange(false); router.refresh(); } catch (e) { toast({ title: "Couldn't save feedback", description: (e as Error).message, variant: "destructive" }); } }); }; const field = "rounded-md border border-zinc-700 bg-zinc-950 px-2 text-xs text-white placeholder:text-zinc-600 focus:border-amber-500/50 focus:outline-none"; return ( { if (!o) reset(); onOpenChange(o); }}> Paste client feedback · {projectName} {preview ? "Check where each note will go. Notes are saved in the player as client feedback and added to the board." : "Paste the message as it arrived. Each note should start with a shot code; a line like “FEEDBACK 113 DUE Oct 7 12:00 PM” starts a round."} {!preview ? (