Files
agent-chat-ui/src/components/thread/history/index.tsx

134 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-03-04 10:34:52 -08:00
import { Button } from "@/components/ui/button";
import { useThreads } from "@/providers/Thread";
2025-03-04 10:34:52 -08:00
import { Thread } from "@langchain/langgraph-sdk";
import { useEffect } from "react";
2025-03-06 15:58:02 -08:00
2025-03-04 10:34:52 -08:00
import { getContentString } from "../utils";
2025-03-04 11:01:19 -08:00
import { useQueryParam, StringParam, BooleanParam } from "use-query-params";
import {
Sheet,
SheetContent,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet";
2025-03-05 09:55:09 -08:00
import { Skeleton } from "@/components/ui/skeleton";
2025-03-06 15:58:02 -08:00
import { PanelRightOpen } from "lucide-react";
import { useMediaQuery } from "@/hooks/useMediaQuery";
2025-03-04 11:01:19 -08:00
function ThreadList({
threads,
onThreadClick,
}: {
threads: Thread[];
onThreadClick?: (threadId: string) => void;
}) {
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
return (
2025-03-06 15:58:02 -08:00
<div className="h-full flex flex-col w-full gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
2025-03-04 11:01:19 -08:00
{threads.map((t) => {
let itemText = t.thread_id;
if (
typeof t.values === "object" &&
t.values &&
"messages" in t.values &&
Array.isArray(t.values.messages) &&
t.values.messages?.length > 0
) {
const firstMessage = t.values.messages[0];
itemText = getContentString(firstMessage.content);
}
return (
2025-03-06 15:58:02 -08:00
<div key={t.thread_id} className="w-full px-1">
2025-03-04 11:01:19 -08:00
<Button
variant="ghost"
2025-03-06 15:58:02 -08:00
className="text-left items-start justify-start font-normal w-[280px]"
2025-03-04 11:01:19 -08:00
onClick={(e) => {
e.preventDefault();
onThreadClick?.(t.thread_id);
if (t.thread_id === threadId) return;
setThreadId(t.thread_id);
}}
>
2025-03-06 15:58:02 -08:00
<p className="truncate text-ellipsis">{itemText}</p>
2025-03-04 11:01:19 -08:00
</Button>
</div>
);
})}
</div>
);
}
2025-03-04 10:34:52 -08:00
2025-03-05 09:55:09 -08:00
function ThreadHistoryLoading() {
return (
2025-03-06 15:58:02 -08:00
<div className="h-full flex flex-col w-full gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
2025-03-05 09:55:09 -08:00
{Array.from({ length: 30 }).map((_, i) => (
2025-03-06 15:58:02 -08:00
<Skeleton key={`skeleton-${i}`} className="w-[280px] h-10" />
2025-03-05 09:55:09 -08:00
))}
</div>
);
}
2025-03-04 10:34:52 -08:00
export default function ThreadHistory() {
2025-03-06 15:58:02 -08:00
const isLargeScreen = useMediaQuery("(min-width: 1024px)");
2025-03-04 11:01:19 -08:00
const [chatHistoryOpen, setChatHistoryOpen] = useQueryParam(
"chatHistoryOpen",
BooleanParam,
);
const { getThreads, threads, setThreads, threadsLoading, setThreadsLoading } =
useThreads();
2025-03-04 10:34:52 -08:00
useEffect(() => {
2025-03-05 09:55:09 -08:00
if (typeof window === "undefined") return;
setThreadsLoading(true);
2025-03-05 09:55:09 -08:00
getThreads()
.then(setThreads)
.catch(console.error)
.finally(() => setThreadsLoading(false));
2025-03-04 10:34:52 -08:00
}, []);
return (
<>
2025-03-06 15:58:02 -08:00
<div className="hidden lg:flex flex-col border-r-[1px] border-slate-300 items-start justify-start gap-6 h-screen w-[300px] shrink-0 shadow-inner-right">
<div className="flex items-center justify-between w-full p-2">
<Button
className="hover:bg-gray-100"
variant="ghost"
onClick={() => setChatHistoryOpen((p) => !p)}
>
<PanelRightOpen />
</Button>
<h1 className="text-xl font-semibold tracking-tight">
Thread History
</h1>
</div>
{threadsLoading ? (
<ThreadHistoryLoading />
) : (
<ThreadList threads={threads} />
)}
2025-03-04 10:34:52 -08:00
</div>
2025-03-06 15:58:02 -08:00
<div className="lg:hidden">
<Sheet
open={!!chatHistoryOpen && !isLargeScreen}
onOpenChange={(open) => {
if (isLargeScreen) return;
setChatHistoryOpen(open);
}}
>
<SheetContent side="left" className="lg:hidden flex">
<SheetHeader>
<SheetTitle>Thread History</SheetTitle>
</SheetHeader>
<ThreadList
threads={threads}
onThreadClick={() => setChatHistoryOpen((o) => !o)}
/>
</SheetContent>
</Sheet>
</div>
2025-03-04 10:34:52 -08:00
</>
);
}