mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-11 09:55:59 +00:00
5819bd8a59
* fix(frontend): paginate workspace chat list beyond 50 threads (#3482) The sidebar 'Recent chats' and /workspace/chats list were hard-capped at the first 50 threads returned by threads.search. Replace the single-shot useThreads() consumers with useInfiniteThreads() and add an IntersectionObserver sentinel to each list so further pages are fetched on demand. In search mode on the chats page, the sentinel is replaced by an explicit 'Load more' button to prevent the observer from draining the entire backend list while the filtered view stays empty. - Add useInfiniteThreads + page-size constant and pure cache helpers (map/filterInfiniteThreadsCache, getInfiniteThreadsNextPageParam) - Mirror rename / delete / stream-finish updates into the new infinite cache so optimistic UI stays consistent - Extend the e2e mock to honour limit/offset slicing - Unit tests for the cache helpers and pagination boundary - Playwright e2e covering chats page + sidebar load-more, and the search-mode guard against runaway auto-pagination - Add en/zh i18n entries for the search-mode load-more button Fixes #3482 * docs(frontend): clarify infinite-threads offset semantics and test post-delete invariant - Add docstring to getInfiniteThreadsNextPageParam explaining that TanStack Query freezes the returned offset into pageParams once, so optimistic cache mutations that shrink page lengths (filterInfiniteThreadsCache on delete) cannot retroactively move the offset backwards. Delete/rename paths reconcile against the backend via invalidateQueries in onSettled. - Add unit test covering the post-delete invariant. - Fix misleading comment in thread-list-infinite-scroll.spec.ts: the thread-search mock does not sort by updated_at; it returns the array in the order provided. Addresses Copilot CR comments on #3485. * fix(frontend): mirror onCreated upsert into infinite cache; add sidebar Load-older button Address review feedback on #3485: - New upsertThreadInInfiniteCache helper; useThreadStream onCreated now upserts into both the legacy ['threads','search'] cache and the new infinite cache, so a freshly created thread appears in the sidebar immediately during streaming instead of only after the run finishes and onSettled invalidates the query. Restores parity with main. - Sidebar Recent Chats now exposes a visible 'Load older chats' button alongside the IntersectionObserver sentinel, so keyboard-only users and environments where IO is unavailable can still reach older conversations. - Add zh-CN / en-US / types entry for chats.loadOlderChats. - Cover the new helper with 3 unit tests (no-op on uninitialised cache, prepend new thread to first page, merge with existing entry without duplication).
358 lines
12 KiB
TypeScript
358 lines
12 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
Download,
|
|
FileJson,
|
|
FileText,
|
|
MoreHorizontal,
|
|
Pencil,
|
|
Share2,
|
|
Trash2,
|
|
} from "lucide-react";
|
|
import Link from "next/link";
|
|
import { useParams, usePathname, useRouter } from "next/navigation";
|
|
import { useCallback, useEffect, useMemo, useRef, 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 { getAPIClient } from "@/core/api";
|
|
import { writeTextToClipboard } from "@/core/clipboard";
|
|
import { useI18n } from "@/core/i18n/hooks";
|
|
import {
|
|
exportThreadAsJSON,
|
|
exportThreadAsMarkdown,
|
|
} from "@/core/threads/export";
|
|
import {
|
|
useDeleteThread,
|
|
useInfiniteThreads,
|
|
useRenameThread,
|
|
} from "@/core/threads/hooks";
|
|
import type { AgentThread, AgentThreadState } from "@/core/threads/types";
|
|
import { pathOfThread, titleOfThread } from "@/core/threads/utils";
|
|
import { env } from "@/env";
|
|
import { isIMEComposing } from "@/lib/ime";
|
|
|
|
export function RecentChatList() {
|
|
const { t } = useI18n();
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const { thread_id: threadIdFromPath, agent_name: agentNameFromPath } =
|
|
useParams<{
|
|
thread_id: string;
|
|
agent_name?: string;
|
|
}>();
|
|
const {
|
|
data: infiniteThreads,
|
|
fetchNextPage,
|
|
hasNextPage,
|
|
isFetchingNextPage,
|
|
} = useInfiniteThreads();
|
|
const threads = useMemo(
|
|
() => infiniteThreads?.pages.flat() ?? [],
|
|
[infiniteThreads],
|
|
);
|
|
|
|
const sentinelRef = useRef<HTMLDivElement | null>(null);
|
|
useEffect(() => {
|
|
const element = sentinelRef.current;
|
|
if (!element || !hasNextPage) {
|
|
return;
|
|
}
|
|
const observer = new IntersectionObserver(
|
|
([entry]) => {
|
|
if (entry?.isIntersecting && hasNextPage && !isFetchingNextPage) {
|
|
void fetchNextPage();
|
|
}
|
|
},
|
|
{ rootMargin: "120px 0px 120px 0px" },
|
|
);
|
|
observer.observe(element);
|
|
return () => observer.disconnect();
|
|
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);
|
|
|
|
const { mutate: deleteThread } = useDeleteThread();
|
|
const { mutate: renameThread } = useRenameThread();
|
|
|
|
// Rename dialog state
|
|
const [renameDialogOpen, setRenameDialogOpen] = useState(false);
|
|
const [renameThreadId, setRenameThreadId] = useState<string | null>(null);
|
|
const [renameValue, setRenameValue] = useState("");
|
|
|
|
const handleDelete = useCallback(
|
|
(threadId: string) => {
|
|
deleteThread({ threadId });
|
|
if (threadId === threadIdFromPath) {
|
|
const threadIndex = threads.findIndex((t) => t.thread_id === threadId);
|
|
let nextThreadPath = pathOfThread("new", {
|
|
agent_name: agentNameFromPath,
|
|
});
|
|
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);
|
|
}
|
|
},
|
|
[agentNameFromPath, 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(
|
|
async (thread: AgentThread) => {
|
|
// Always use Vercel URL for sharing so others can access
|
|
const VERCEL_URL = "https://deer-flow-v2.vercel.app";
|
|
const isLocalhost =
|
|
window.location.hostname === "localhost" ||
|
|
window.location.hostname === "127.0.0.1";
|
|
// On localhost: use Vercel URL; On production: use current origin
|
|
const baseUrl = isLocalhost ? VERCEL_URL : window.location.origin;
|
|
const shareUrl = `${baseUrl}${pathOfThread(thread)}`;
|
|
try {
|
|
const didCopy = await writeTextToClipboard(shareUrl);
|
|
if (!didCopy) {
|
|
toast.error(t.clipboard.failedToCopyToClipboard);
|
|
return;
|
|
}
|
|
|
|
toast.success(t.clipboard.linkCopied);
|
|
} catch {
|
|
toast.error(t.clipboard.failedToCopyToClipboard);
|
|
}
|
|
},
|
|
[t],
|
|
);
|
|
|
|
const handleExport = useCallback(
|
|
async (thread: AgentThread, format: "markdown" | "json") => {
|
|
try {
|
|
const apiClient = getAPIClient();
|
|
const state = await apiClient.threads.getState<AgentThreadState>(
|
|
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],
|
|
);
|
|
|
|
if (threads.length === 0) {
|
|
return null;
|
|
}
|
|
return (
|
|
<>
|
|
<SidebarGroup>
|
|
<SidebarGroupLabel>
|
|
{env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true"
|
|
? t.sidebar.recentChats
|
|
: t.sidebar.demoChats}
|
|
</SidebarGroupLabel>
|
|
<SidebarGroupContent className="group-data-[collapsible=icon]:pointer-events-none group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0">
|
|
<SidebarMenu>
|
|
<div className="flex w-full flex-col gap-1">
|
|
{threads.map((thread) => {
|
|
const isActive = pathOfThread(thread) === pathname;
|
|
return (
|
|
<SidebarMenuItem
|
|
key={thread.thread_id}
|
|
className="group/side-menu-item"
|
|
>
|
|
<SidebarMenuButton isActive={isActive} asChild>
|
|
<div>
|
|
<Link
|
|
className="text-muted-foreground block w-full whitespace-nowrap group-hover/side-menu-item:overflow-hidden"
|
|
href={pathOfThread(thread)}
|
|
>
|
|
{titleOfThread(thread)}
|
|
</Link>
|
|
{env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true" && (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<SidebarMenuAction
|
|
showOnHover
|
|
className="bg-background/50 hover:bg-background"
|
|
>
|
|
<MoreHorizontal />
|
|
<span className="sr-only">{t.common.more}</span>
|
|
</SidebarMenuAction>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent
|
|
className="w-48 rounded-lg"
|
|
side={"right"}
|
|
align={"start"}
|
|
>
|
|
<DropdownMenuItem
|
|
onSelect={() =>
|
|
handleRenameClick(
|
|
thread.thread_id,
|
|
titleOfThread(thread),
|
|
)
|
|
}
|
|
>
|
|
<Pencil className="text-muted-foreground" />
|
|
<span>{t.common.rename}</span>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
onSelect={() => handleShare(thread)}
|
|
>
|
|
<Share2 className="text-muted-foreground" />
|
|
<span>{t.common.share}</span>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuSub>
|
|
<DropdownMenuSubTrigger>
|
|
<Download className="text-muted-foreground" />
|
|
<span>{t.common.export}</span>
|
|
</DropdownMenuSubTrigger>
|
|
<DropdownMenuSubContent>
|
|
<DropdownMenuItem
|
|
onSelect={() =>
|
|
handleExport(thread, "markdown")
|
|
}
|
|
>
|
|
<FileText className="text-muted-foreground" />
|
|
<span>{t.common.exportAsMarkdown}</span>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
onSelect={() =>
|
|
handleExport(thread, "json")
|
|
}
|
|
>
|
|
<FileJson className="text-muted-foreground" />
|
|
<span>{t.common.exportAsJSON}</span>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuSubContent>
|
|
</DropdownMenuSub>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem
|
|
onSelect={() => handleDelete(thread.thread_id)}
|
|
>
|
|
<Trash2 className="text-muted-foreground" />
|
|
<span>{t.common.delete}</span>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)}
|
|
</div>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
);
|
|
})}
|
|
{hasNextPage && (
|
|
<>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="mx-2 my-1 w-[calc(100%-1rem)] justify-center text-xs"
|
|
onClick={() => void fetchNextPage()}
|
|
disabled={isFetchingNextPage}
|
|
data-testid="recent-chat-list-load-more"
|
|
>
|
|
{isFetchingNextPage
|
|
? t.chats.loadingMore
|
|
: t.chats.loadOlderChats}
|
|
</Button>
|
|
<div
|
|
ref={sentinelRef}
|
|
aria-hidden="true"
|
|
className="h-px w-full"
|
|
data-testid="recent-chat-list-sentinel"
|
|
/>
|
|
</>
|
|
)}
|
|
</div>
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
|
|
{/* Rename Dialog */}
|
|
<Dialog open={renameDialogOpen} onOpenChange={setRenameDialogOpen}>
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
<DialogHeader>
|
|
<DialogTitle>{t.common.rename}</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="py-4">
|
|
<Input
|
|
value={renameValue}
|
|
onChange={(e) => setRenameValue(e.target.value)}
|
|
placeholder={t.common.rename}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "Enter" && !isIMEComposing(e)) {
|
|
e.preventDefault();
|
|
handleRenameSubmit();
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => setRenameDialogOpen(false)}
|
|
>
|
|
{t.common.cancel}
|
|
</Button>
|
|
<Button onClick={handleRenameSubmit}>{t.common.save}</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|