forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

rag-thread-list.tsx134 lines, 5.9 KiBHistoryRawDownload
1import { For, Show, createEffect, createSignal, on } from "solid-js";
2import { createResource } from "@/lib/create-resource";
3import { getRagThreads } from "@/api/rag";
4import type { RagThread } from "@/api/client";
5import { EmptyInline } from "@/components/ui/empty-inline";
6import { IconEdit, IconTrash } from "@/components/ui/icons";
7import { PageSpinner } from "@/components/ui/page-spinner";
8import { PaginationControls } from "@/components/ui/pagination-controls";
9import { TableRowActions } from "@/components/ui/table-row-actions";
10import { cn } from "@/lib/cn";
11import { formatDateTime } from "@/lib/format";
12import type { Locale } from "@/i18n/messages";
13
14const THREAD_PAGE_SIZE = 12;
15const DAY_MS = 86_400_000;
16
17type Bucket = "today" | "yesterday" | "week" | "earlier";
18const BUCKET_ORDER: Bucket[] = ["today", "yesterday", "week", "earlier"];
19
20/** Which recency bucket a thread falls in, by local calendar day rather than a
21 * rolling 24 hours — "dün" has to mean yesterday's date, not 25 hours ago. */
22function bucketOf(updatedAt: number, now: Date): Bucket {
23 const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
24 if (updatedAt >= startOfToday) return "today";
25 if (updatedAt >= startOfToday - DAY_MS) return "yesterday";
26 if (updatedAt >= startOfToday - 6 * DAY_MS) return "week";
27 return "earlier";
28}
29
30export function RagThreadList(props: {
31 activeId?: string;
32 version: number;
33 locale: Locale;
34 labels: {
35 untitled: string;
36 emptyThreads: string;
37 emptyThreadsHint: string;
38 rename: string;
39 actions: string;
40 delete: string;
41 groupToday: string;
42 groupYesterday: string;
43 groupThisWeek: string;
44 groupEarlier: string;
45 };
46 onOpen: (thread: RagThread) => void;
47 onRename: (thread: RagThread) => void;
48 onRemove: (thread: RagThread) => void;
49}) {
50 const [page, setPage] = createSignal(0);
51 const [threads] = createResource(
52 () => ({ page: page(), version: props.version }),
53 ({ page }) => getRagThreads({ limit: THREAD_PAGE_SIZE, offset: page * THREAD_PAGE_SIZE }),
54 );
55 const totalPages = () => Math.max(1, Math.ceil((threads.latest?.total ?? 0) / THREAD_PAGE_SIZE));
56 const bucketLabel = (bucket: Bucket) =>
57 bucket === "today" ? props.labels.groupToday
58 : bucket === "yesterday" ? props.labels.groupYesterday
59 : bucket === "week" ? props.labels.groupThisWeek
60 : props.labels.groupEarlier;
61 // Grouping is presentational only — the page still comes from the server in
62 // its own order, so an empty bucket simply does not get a heading.
63 const groups = (items: RagThread[]) => {
64 const now = new Date();
65 return BUCKET_ORDER.flatMap((bucket) => {
66 const rows = items.filter((thread) => bucketOf(thread.updated_at, now) === bucket);
67 return rows.length > 0 ? [{ bucket, rows }] : [];
68 });
69 };
70
71 createEffect(on(() => threads.latest, (result) => {
72 if (result && result.items.length === 0 && page() > 0) setPage(Math.min(page() - 1, totalPages() - 1));
73 }));
74
75 return (
76 <div class="flex min-h-0 flex-1 flex-col">
77 <div class="min-h-0 flex-1 overflow-y-auto overscroll-contain pr-1">
78 <Show when={threads.latest} fallback={<PageSpinner />}>
79 {(result) => (
80 <Show
81 when={result().items.length > 0}
82 fallback={<EmptyInline class="h-full py-10" size="md" illustration="messages" title={props.labels.emptyThreads} hint={props.labels.emptyThreadsHint} />}
83 >
84 <For each={groups(result().items)}>
85 {(group) => (
86 <section class="mb-3 last:mb-0">
87 <h3 class="px-1 pb-1.5 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
88 {bucketLabel(group.bucket)}
89 </h3>
90 <ul class="space-y-1.5">
91 <For each={group.rows}>
92 {(thread) => (
93 <li
94 class={cn(
95 "flex overflow-hidden rounded-lg border transition-colors",
96 thread.id === props.activeId ? "border-primary/40 bg-primary/5" : "border-border-hairline bg-card hover:border-border-line",
97 )}
98 aria-current={thread.id === props.activeId ? "true" : undefined}
99 >
100 <button
101 type="button"
102 class="flex min-w-0 flex-1 flex-col items-start gap-0.5 px-3 py-2 text-left transition-colors hover:bg-muted/60"
103 onClick={() => props.onOpen(thread)}
104 >
105 <span class="w-full truncate text-sm font-medium">{thread.title || props.labels.untitled}</span>
106 <span class="text-[11px] tabular-nums text-muted-foreground">{formatDateTime(thread.updated_at, props.locale)}</span>
107 </button>
108 <TableRowActions
109 compact
110 label={props.labels.actions}
111 actions={[
112 { label: props.labels.rename, icon: <IconEdit class="h-4 w-4" />, onSelect: () => props.onRename(thread) },
113 { label: props.labels.delete, icon: <IconTrash class="h-4 w-4" />, destructive: true, onSelect: () => props.onRemove(thread) },
114 ]}
115 />
116 </li>
117 )}
118 </For>
119 </ul>
120 </section>
121 )}
122 </For>
123 </Show>
124 )}
125 </Show>
126 </div>
127 <Show when={totalPages() > 1}>
128 <div class="mt-3 shrink-0">
129 <PaginationControls page={page()} totalPages={totalPages()} onPageChange={setPage} />
130 </div>
131 </Show>
132 </div>
133 );
134}