import { For, Show, createEffect, createSignal, on } from "solid-js"; import { createResource } from "@/lib/create-resource"; import { getRagThreads } from "@/api/rag"; import type { RagThread } from "@/api/client"; import { EmptyInline } from "@/components/ui/empty-inline"; import { IconEdit, IconTrash } from "@/components/ui/icons"; import { PageSpinner } from "@/components/ui/page-spinner"; import { PaginationControls } from "@/components/ui/pagination-controls"; import { TableRowActions } from "@/components/ui/table-row-actions"; import { cn } from "@/lib/cn"; import { formatDateTime } from "@/lib/format"; import type { Locale } from "@/i18n/messages"; const THREAD_PAGE_SIZE = 12; const DAY_MS = 86_400_000; type Bucket = "today" | "yesterday" | "week" | "earlier"; const BUCKET_ORDER: Bucket[] = ["today", "yesterday", "week", "earlier"]; /** Which recency bucket a thread falls in, by local calendar day rather than a * rolling 24 hours — "dün" has to mean yesterday's date, not 25 hours ago. */ function bucketOf(updatedAt: number, now: Date): Bucket { const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); if (updatedAt >= startOfToday) return "today"; if (updatedAt >= startOfToday - DAY_MS) return "yesterday"; if (updatedAt >= startOfToday - 6 * DAY_MS) return "week"; return "earlier"; } export function RagThreadList(props: { activeId?: string; version: number; locale: Locale; labels: { untitled: string; emptyThreads: string; emptyThreadsHint: string; rename: string; actions: string; delete: string; groupToday: string; groupYesterday: string; groupThisWeek: string; groupEarlier: string; }; onOpen: (thread: RagThread) => void; onRename: (thread: RagThread) => void; onRemove: (thread: RagThread) => void; }) { const [page, setPage] = createSignal(0); const [threads] = createResource( () => ({ page: page(), version: props.version }), ({ page }) => getRagThreads({ limit: THREAD_PAGE_SIZE, offset: page * THREAD_PAGE_SIZE }), ); const totalPages = () => Math.max(1, Math.ceil((threads.latest?.total ?? 0) / THREAD_PAGE_SIZE)); const bucketLabel = (bucket: Bucket) => bucket === "today" ? props.labels.groupToday : bucket === "yesterday" ? props.labels.groupYesterday : bucket === "week" ? props.labels.groupThisWeek : props.labels.groupEarlier; // Grouping is presentational only — the page still comes from the server in // its own order, so an empty bucket simply does not get a heading. const groups = (items: RagThread[]) => { const now = new Date(); return BUCKET_ORDER.flatMap((bucket) => { const rows = items.filter((thread) => bucketOf(thread.updated_at, now) === bucket); return rows.length > 0 ? [{ bucket, rows }] : []; }); }; createEffect(on(() => threads.latest, (result) => { if (result && result.items.length === 0 && page() > 0) setPage(Math.min(page() - 1, totalPages() - 1)); })); return (