hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { getRagThreads } from "@/api/rag"; |
| 4 | import type { RagThread } from "@/api/client"; |
| 5 | import { EmptyInline } from "@/components/ui/empty-inline"; |
| 6 | import { IconEdit, IconTrash } from "@/components/ui/icons"; |
| 7 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 8 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 9 | import { cn } from "@/lib/cn"; |
| 10 | import type { Locale } from "@/i18n/messages"; |
| 11 | |
| 12 | // One growing page instead of numbered pages: a chat sidebar scrolls, it does |
| 13 | // not paginate. The API caps a page at 500 rows. |
| 14 | const THREAD_PAGE_SIZE = 40; |
| 15 | const THREAD_PAGE_MAX = 500; |
| 16 | |
| 17 | export function RagThreadList(props: { |
| 18 | activeId?: string; |
| 19 | version: number; |
| 20 | locale: Locale; |
| 21 | labels: { |
| 22 | untitled: string; |
| 23 | emptyThreads: string; |
| 24 | emptyThreadsHint: string; |
| 25 | rename: string; |
| 26 | actions: string; |
| 27 | delete: string; |
| 28 | loadMore: string; |
| 29 | threadsFailed: string; |
| 30 | tryAgain: string; |
| 31 | groupToday: string; |
| 32 | groupYesterday: string; |
| 33 | groupThisWeek: string; |
| 34 | groupEarlier: string; |
| 35 | }; |
| 36 | /** Every thread the list holds, for callers that need a title by id. */ |
| 37 | onThreads?: (threads: RagThread[]) => void; |
| 38 | onOpen: (thread: RagThread) => void; |
| 39 | onRename: (thread: RagThread) => void; |
| 40 | onRemove: (thread: RagThread) => void; |
| 41 | }) { |
| 42 | const [limit, setLimit] = createSignal(THREAD_PAGE_SIZE); |
| 43 | const [threads, { refetch }] = createResource( |
| 44 | () => ({ limit: limit(), version: props.version }), |
| 45 | ({ limit }) => getRagThreads({ limit, offset: 0 }), |
| 46 | ); |
| 47 | createEffect(() => { |
| 48 | const result = threads.latest; |
| 49 | if (result) props.onThreads?.(result.items); |
| 50 | }); |
| 51 | const hasMore = () => { |
| 52 | const result = threads.latest; |
| 53 | return !!result && result.total > result.items.length && limit() < THREAD_PAGE_MAX; |
| 54 | }; |
| 55 | return ( |
| 56 | <div class="min-h-0 flex-1 overflow-y-auto overscroll-contain"> |
| 57 | {/* A failed first read used to spin forever: `latest` never arrives. */} |
| 58 | <Show when={threads.error && !threads.latest}> |
| 59 | <div class="space-y-2 px-2.5 py-6 text-center text-sm text-muted-foreground" role="alert"> |
| 60 | <p>{props.labels.threadsFailed}</p> |
| 61 | <button |
| 62 | type="button" |
| 63 | class="inline-flex h-10 items-center rounded-full border border-border-line px-3.5 text-[13px] font-medium text-foreground outline-hidden transition-colors hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring lg:h-8 touch:h-10" |
| 64 | onClick={() => void refetch()} |
| 65 | > |
| 66 | {props.labels.tryAgain} |
| 67 | </button> |
| 68 | </div> |
| 69 | </Show> |
| 70 | <Show when={threads.latest} fallback={<Show when={!threads.error}><PageSpinner /></Show>}> |
| 71 | {(result) => ( |
| 72 | <Show |
| 73 | when={result().items.length > 0} |
| 74 | fallback={<EmptyInline class="h-full py-10" size="md" illustration="messages" title={props.labels.emptyThreads} hint={props.labels.emptyThreadsHint} />} |
| 75 | > |
| 76 | <ul class="space-y-px"> |
| 77 | <For each={result().items}> |
| 78 | {(thread) => { |
| 79 | const active = () => thread.id === props.activeId; |
| 80 | return ( |
| 81 | <li |
| 82 | class={cn( |
| 83 | "group relative flex items-center rounded-lg transition-colors", |
| 84 | active() ? "bg-muted" : "hover:bg-muted/60", |
| 85 | )} |
| 86 | aria-current={active() ? "true" : undefined} |
| 87 | > |
| 88 | <button |
| 89 | type="button" |
| 90 | class={cn( |
| 91 | "min-w-0 flex-1 truncate rounded-lg py-2.5 pl-2.5 pr-10 text-left text-sm lg:py-1.5 lg:pr-8 touch:py-2.5 outline-hidden focus-visible:ring-2 focus-visible:ring-ring", |
| 92 | active() ? "font-medium text-foreground" : "text-foreground/85", |
| 93 | )} |
| 94 | title={thread.title || props.labels.untitled} |
| 95 | onClick={() => props.onOpen(thread)} |
| 96 | > |
| 97 | {thread.title || props.labels.untitled} |
| 98 | </button> |
| 99 | {/* The row menu shows on hover, on keyboard focus and on the open chat. */} |
| 100 | <div |
| 101 | class={cn( |
| 102 | "absolute right-0 top-1/2 -translate-y-1/2 transition-opacity lg:right-1 [&_[data-row-actions-trigger]]:size-10 lg:[&_[data-row-actions-trigger]]:size-7 touch:[&_[data-row-actions-trigger]]:size-10 focus-within:opacity-100 group-hover:opacity-100 [@media(pointer:coarse)]:opacity-100", |
| 103 | active() ? "opacity-100" : "opacity-0", |
| 104 | )} |
| 105 | > |
| 106 | <TableRowActions |
| 107 | compact |
| 108 | label={props.labels.actions} |
| 109 | actions={[ |
| 110 | { label: props.labels.rename, icon: <IconEdit class="h-4 w-4" />, onSelect: () => props.onRename(thread) }, |
| 111 | { label: props.labels.delete, icon: <IconTrash class="h-4 w-4" />, destructive: true, onSelect: () => props.onRemove(thread) }, |
| 112 | ]} |
| 113 | /> |
| 114 | </div> |
| 115 | </li> |
| 116 | ); |
| 117 | }} |
| 118 | </For> |
| 119 | </ul> |
| 120 | <Show when={hasMore()}> |
| 121 | <button |
| 122 | type="button" |
| 123 | class="mt-1 h-10 w-full rounded-lg px-2.5 text-left text-xs font-medium text-muted-foreground outline-hidden transition-colors hover:bg-muted/60 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50 lg:h-8 touch:h-10" |
| 124 | disabled={threads.loading} |
| 125 | onClick={() => setLimit((value) => Math.min(THREAD_PAGE_MAX, value + THREAD_PAGE_SIZE))} |
| 126 | > |
| 127 | {props.labels.loadMore} |
| 128 | </button> |
| 129 | </Show> |
| 130 | </Show> |
| 131 | )} |
| 132 | </Show> |
| 133 | </div> |
| 134 | ); |
| 135 | } |