"use client"; import { ChevronDown, Download, FileJson, FileText, MessageSquare, MoreHorizontal, Pencil, Pin, PinOff, Share2, Star, Trash2, } from "lucide-react"; import Link from "next/link"; import { useParams, usePathname, useRouter } from "next/navigation"; import { useCallback, useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarMenu, SidebarMenuAction, SidebarMenuButton, SidebarMenuItem, } from "@/components/ui/sidebar"; import { ShareConversationDialog } from "@/components/workspace/thread-share-dialogs"; import { getAPIClient } from "@/core/api"; import { useI18n } from "@/core/i18n/hooks"; import { exportThreadAsJSON, exportThreadAsMarkdown, } from "@/core/threads/export"; import { useDeleteThread, useRenameThread, useThreads, useToggleThreadFavorite, useToggleThreadPin, } from "@/core/threads/hooks"; import type { AgentThread, AgentThreadState } from "@/core/threads/types"; import { isThreadFavorite, isThreadPinned, pathOfThread, sortThreadsPinnedFirst, titleOfThread, } from "@/core/threads/utils"; import { env } from "@/env"; import { desensitize } from "@/lib/desensitize"; import { isIMEComposing } from "@/lib/ime"; import { cn } from "@/lib/utils"; export function RecentChatList({ compact = false }: { compact?: boolean }) { const { t } = useI18n(); const router = useRouter(); const pathname = usePathname(); const [internalCollapsed, setInternalCollapsed] = useState( () => localStorage.getItem("sidebar:recent-chats-collapsed") === "true", ); // 紧凑模式下整列由 WorkspaceSidebar 统一收起,列表本身始终展开、不显示标题折叠钮。 const collapsed = compact ? false : internalCollapsed; const toggleCollapsed = () => { if (compact) return; const next = !internalCollapsed; setInternalCollapsed(next); localStorage.setItem("sidebar:recent-chats-collapsed", String(next)); }; const { thread_id: threadIdFromPath, agent_id: agentIdFromPath } = useParams<{ thread_id: string; agent_id?: string; }>(); const { data: threads = [] } = useThreads(); const { mutate: deleteThread } = useDeleteThread(); const { mutate: renameThread } = useRenameThread(); const { toggle: togglePin } = useToggleThreadPin(); const { toggle: toggleFavorite } = useToggleThreadFavorite(); // Rename dialog state const [renameDialogOpen, setRenameDialogOpen] = useState(false); const [renameThreadId, setRenameThreadId] = useState(null); const [renameValue, setRenameValue] = useState(""); // Share dialog state — the conversation whose share code is being shown. const [shareThreadId, setShareThreadId] = useState(null); const handleDelete = useCallback( (threadId: string) => { deleteThread({ threadId }); if (threadId === threadIdFromPath) { const threadIndex = threads.findIndex((t) => t.thread_id === threadId); let nextThreadPath = pathOfThread("new", { agent_id: agentIdFromPath, }); if (threadIndex > -1) { if (threads[threadIndex + 1]) { nextThreadPath = pathOfThread(threads[threadIndex + 1]!); } else if (threads[threadIndex - 1]) { nextThreadPath = pathOfThread(threads[threadIndex - 1]!); } } void router.push(nextThreadPath); } }, [agentIdFromPath, deleteThread, router, threadIdFromPath, threads], ); const handleRenameClick = useCallback( (threadId: string, currentTitle: string) => { setRenameThreadId(threadId); setRenameValue(currentTitle); setRenameDialogOpen(true); }, [], ); const handleRenameSubmit = useCallback(() => { if (renameThreadId && renameValue.trim()) { renameThread({ threadId: renameThreadId, title: renameValue.trim() }); setRenameDialogOpen(false); setRenameThreadId(null); setRenameValue(""); } }, [renameThread, renameThreadId, renameValue]); const handleShare = useCallback((thread: AgentThread) => { // Opens the share-code dialog; the recipient imports a copy via that code. setShareThreadId(thread.thread_id); }, []); const handleExport = useCallback( async (thread: AgentThread, format: "markdown" | "json") => { try { const apiClient = getAPIClient(); const state = await apiClient.threads.getState( thread.thread_id, ); const messages = state.values?.messages ?? []; if (messages.length === 0) { toast.error(t.conversation.noMessages); return; } if (format === "markdown") { exportThreadAsMarkdown(thread, messages); } else { exportThreadAsJSON(thread, messages); } toast.success(t.common.exportSuccess); } catch { toast.error("Failed to export conversation"); } }, [t], ); const visibleThreads = sortThreadsPinnedFirst( threads.filter( (thread) => thread.metadata?.thread_type !== "scheduler" && thread.metadata?.thread_type !== "bootstrap" && thread.metadata?.thread_type !== "roundtable", ), ); if (visibleThreads.length === 0) { return null; } return ( <> {env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true" ? t.sidebar.recentChats : t.sidebar.demoChats} {compact && ( {visibleThreads.length} )} {!compact && ( )}
{visibleThreads.map((thread) => { const isActive = pathOfThread(thread) === pathname; const pinned = isThreadPinned(thread); const favorited = isThreadFavorite(thread); return ( {compact && ( )} {pinned && ( )} {desensitize(titleOfThread(thread, t.pages.newChat))} {env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true" && ( {t.common.more} handleRenameClick( thread.thread_id, titleOfThread(thread, t.pages.newChat), ) } > {t.common.rename} togglePin(thread.thread_id, !pinned) } > {pinned ? ( ) : ( )} {pinned ? t.common.unpin : t.common.pin} toggleFavorite(thread.thread_id, !favorited) } > {favorited ? t.common.unfavorite : t.common.favorite} handleShare(thread)} > {t.common.share} {t.common.export} handleExport(thread, "markdown") } > {t.common.exportAsMarkdown} handleExport(thread, "json") } > {t.common.exportAsJSON} handleDelete(thread.thread_id)} > {t.common.delete} )} ); })}
{/* Rename Dialog */} {t.common.rename}
setRenameValue(e.target.value)} placeholder={t.common.rename} onKeyDown={(e) => { if (e.key === "Enter" && !isIMEComposing(e)) { e.preventDefault(); handleRenameSubmit(); } }} />
{/* Share Dialog */} { if (!o) setShareThreadId(null); }} /> ); }