xonecas.io
53129
Pa

PAT / files

PAT / src / components / Sidebar.tsx

2.7 KB
1"use client";
2
3import { useAppContext } from "@/src/components/App";
4import Button from "@/src/components/Button";
5import BowtieIcon from "@/src/components/icons/BowtieIcon";
6import ChevronDownIcon from "@/src/components/icons/ChevronDownIcon";
7import PlusIcon from "@/src/components/icons/PlusIcon";
8import { useThreads } from "@/src/hooks/api/useThreads";
9
10export default function Sidebar() {
11  const { selectedThreadId, setSelectedThreadId } = useAppContext();
12  const {
13    data: threadsData,
14    isLoading: isThreadsLoading,
15    error: threadsError,
16    fetchNextPage,
17    hasNextPage,
18  } = useThreads();
19
20  const totalThreadCount = threadsData?.pages[0]?.totalCount ?? 0;
21  const loadedThreadCount = threadsData?.pages.flatMap((page) => page.threads).length ?? 0;
22
23  return (
24    <div className="h-screen w-80 overflow-y-auto bg-neutral-50 p-8 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
25      <div className="flex flex-col gap-8">
26        <div className="flex justify-center">
27          <BowtieIcon className="mb-8 h-12 w-12" />
28        </div>
29        <div className="flex flex-col gap-2">
30          <Button onClick={() => setSelectedThreadId(null)}>
31            <PlusIcon className="h-4 w-4" />
32            New Thread
33          </Button>
34        </div>
35        <div className="flex flex-col gap-4">
36          <label className="px-2 text-neutral-500">
37            Threads{" "}
38            <span className="text-neutral-400 dark:text-neutral-500">
39              ({loadedThreadCount} out of {totalThreadCount})
40            </span>
41          </label>
42          <div className="flex flex-col gap-2">
43            <div className="flex flex-col text-neutral-600 dark:text-neutral-400">
44              {isThreadsLoading
45                ? "Loading threads..."
46                : threadsError
47                  ? "Failed to load threads"
48                  : loadedThreadCount === 0
49                    ? "No threads yet"
50                    : threadsData?.pages
51                        .flatMap((page) => page.threads)
52                        .map((thread) => (
53                          <Button
54                            key={thread.id}
55                            isSelected={selectedThreadId === thread.id}
56                            onClick={() => setSelectedThreadId(thread.id)}
57                          >
58                            {thread.title}
59                          </Button>
60                        ))}
61            </div>
62            {hasNextPage && (
63              <Button onClick={() => fetchNextPage()}>
64                <ChevronDownIcon className="h-4 w-4" />
65                Load More
66              </Button>
67            )}
68          </div>
69        </div>
70      </div>
71    </div>
72  );
73}