"use client"; import { BookOpen, Eye, FileText, History, LayoutGrid, Loader2, PanelLeftClose, Pencil, Plus, RefreshCw, Trash2, } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { getStoredAuth } from "@/core/auth"; import { createSearchSpace } from "@/core/studio/api"; import { useStudioNotebooks } from "@/core/studio/hooks"; import { cn } from "@/lib/utils"; import { useAIWritingSessions } from "../../hooks/useAIWritingSessions"; import type { AIWritingSessionSummary } from "../../api/ai-writing-sessions"; import { useAIWritingHistory } from "../../contexts/AIWritingHistoryContext"; import { formatRelative } from "./AIWritingHistoryDropdown"; // 历史记录状态药丸样式(对齐多智能体会商的历史记录观感)。 // 注:`in_progress` 不再转圈显示「进行中」——刷新=重置后这些会话已无任何活进程, // 转圈会误导用户以为还在跑;统一显示为中性、不动的「未完成」。真正还在跑的是 `background` // (后台挂起,有 AIWritingJobExecutor 驱动),保留「挂起中」转圈。 const WRITING_STATUS_PILL: Record = { in_progress: { label: "未完成", className: "text-slate-400" }, background: { label: "挂起中", className: "text-sky-500", spin: true }, draft_ready: { label: "草稿", className: "text-amber-500" }, review_ready: { label: "审核", className: "text-purple-500", dot: true }, done: { label: "完成", className: "text-emerald-500" }, error: { label: "出错", className: "text-rose-500" }, }; const SHOW_NOTEBOOK_SPACE_ENTRIES = false; /** 单条历史记录项 —— 样式参考多智能体会商的 HistoryDropdown 列表项(圆角卡片 + 状态药丸 + 操作按钮)。 */ function WritingHistoryItem({ session, isCurrent, onSelect, onShowProgress, onDelete, onRename, }: { session: AIWritingSessionSummary; isCurrent: boolean; onSelect: () => void; onShowProgress?: () => void; onDelete: () => void; onRename: (title: string) => void; }) { const [renaming, setRenaming] = useState(false); const [value, setValue] = useState(session.title); const inputRef = useRef(null); const isBackground = session.status === "background"; // Row/title click only opens the history view; restore starts from the detail header. const primary = isBackground && onShowProgress ? onShowProgress : onSelect; const primaryTitle = isBackground ? "查看进度" : "打开历史"; const pill = WRITING_STATUS_PILL[session.status] ?? { label: session.status, className: "text-slate-400" }; useEffect(() => { if (renaming) inputRef.current?.focus(); }, [renaming]); const commit = () => { const t = value.trim(); if (t && t !== session.title) onRename(t); setRenaming(false); }; return (
  • {renaming ? ( setValue(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") commit(); if (e.key === "Escape") { setValue(session.title); setRenaming(false); } }} onBlur={commit} className="w-full rounded border border-slate-300 px-2 py-1 text-[11.5px] font-semibold text-slate-800 outline-none focus:border-primary dark:border-[#1b477e] dark:bg-[#022248] dark:text-[#e8edf5]" /> ) : ( )}
    {!renaming && (
    )}
  • ); } export function ResearchSidebar({ viewingNotebookId, onOpenNotebook, onOpenAllSpaces, onCollapse, }: { viewingNotebookId?: number | null; onOpenNotebook: (notebookId: number) => void; onOpenAllSpaces: () => void; /** 收起侧栏(由页面承载收起态)。 */ onCollapse?: () => void; }) { const { notebooks, isLoading: notebooksLoading, isError: notebooksError, } = useStudioNotebooks(); const queryClient = useQueryClient(); // 「历史记录」列表(左下角,样式参考多智能体会商的历史记录):自带一份会话列表, // 点击打开「历史回看」(openHistory,全局共享上下文,打开后本侧栏随写作进入历史态而隐藏)。 const { sessions, loading: sessionsLoading, refresh: refreshSessions, rename: renameSession, remove: removeSession, } = useAIWritingSessions(); const { openHistory, historySession } = useAIWritingHistory(); // 进入/返回写作配置页时刷新一次历史(本侧栏仅在 idle 态挂载,故等同每次回到表单刷新)。 useEffect(() => { void refreshSessions(); }, [refreshSessions]); const handleSelectHistory = (session: AIWritingSessionSummary) => { void openHistory(session); }; // 「挂起中」记录:交给右侧面板打开进度流程图弹窗(沿用面板已有的 window 事件解耦模式, // 与 ai-writing:open-session 一致),本侧栏不直接持有弹窗状态。 const handleShowProgress = (session: AIWritingSessionSummary) => { window.dispatchEvent(new CustomEvent("ai-writing:show-progress", { detail: session })); }; const [createOpen, setCreateOpen] = useState(false); const [name, setName] = useState(""); const [creating, setCreating] = useState(false); const [createError, setCreateError] = useState(""); const handleCreate = async () => { const trimmed = name.trim(); if (!trimmed) return; setCreating(true); setCreateError(""); try { const email = getStoredAuth()?.email ?? ""; const token = queryClient.getQueryData(["studio", "token", email]); if (!token) throw new Error("未登录或 token 已过期,请刷新页面"); await createSearchSpace(trimmed, token); setCreateOpen(false); setName(""); await queryClient.invalidateQueries({ queryKey: ["studio", "notebooks"] }); } catch (e) { setCreateError(e instanceof Error ? e.message : "创建失败"); } finally { setCreating(false); } }; return ( ); }