"use client"; import { favoriteSnapshot, favoriteKind, FAVORITE_LABELS } from "@/lib/favorite-content"; import { favoriteRecordRoute } from "@/lib/resource-routes"; import Tooltip from "@/shared/ui/Tooltip"; import dynamic from "next/dynamic"; import { useEffect, useState } from "react"; import { BookOpen, FileQuestion, Calculator, Bot, ExternalLink, MessageSquare, NotebookPen, Pencil, Search, Video, } from "lucide-react"; import { useTranslation } from "react-i18next"; import { ConfirmDialog } from "@/components/ui/ConfirmDialog"; import NotebookRecordActions from "@/components/notebook/NotebookRecordActions"; import { FavoriteKindIcon } from "@/components/notebook/favorite-kinds"; import { formatRelativeTime } from "@/lib/relative-time"; import { notify } from "@/lib/notifications"; import type { NotebookRecordItem, NotebookSummary } from "@/lib/notebook-api"; const FavoriteContent = dynamic(() => import("./NotebookFavoriteContent"), { ssr: false, }); const AddToNote = dynamic( () => import("@/features/notes/components/AddToNote"), { ssr: false }, ); const MarkdownRenderer = dynamic( () => import("@/components/common/MarkdownRenderer"), { ssr: false, }, ); interface NotebookRecordRowProps { record: NotebookRecordItem; notebooks: NotebookSummary[]; currentNotebookId: string; expanded: boolean; onToggle: () => void; onEdit: ( recordId: string, changes: { title?: string; summary?: string; output?: string }, ) => Promise; onDelete: (recordId: string) => Promise; onRelocate: ( recordId: string, targetNotebookId: string, mode: "move" | "copy", ) => Promise; onOpenSession: (sessionId: string) => void; } const BADGES: Record< string, { labelKey: string; className: string; icon: typeof MessageSquare } > = { reading: { labelKey: "Reading", className: "bg-teal-100 text-teal-700 dark:bg-teal-900/40 dark:text-teal-300", icon: BookOpen }, question: { labelKey: "Question", className: "bg-orange-100 text-orange-700 dark:bg-orange-900/40 dark:text-orange-300", icon: FileQuestion }, solve: { labelKey: "Solution", className: "bg-indigo-100 text-indigo-700 dark:bg-indigo-900/40 dark:text-indigo-300", icon: Calculator }, chat: { labelKey: "Chat", className: "bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300", icon: MessageSquare, }, tutorbot: { labelKey: "Partner", className: "bg-violet-100 text-violet-700 dark:bg-violet-900/40 dark:text-violet-300", icon: Bot, }, research: { labelKey: "Research", className: "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300", icon: Search, }, co_writer: { labelKey: "Learning notebook", className: "bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300", icon: Pencil, }, video_learning: { labelKey: "Video Learning", className: "bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-300", icon: Video, }, }; const FALLBACK_BADGE = { className: "bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-400", icon: NotebookPen, }; /** Which confirmation the dialog is currently asking for, if any. */ type Pending = null | "delete" | "discard"; export default function NotebookRecordRow({ record, notebooks, currentNotebookId, expanded, onToggle, onEdit, onDelete, onRelocate, onOpenSession, }: NotebookRecordRowProps) { const { t, i18n } = useTranslation(); const [editing, setEditing] = useState(false); const [previewing, setPreviewing] = useState(false); const [busy, setBusy] = useState(false); const [pending, setPending] = useState(null); const [draftTitle, setDraftTitle] = useState(record.title); const [draftSummary, setDraftSummary] = useState(record.summary ?? ""); const [draftOutput, setDraftOutput] = useState(record.output ?? ""); const [failure, setFailure] = useState(null); // Re-seed the draft whenever a different record lands in this row, or the // saved values change underneath an idle editor. useEffect(() => { if (editing) return; setDraftTitle(record.title); setDraftSummary(record.summary ?? ""); setDraftOutput(record.output ?? ""); }, [record.id, record.title, record.summary, record.output, editing]); const badge = BADGES[favoriteKind(record.type, record.metadata)] ?? null; const BadgeIcon = badge?.icon ?? FALLBACK_BADGE.icon; const partnerName = typeof record.metadata?.partner_name === "string" ? record.metadata.partner_name.trim() : ""; const badgeLabel = badge ? record.type === "tutorbot" && partnerName ? partnerName : t(badge.labelKey) : t(FAVORITE_LABELS[favoriteKind(record.type, record.metadata)] || "Favorite content"); const sessionId = String(record.metadata?.session_id ?? ""); const canOpenSession = record.type === "chat" && Boolean(sessionId); const moveTargets = notebooks.filter( (n) => n.id !== currentNotebookId && !n.unreadable, ); const dirty = draftTitle !== record.title || draftSummary !== (record.summary ?? "") || draftOutput !== (record.output ?? ""); const startEditing = () => { setDraftTitle(record.title); setDraftSummary(record.summary ?? ""); setDraftOutput(record.output ?? ""); setFailure(null); setPreviewing(false); setEditing(true); }; const leaveEditor = () => { setEditing(false); setFailure(null); setPending(null); }; const requestCancel = () => { if (dirty) { setPending("discard"); return; } leaveEditor(); }; const save = async () => { if (!draftTitle.trim()) { setFailure(t("A record needs a title.")); return; } setBusy(true); setFailure(null); try { // Send only what actually changed — the backend treats an omitted // field as "leave alone", which is how a rename keeps the record's // other values intact. const changes: { title?: string; summary?: string; output?: string } = {}; if (draftTitle !== record.title) changes.title = draftTitle.trim(); if (draftSummary !== (record.summary ?? "")) changes.summary = draftSummary; if (Object.keys(changes).length) await onEdit(record.id, changes); setEditing(false); notify(t("Record saved"), { tone: "success" }); } catch (err) { setFailure(err instanceof Error ? err.message : String(err)); } finally { setBusy(false); } }; const runDelete = async () => { setBusy(true); setPending(null); try { await onDelete(record.id); notify(t("Record deleted"), { tone: "success" }); } catch (err) { setFailure(err instanceof Error ? err.message : String(err)); setBusy(false); } }; const runRelocate = async (targetId: string, mode: "move" | "copy") => { const target = notebooks.find((n) => n.id === targetId); setBusy(true); setFailure(null); try { await onRelocate(record.id, targetId, mode); notify( mode === "move" ? t('Moved to "{{name}}"', { name: target?.name ?? targetId }) : t('Copied to "{{name}}"', { name: target?.name ?? targetId }), { tone: "success" }, ); } catch (err) { setFailure(err instanceof Error ? err.message : String(err)); } finally { setBusy(false); } }; const timestamp = record.created_at ? new Date(record.created_at * 1000).toLocaleString() : ""; const kind = favoriteKind(record.type, record.metadata); const messageCount = Number(record.metadata?.message_count ?? 0); return (
{ if (!expanded) onToggle(); startEditing(); }} onDelete={() => setPending("delete")} onRelocate={(targetId, mode) => void runRelocate(targetId, mode)} />
{expanded && (
{failure && (

{failure}

)} {editing ? ( setPreviewing((v) => !v)} onSave={() => void save()} onCancel={requestCancel} /> ) : ( <> {record.summary &&

{record.summary}

} {record.type !== "chat" && record.user_query && (

{t("Query:")} {record.user_query}

)}
{canOpenSession && ( )} notebook.id === currentNotebookId), )} sourceUrl={favoriteRecordRoute(currentNotebookId, record.id)} className="dt-fav-add-to-note" />
)}
)} void runDelete()} onCancel={() => setPending(null)} >

{t('"{{title}}" will be removed from this notebook.', { title: record.title, })}

setPending(null)} >

{t("Your edits to this record have not been saved yet.")}

); } /** * The record editor. * * Fields read as one continuous document — title, then summary, then body — * rather than three boxed inputs, so editing feels like working on the * record instead of filling a form. */ function RecordEditor({ title, summary, output, previewing, busy, dirty, onTitleChange, onSummaryChange, onTogglePreview, onSave, onCancel, }: { title: string; summary: string; output: string; previewing: boolean; busy: boolean; dirty: boolean; onTitleChange: (value: string) => void; onSummaryChange: (value: string) => void; onOutputChange: (value: string) => void; onTogglePreview: () => void; onSave: () => void; onCancel: () => void; }) { const { t } = useTranslation(); return (
onTitleChange(e.target.value)} placeholder={t("Record title")} aria-label={t("Record title")} className="w-full border-0 bg-transparent p-0 text-[13.5px] font-semibold text-[var(--foreground)] outline-none placeholder:font-normal placeholder:text-[var(--muted-foreground)]" />