"use client"; import { ChevronLeft, ChevronRight, DownloadIcon, Pin, PinOff, Star, } from "lucide-react"; import Link from "next/link"; import { useEffect, useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { ScrollArea } from "@/components/ui/scroll-area"; import { ImportConversationsDialog } from "@/components/workspace/thread-share-dialogs"; import { CompactShortcutButtons, LayoutModeSwitcher, } from "@/components/workspace/appearance-controls"; import { WorkspaceBody, WorkspaceContainer, WorkspaceHeader, } from "@/components/workspace/workspace-container"; import { useI18n } from "@/core/i18n/hooks"; import { useThreadsCount, useThreadsPage, useToggleThreadFavorite, useToggleThreadPin, } from "@/core/threads/hooks"; import type { AgentThread } from "@/core/threads/types"; import { isThreadFavorite, isThreadPinned, usePathOfThread, titleOfThread, } from "@/core/threads/utils"; import { formatTimeAgo } from "@/core/utils/datetime"; import { desensitize } from "@/lib/desensitize"; import { cn } from "@/lib/utils"; const PAGE_SIZE = 20; export default function ChatsPage({ compactChrome = false, }: { compactChrome?: boolean; }) { const pathOfThread = usePathOfThread(); const { t } = useI18n(); const { toggle: togglePin } = useToggleThreadPin(); const { toggle: toggleFavorite } = useToggleThreadFavorite(); const [search, setSearch] = useState(""); const [debouncedSearch, setDebouncedSearch] = useState(""); const [onlyFavorites, setOnlyFavorites] = useState(false); const [page, setPage] = useState(1); const [importOpen, setImportOpen] = useState(false); useEffect(() => { document.title = `${t.pages.chats} - ${t.pages.appName}`; }, [t.pages.chats, t.pages.appName]); // Debounce the search box so each keystroke doesn't fire a backend query. useEffect(() => { const timer = setTimeout(() => setDebouncedSearch(search), 300); return () => clearTimeout(timer); }, [search]); // Reset to the first page whenever the filtered result set changes. useEffect(() => { setPage(1); }, [debouncedSearch, onlyFavorites]); // Conversations are paginated on the backend: each page is one bounded // query (title search + favorites filter included), so a long history is // never fetched in one go. Pinned conversations come from their own small // query and are surfaced in a dedicated section on the first page only, // and dropped from the paginated rows so they don't repeat on every page. const favoriteFilter = useMemo( () => (onlyFavorites ? { is_favorite: true } : {}), [onlyFavorites], ); const pinnedFilter = useMemo( () => ({ ...favoriteFilter, is_pinned: true }), [favoriteFilter], ); const { data: pageData } = useThreadsPage({ page, pageSize: PAGE_SIZE, search: debouncedSearch, metadata: favoriteFilter, }); const { data: pinnedData } = useThreadsPage({ page: 1, pageSize: 100, search: debouncedSearch, metadata: pinnedFilter, }); const { data: totalCount } = useThreadsCount({ search: debouncedSearch, metadata: favoriteFilter, }); const pinnedThreads = pinnedData?.threads ?? []; const pagedThreads = useMemo( () => (pageData?.threads ?? []).filter((t) => !isThreadPinned(t)), [pageData?.threads], ); const currentPage = page; const hasMore = pageData?.hasMore ?? false; // Page windows are sliced over the raw (pinned-inclusive) row order, so the // page count derives from the raw total as well. const totalPages = totalCount != null ? Math.max(1, Math.ceil(totalCount / PAGE_SIZE)) : null; const renderThread = (thread: AgentThread) => { const pinned = isThreadPinned(thread); const favorited = isThreadFavorite(thread); return (
{pinned && ( )} {desensitize(titleOfThread(thread, t.pages.newChat))}
{thread.updated_at && (
{formatTimeAgo(thread.updated_at)}
)} {/* Row actions — kept out of the Link so clicks don't navigate. */}
); }; const isEmpty = pagedThreads.length === 0 && pinnedThreads.length === 0; return ( ) : null } />
setSearch(e.target.value)} /> {totalCount != null && (
共 {totalCount} 条对话
)}
{currentPage === 1 && pinnedThreads.length > 0 && ( <>
{t.chats.pinnedSection}
{pinnedThreads.map(renderThread)} )} {pagedThreads.map(renderThread)} {isEmpty && (
{onlyFavorites ? t.chats.noFavorites : t.chats.noChats}
)} {(currentPage > 1 || hasMore) && (
{totalPages != null ? `${currentPage} / ${totalPages}` : currentPage}
)}
); }