🧭 organize Celebi chat actions and search
Browse files at this commite1768cd80ff48a641361954e6ca97277f6ddaf85Parent: 14fb9ac- fix
- 🧭 organize Celebi chat actions and search
7 changed files +309 -53
- src/components/layout/celebi-markdown.test.tsx +8 -0
- src/components/layout/celebi-markdown.tsx +50 -12
- src/components/layout/celebi-panel.tsx +183 -37
- src/components/ui/data-table-search.tsx +6 -1
- src/i18n/messages.ts +5 -2
- src/lib/search-text.test.ts +14 -1
- src/lib/search-text.ts +43 -0
src/components/layout/celebi-markdown.test.tsxmodified+8 -0
| @@ -4,6 +4,14 @@ import { CelebiMarkdown } from "@/components/layout/celebi-markdown"; | |||
| 4 | test("renders bold text", () => { | 4 | test("renders bold text", () => { |
| 5 | const { container } = render(() => <CelebiMarkdown text="a **bold** word" />); | 5 | const { container } = render(() => <CelebiMarkdown text="a **bold** word" />); |
| 6 | expect(container.querySelector("strong")?.textContent).toBe("bold"); | 6 | expect(container.querySelector("strong")?.textContent).toBe("bold"); |
| 7 | + expect(container.querySelector("strong")?.className).toContain("underline"); | ||
| 8 | +}); | ||
| 9 | + | ||
| 10 | +test("keeps the typing cursor at the end of the visible answer", () => { | ||
| 11 | + const { container } = render(() => <CelebiMarkdown text={"first\n\nlast"} showCursor />); | ||
| 12 | + const cursor = container.querySelector('[aria-hidden="true"]'); | ||
| 13 | + expect(container.querySelectorAll('[aria-hidden="true"]')).toHaveLength(1); | ||
| 14 | + expect(cursor?.parentElement?.textContent).toContain("last"); | ||
| 7 | }); | 15 | }); |
| 8 | 16 | ||
| 9 | test("renders inline code", () => { | 17 | test("renders inline code", () => { |
src/components/layout/celebi-markdown.tsxmodified+50 -12
| @@ -1,4 +1,5 @@ | |||
| 1 | -import { For, Show } from "solid-js"; | 1 | +import { For, Show, type JSX } from "solid-js"; |
| 2 | +import { searchMatchRanges } from "@/lib/search-text"; | ||
| 2 | 3 | ||
| 3 | type Segment = | 4 | type Segment = |
| 4 | | { kind: "text"; value: string } | 5 | | { kind: "text"; value: string } |
| @@ -100,16 +101,37 @@ function parseBlocks(text: string): Block[] { | |||
| 100 | return blocks; | 101 | return blocks; |
| 101 | } | 102 | } |
| 102 | 103 | ||
| 103 | -function InlineSegments(props: { segments: Segment[] }) { | 104 | +function highlightedText(value: string, query?: string): JSX.Element { |
| 105 | + const ranges = searchMatchRanges(value, query ?? ""); | ||
| 106 | + if (ranges.length === 0) return <>{value}</>; | ||
| 107 | + | ||
| 108 | + const parts: JSX.Element[] = []; | ||
| 109 | + let cursor = 0; | ||
| 110 | + for (const range of ranges) { | ||
| 111 | + if (range.start > cursor) parts.push(<>{value.slice(cursor, range.start)}</>); | ||
| 112 | + parts.push( | ||
| 113 | + <span class="rounded-sm bg-muted/75 px-0.5 text-foreground line-through decoration-muted-foreground/65 decoration-2" data-search-match="true"> | ||
| 114 | + {value.slice(range.start, range.end)} | ||
| 115 | + </span>, | ||
| 116 | + ); | ||
| 117 | + cursor = range.end; | ||
| 118 | + } | ||
| 119 | + if (cursor < value.length) parts.push(<>{value.slice(cursor)}</>); | ||
| 120 | + return <>{parts}</>; | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +function InlineSegments(props: { segments: Segment[]; searchQuery?: string }) { | ||
| 104 | return ( | 124 | return ( |
| 105 | <For each={props.segments}> | 125 | <For each={props.segments}> |
| 106 | {(segment) => ( | 126 | {(segment) => ( |
| 107 | - <Show when={segment.kind !== "text"} fallback={<>{(segment as { value: string }).value}</>}> | 127 | + <Show when={segment.kind !== "text"} fallback={highlightedText((segment as { value: string }).value, props.searchQuery)}> |
| 108 | <Show when={segment.kind === "bold"}> | 128 | <Show when={segment.kind === "bold"}> |
| 109 | - <strong>{(segment as { value: string }).value}</strong> | 129 | + <strong class="font-semibold text-primary-text underline decoration-primary/45 decoration-2 underline-offset-2"> |
| 130 | + {highlightedText((segment as { value: string }).value, props.searchQuery)} | ||
| 131 | + </strong> | ||
| 110 | </Show> | 132 | </Show> |
| 111 | <Show when={segment.kind === "code"}> | 133 | <Show when={segment.kind === "code"}> |
| 112 | - <code class="rounded bg-muted px-1 py-0.5 font-mono text-[0.85em]">{(segment as { value: string }).value}</code> | 134 | + <code class="rounded bg-muted px-1 py-0.5 font-mono text-[0.85em]">{highlightedText((segment as { value: string }).value, props.searchQuery)}</code> |
| 113 | </Show> | 135 | </Show> |
| 114 | <Show when={segment.kind === "link"}> | 136 | <Show when={segment.kind === "link"}> |
| 115 | <a | 137 | <a |
| @@ -118,7 +140,7 @@ function InlineSegments(props: { segments: Segment[] }) { | |||
| 118 | rel="noreferrer" | 140 | rel="noreferrer" |
| 119 | class="underline underline-offset-2 hover:text-primary-text" | 141 | class="underline underline-offset-2 hover:text-primary-text" |
| 120 | > | 142 | > |
| 121 | - {(segment as { label: string }).label} | 143 | + {highlightedText((segment as { label: string }).label, props.searchQuery)} |
| 122 | </a> | 144 | </a> |
| 123 | </Show> | 145 | </Show> |
| 124 | </Show> | 146 | </Show> |
| @@ -127,30 +149,46 @@ function InlineSegments(props: { segments: Segment[] }) { | |||
| 127 | ); | 149 | ); |
| 128 | } | 150 | } |
| 129 | 151 | ||
| 130 | -export function CelebiMarkdown(props: { text: string }) { | 152 | +export function CelebiMarkdown(props: { text: string; showCursor?: boolean; searchQuery?: string }) { |
| 131 | const blocks = () => parseBlocks(props.text); | 153 | const blocks = () => parseBlocks(props.text); |
| 132 | return ( | 154 | return ( |
| 133 | <div class="space-y-2"> | 155 | <div class="space-y-2"> |
| 134 | <For each={blocks()}> | 156 | <For each={blocks()}> |
| 135 | - {(block) => ( | 157 | + {(block, index) => { |
| 158 | + const lastBlock = () => index() === blocks().length - 1; | ||
| 159 | + return ( | ||
| 136 | <Show when={block.kind === "paragraph"} fallback={ | 160 | <Show when={block.kind === "paragraph"} fallback={ |
| 137 | <Show when={block.kind === "code-block"} fallback={ | 161 | <Show when={block.kind === "code-block"} fallback={ |
| 138 | <ul class="list-disc space-y-1 pl-5"> | 162 | <ul class="list-disc space-y-1 pl-5"> |
| 139 | <For each={(block as { items: Segment[][] }).items}> | 163 | <For each={(block as { items: Segment[][] }).items}> |
| 140 | - {(item) => <li><InlineSegments segments={item} /></li>} | 164 | + {(item, itemIndex) => ( |
| 165 | + <li> | ||
| 166 | + <InlineSegments segments={item} searchQuery={props.searchQuery} /> | ||
| 167 | + <Show when={props.showCursor && lastBlock() && itemIndex() === (block as { items: Segment[][] }).items.length - 1}> | ||
| 168 | + <span class="ml-1 inline-block h-3.5 w-0.5 animate-pulse rounded-full bg-primary align-middle" aria-hidden="true" /> | ||
| 169 | + </Show> | ||
| 170 | + </li> | ||
| 171 | + )} | ||
| 141 | </For> | 172 | </For> |
| 142 | </ul> | 173 | </ul> |
| 143 | }> | 174 | }> |
| 144 | <pre class="overflow-x-auto rounded-lg bg-muted p-2.5 text-xs"> | 175 | <pre class="overflow-x-auto rounded-lg bg-muted p-2.5 text-xs"> |
| 145 | - <code>{(block as { value: string }).value}</code> | 176 | + <code>{highlightedText((block as { value: string }).value, props.searchQuery)}</code> |
| 177 | + <Show when={props.showCursor && lastBlock()}> | ||
| 178 | + <span class="ml-1 inline-block h-3.5 w-0.5 animate-pulse rounded-full bg-primary align-middle" aria-hidden="true" /> | ||
| 179 | + </Show> | ||
| 146 | </pre> | 180 | </pre> |
| 147 | </Show> | 181 | </Show> |
| 148 | }> | 182 | }> |
| 149 | <p class="whitespace-pre-wrap leading-6"> | 183 | <p class="whitespace-pre-wrap leading-6"> |
| 150 | - <InlineSegments segments={(block as { segments: Segment[] }).segments} /> | 184 | + <InlineSegments segments={(block as { segments: Segment[] }).segments} searchQuery={props.searchQuery} /> |
| 185 | + <Show when={props.showCursor && lastBlock()}> | ||
| 186 | + <span class="ml-1 inline-block h-3.5 w-0.5 animate-pulse rounded-full bg-primary align-middle" aria-hidden="true" /> | ||
| 187 | + </Show> | ||
| 151 | </p> | 188 | </p> |
| 152 | </Show> | 189 | </Show> |
| 153 | - )} | 190 | + ); |
| 191 | + }} | ||
| 154 | </For> | 192 | </For> |
| 155 | </div> | 193 | </div> |
| 156 | ); | 194 | ); |
src/components/layout/celebi-panel.tsxmodified+183 -37
| @@ -1,4 +1,4 @@ | |||
| 1 | -import { For, Match, Show, Switch, createEffect, createSignal, on, onCleanup, untrack } from "solid-js"; | 1 | +import { For, Match, Show, Suspense, Switch, createEffect, createSignal, on, onCleanup, untrack, type JSX } from "solid-js"; |
| 2 | import { useNavigate } from "@tanstack/solid-router"; | 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 3 | import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; | 3 | import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; |
| 4 | import { formatApiError } from "@/api/client"; | 4 | import { formatApiError } from "@/api/client"; |
| @@ -10,10 +10,11 @@ import { CelebiThreadList } from "@/components/layout/celebi-thread-list"; | |||
| 10 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | 10 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 11 | import { DataTableSearch } from "@/components/ui/data-table-search"; | 11 | import { DataTableSearch } from "@/components/ui/data-table-search"; |
| 12 | import { EmptyInline } from "@/components/ui/empty-inline"; | 12 | import { EmptyInline } from "@/components/ui/empty-inline"; |
| 13 | -import { IconAlert, IconBotSquare, IconChevronDown, IconChevronLeft, IconCopy, IconEdit, IconMessage, IconPlus } from "@/components/ui/icons"; | 13 | +import { IconAlert, IconBotSquare, IconChevronDown, IconChevronLeft, IconChevronUp, IconCopy, IconEdit, IconMessage, IconPlus, IconSearch, IconX } from "@/components/ui/icons"; |
| 14 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 14 | import { SidePanel } from "@/components/ui/side-panel"; | 15 | import { SidePanel } from "@/components/ui/side-panel"; |
| 15 | import { cn } from "@/lib/cn"; | 16 | import { cn } from "@/lib/cn"; |
| 16 | -import { matchesSearch } from "@/lib/search-text"; | 17 | +import { matchesSearch, searchMatchRanges } from "@/lib/search-text"; |
| 17 | import { usePreferences, useT } from "@/stores/preferences-context"; | 18 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 18 | 19 | ||
| 19 | type PanelMessage = Pick<ChatbotMessage, "id" | "role" | "status" | "content" | "truncated" | "error_code" | "navigation" | "suggestions">; | 20 | type PanelMessage = Pick<ChatbotMessage, "id" | "role" | "status" | "content" | "truncated" | "error_code" | "navigation" | "suggestions">; |
| @@ -42,6 +43,8 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 42 | const [sending, setSending] = createSignal(false); | 43 | const [sending, setSending] = createSignal(false); |
| 43 | const [copiedId, setCopiedId] = createSignal<string>(); | 44 | const [copiedId, setCopiedId] = createSignal<string>(); |
| 44 | const [searchQuery, setSearchQuery] = createSignal(""); | 45 | const [searchQuery, setSearchQuery] = createSignal(""); |
| 46 | + const [searchOpen, setSearchOpen] = createSignal(false); | ||
| 47 | + const [searchMatchIndex, setSearchMatchIndex] = createSignal(0); | ||
| 45 | 48 | ||
| 46 | const copyMessage = async (message: PanelMessage) => { | 49 | const copyMessage = async (message: PanelMessage) => { |
| 47 | await navigator.clipboard.writeText(message.content); | 50 | await navigator.clipboard.writeText(message.content); |
| @@ -134,6 +137,66 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 134 | if (!query) return messages(); | 137 | if (!query) return messages(); |
| 135 | return messages().filter((message) => matchesSearch(query, message.content)); | 138 | return messages().filter((message) => matchesSearch(query, message.content)); |
| 136 | }; | 139 | }; |
| 140 | + const updateSearchQuery = (value: string) => { | ||
| 141 | + setSearchQuery(value); | ||
| 142 | + setSearchMatchIndex(0); | ||
| 143 | + }; | ||
| 144 | + const searchHits = () => { | ||
| 145 | + const query = searchQuery().trim(); | ||
| 146 | + if (!query) return [] as Array<{ messageId: string }>; | ||
| 147 | + return messages().flatMap((message) => | ||
| 148 | + searchMatchRanges(message.content, query).map(() => ({ messageId: message.id })), | ||
| 149 | + ); | ||
| 150 | + }; | ||
| 151 | + const activeSearchIndex = () => { | ||
| 152 | + const total = searchHits().length; | ||
| 153 | + return total > 0 ? Math.min(searchMatchIndex(), total - 1) : 0; | ||
| 154 | + }; | ||
| 155 | + const activeSearchHit = () => searchHits()[activeSearchIndex()]; | ||
| 156 | + const scrollToSearchHit = (hit: { messageId: string } | undefined) => { | ||
| 157 | + if (!hit || !transcript) return; | ||
| 158 | + const messageNode = Array.from(transcript.querySelectorAll<HTMLElement>("[data-search-message-id]")) | ||
| 159 | + .find((node) => node.dataset.searchMessageId === hit.messageId); | ||
| 160 | + messageNode?.scrollIntoView({ behavior: "smooth", block: "center" }); | ||
| 161 | + }; | ||
| 162 | + const moveSearchMatch = (direction: -1 | 1) => { | ||
| 163 | + const total = searchHits().length; | ||
| 164 | + if (total === 0) return; | ||
| 165 | + const next = (activeSearchIndex() + direction + total) % total; | ||
| 166 | + setSearchMatchIndex(next); | ||
| 167 | + requestAnimationFrame(() => scrollToSearchHit(searchHits()[next])); | ||
| 168 | + }; | ||
| 169 | + const handleSearchKeyDown = (event: KeyboardEvent) => { | ||
| 170 | + if (event.key === "ArrowUp") { | ||
| 171 | + event.preventDefault(); | ||
| 172 | + moveSearchMatch(-1); | ||
| 173 | + } else if (event.key === "ArrowDown") { | ||
| 174 | + event.preventDefault(); | ||
| 175 | + moveSearchMatch(1); | ||
| 176 | + } | ||
| 177 | + }; | ||
| 178 | + const highlightedMessageText = (value: string, query: string): JSX.Element => { | ||
| 179 | + const ranges = searchMatchRanges(value, query); | ||
| 180 | + if (ranges.length === 0) return <>{value}</>; | ||
| 181 | + const parts: JSX.Element[] = []; | ||
| 182 | + let cursor = 0; | ||
| 183 | + for (const range of ranges) { | ||
| 184 | + if (range.start > cursor) parts.push(<>{value.slice(cursor, range.start)}</>); | ||
| 185 | + parts.push( | ||
| 186 | + <span class="rounded-sm bg-muted/75 px-0.5 text-foreground line-through decoration-muted-foreground/65 decoration-2" data-search-match="true"> | ||
| 187 | + {value.slice(range.start, range.end)} | ||
| 188 | + </span>, | ||
| 189 | + ); | ||
| 190 | + cursor = range.end; | ||
| 191 | + } | ||
| 192 | + if (cursor < value.length) parts.push(<>{value.slice(cursor)}</>); | ||
| 193 | + return <>{parts}</>; | ||
| 194 | + }; | ||
| 195 | + createEffect(() => { | ||
| 196 | + const hit = activeSearchHit(); | ||
| 197 | + if (!hit) return; | ||
| 198 | + requestAnimationFrame(() => scrollToSearchHit(hit)); | ||
| 199 | + }); | ||
| 137 | createEffect(() => { | 200 | createEffect(() => { |
| 138 | const items = messages(); | 201 | const items = messages(); |
| 139 | typedCount(); | 202 | typedCount(); |
| @@ -176,12 +239,19 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 176 | if (match && threadId() === active) setThreadTitle(match.title); | 239 | if (match && threadId() === active) setThreadTitle(match.title); |
| 177 | } catch { /* history is non-blocking */ } | 240 | } catch { /* history is non-blocking */ } |
| 178 | }; | 241 | }; |
| 179 | - const openThread = async (thread: ChatbotThread) => { stopPolling(); stopStream(); stopReveal(); setPinned(true); setUnread(false); setView("chat"); setSearchQuery(""); setThreadId(thread.id); setThreadTitle(thread.title); setMessages((await getChatbotThreadMessages(thread.id, { limit: 500 })).items); }; | 242 | + const openThread = async (thread: ChatbotThread) => { stopPolling(); stopStream(); stopReveal(); setPinned(true); setUnread(false); setView("chat"); setSearchOpen(false); updateSearchQuery(""); setThreadId(thread.id); setThreadTitle(thread.title); setMessages((await getChatbotThreadMessages(thread.id, { limit: 500 })).items); }; |
| 180 | - const createThread = () => { stopPolling(); stopStream(); stopReveal(); setSearchQuery(""); setThreadId(undefined); setThreadTitle(null); setMessages([]); }; | 243 | + const createThread = () => { stopPolling(); stopStream(); stopReveal(); setSearchOpen(false); updateSearchQuery(""); setThreadId(undefined); setThreadTitle(null); setMessages([]); }; |
| 244 | + const startNewChat = () => { createThread(); setView("chat"); }; | ||
| 245 | + const openThreads = () => { setSearchOpen(false); setSearchQuery(""); setView("threads"); }; | ||
| 181 | // Rename and delete go through the shared confirm dialog, never the | 246 | // Rename and delete go through the shared confirm dialog, never the |
| 182 | // browser's own prompt()/confirm() boxes. | 247 | // browser's own prompt()/confirm() boxes. |
| 183 | const [renaming, setRenaming] = createSignal<ChatbotThread | null>(null); | 248 | const [renaming, setRenaming] = createSignal<ChatbotThread | null>(null); |
| 184 | const [removing, setRemoving] = createSignal<ChatbotThread | null>(null); | 249 | const [removing, setRemoving] = createSignal<ChatbotThread | null>(null); |
| 250 | + const renameActiveThread = () => { | ||
| 251 | + const activeId = threadId(); | ||
| 252 | + if (!activeId) return; | ||
| 253 | + setRenaming({ id: activeId, title: threadTitle(), created_at: 0, updated_at: 0 }); | ||
| 254 | + }; | ||
| 185 | const renameThread = async (thread: ChatbotThread, title: string | undefined) => { | 255 | const renameThread = async (thread: ChatbotThread, title: string | undefined) => { |
| 186 | const updated = await patchChatbotThreadById(thread.id, { title: title?.trim() || null }); | 256 | const updated = await patchChatbotThreadById(thread.id, { title: title?.trim() || null }); |
| 187 | if (threadId() === thread.id) setThreadTitle(updated.title); | 257 | if (threadId() === thread.id) setThreadTitle(updated.title); |
| @@ -282,49 +352,120 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 282 | return ( | 352 | return ( |
| 283 | <SidePanel open={props.open} onOpenChange={props.onOpenChange} title={t("ai.title")} description={t("ai.description")} bodyClass="overflow-hidden pb-0"> | 353 | <SidePanel open={props.open} onOpenChange={props.onOpenChange} title={t("ai.title")} description={t("ai.description")} bodyClass="overflow-hidden pb-0"> |
| 284 | <div class="flex h-full min-h-0 flex-col"> | 354 | <div class="flex h-full min-h-0 flex-col"> |
| 285 | - <div class="mb-3 flex shrink-0 items-center gap-2 border-b border-border pb-3"> | 355 | + <div class="mb-3 shrink-0 border-b border-border pb-3"> |
| 286 | <Show | 356 | <Show |
| 287 | when={view() === "chat"} | 357 | when={view() === "chat"} |
| 288 | fallback={ | 358 | fallback={ |
| 289 | - <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={() => setView("chat")}> | 359 | + <div class="flex min-w-0 items-center gap-2"> |
| 290 | - <IconChevronLeft class="h-3.5 w-3.5" /> | 360 | + <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={() => setView("chat")}> |
| 291 | - {t("common.back")} | 361 | + <IconChevronLeft class="h-3.5 w-3.5" /> |
| 292 | - </button> | 362 | + {t("common.back")} |
| 363 | + </button> | ||
| 364 | + <span class="min-w-0 flex-1 truncate text-sm font-medium text-foreground">{t("ai.chats")}</span> | ||
| 365 | + <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={(event) => { event.preventDefault(); startNewChat(); }}> | ||
| 366 | + <IconPlus class="h-3.5 w-3.5" /> | ||
| 367 | + {t("ai.newChat")} | ||
| 368 | + </button> | ||
| 369 | + </div> | ||
| 293 | } | 370 | } |
| 294 | > | 371 | > |
| 295 | - <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={createThread}> | 372 | + <div class="flex min-w-0 items-center justify-between gap-3"> |
| 296 | - <IconPlus class="h-3.5 w-3.5" /> | 373 | + <div class="flex shrink-0 items-center gap-1.5"> |
| 297 | - {t("ai.newChat")} | 374 | + <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={(event) => { event.preventDefault(); startNewChat(); }}> |
| 298 | - </button> | 375 | + <IconPlus class="h-3.5 w-3.5" /> |
| 299 | - </Show> | 376 | + {t("ai.newChat")} |
| 300 | - <span class="min-w-0 flex-1 truncate text-sm font-medium text-foreground"> | 377 | + </button> |
| 301 | - {view() === "threads" ? t("ai.chats") : threadId() ? threadTitle() || t("ai.untitledChat") : t("ai.newChat")} | 378 | + <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={(event) => { event.preventDefault(); openThreads(); }}> |
| 302 | - </span> | 379 | + <IconMessage class="h-3.5 w-3.5" /> |
| 303 | - <Show when={view() === "chat"}> | 380 | + {t("ai.chats")} |
| 304 | - <button type="button" class="flex h-8 shrink-0 items-center gap-1.5 rounded-lg border border-border bg-card px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-accent" onClick={() => setView("threads")}> | 381 | + </button> |
| 305 | - <IconMessage class="h-3.5 w-3.5" /> | 382 | + </div> |
| 306 | - {t("ai.chats")} | 383 | + <Show when={threadId()}> |
| 307 | - </button> | 384 | + <span class="min-w-0 flex-1 truncate text-sm font-medium text-foreground">{threadTitle() || t("ai.untitledChat")}</span> |
| 385 | + <button | ||
| 386 | + type="button" | ||
| 387 | + class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg border border-border bg-card text-muted-foreground transition-colors hover:bg-accent hover:text-foreground" | ||
| 388 | + aria-label={t("ai.renameChat")} | ||
| 389 | + title={t("ai.renameChat")} | ||
| 390 | + onClick={(event) => { event.preventDefault(); renameActiveThread(); }} | ||
| 391 | + > | ||
| 392 | + <IconEdit class="h-3.5 w-3.5" /> | ||
| 393 | + </button> | ||
| 394 | + </Show> | ||
| 395 | + <div class="flex shrink-0 items-center"> | ||
| 396 | + <Show | ||
| 397 | + when={!searchOpen()} | ||
| 398 | + fallback={ | ||
| 399 | + <button type="button" class="flex h-8 w-8 items-center justify-center rounded-lg border border-border bg-card text-xs font-medium text-foreground transition-colors hover:bg-accent" aria-label={t("common.close")} title={t("common.close")} onClick={() => { setSearchOpen(false); updateSearchQuery(""); }}> | ||
| 400 | + <IconX class="h-3.5 w-3.5" /> | ||
| 401 | + </button> | ||
| 402 | + } | ||
| 403 | + > | ||
| 404 | + <button type="button" class="flex h-8 w-8 items-center justify-center rounded-lg border border-border bg-card text-xs font-medium text-foreground transition-colors hover:bg-accent" aria-label={t("common.search")} title={t("common.search")} aria-expanded={searchOpen()} onClick={(event) => { event.preventDefault(); setSearchOpen(true); }}> | ||
| 405 | + <IconSearch class="h-3.5 w-3.5" /> | ||
| 406 | + </button> | ||
| 407 | + </Show> | ||
| 408 | + </div> | ||
| 409 | + </div> | ||
| 308 | </Show> | 410 | </Show> |
| 309 | </div> | 411 | </div> |
| 310 | <Show when={view() === "threads"}> | 412 | <Show when={view() === "threads"}> |
| 311 | <div class="min-h-0 flex-1"> | 413 | <div class="min-h-0 flex-1"> |
| 312 | - <CelebiThreadList | 414 | + <Suspense fallback={<PageSpinner />}> |
| 313 | - activeId={threadId()} | 415 | + <CelebiThreadList |
| 314 | - version={threadsVersion()} | 416 | + activeId={threadId()} |
| 315 | - onOpen={(thread) => void openThread(thread)} | 417 | + version={threadsVersion()} |
| 316 | - onRename={setRenaming} | 418 | + onOpen={(thread) => void openThread(thread)} |
| 317 | - onRemove={setRemoving} | 419 | + onRename={setRenaming} |
| 318 | - /> | 420 | + onRemove={setRemoving} |
| 421 | + /> | ||
| 422 | + </Suspense> | ||
| 319 | </div> | 423 | </div> |
| 320 | </Show> | 424 | </Show> |
| 321 | {/* Hidden rather than unmounted while the history is open, so going | 425 | {/* Hidden rather than unmounted while the history is open, so going |
| 322 | back lands on the same scroll position and a streaming answer | 426 | back lands on the same scroll position and a streaming answer |
| 323 | keeps writing. */} | 427 | keeps writing. */} |
| 324 | <div class={cn("flex min-h-0 flex-1 flex-col", view() !== "chat" && "hidden")}> | 428 | <div class={cn("flex min-h-0 flex-1 flex-col", view() !== "chat" && "hidden")}> |
| 325 | - <div class="mb-3 shrink-0"> | 429 | + <Show when={searchOpen()}> |
| 326 | - <DataTableSearch value={searchQuery()} onChange={setSearchQuery} placeholder={t("ai.searchPlaceholder")} hint={t("search.hint.chat")} class="w-full" /> | 430 | + <div class="mb-3 flex shrink-0 items-start gap-2"> |
| 327 | - </div> | 431 | + <DataTableSearch |
| 432 | + value={searchQuery()} | ||
| 433 | + onChange={updateSearchQuery} | ||
| 434 | + onKeyDown={handleSearchKeyDown} | ||
| 435 | + placeholder={t("ai.searchPlaceholder")} | ||
| 436 | + hint={t("search.hint.chat")} | ||
| 437 | + hintClass="w-full min-w-0 max-w-none border-primary/40 bg-card px-3 py-2 text-xs font-medium text-foreground shadow-lg" | ||
| 438 | + class="min-w-0 flex-1 sm:max-w-none" | ||
| 439 | + /> | ||
| 440 | + <Show when={searchQuery().trim()}> | ||
| 441 | + <div class="flex h-8 shrink-0 items-center gap-0.5 rounded-lg border border-border bg-card px-1 shadow-sm"> | ||
| 442 | + <span class="px-1.5 text-[11px] tabular-nums text-muted-foreground" aria-live="polite"> | ||
| 443 | + {t("ai.searchCount", { current: searchHits().length > 0 ? activeSearchIndex() + 1 : 0, total: searchHits().length })} | ||
| 444 | + </span> | ||
| 445 | + <button | ||
| 446 | + type="button" | ||
| 447 | + class="flex h-6 w-6 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground disabled:pointer-events-none disabled:opacity-40" | ||
| 448 | + aria-label={t("common.prev")} | ||
| 449 | + title={t("common.prev")} | ||
| 450 | + disabled={searchHits().length === 0} | ||
| 451 | + onClick={() => moveSearchMatch(-1)} | ||
| 452 | + > | ||
| 453 | + <IconChevronUp class="h-3.5 w-3.5" /> | ||
| 454 | + </button> | ||
| 455 | + <button | ||
| 456 | + type="button" | ||
| 457 | + class="flex h-6 w-6 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground disabled:pointer-events-none disabled:opacity-40" | ||
| 458 | + aria-label={t("common.next")} | ||
| 459 | + title={t("common.next")} | ||
| 460 | + disabled={searchHits().length === 0} | ||
| 461 | + onClick={() => moveSearchMatch(1)} | ||
| 462 | + > | ||
| 463 | + <IconChevronDown class="h-3.5 w-3.5" /> | ||
| 464 | + </button> | ||
| 465 | + </div> | ||
| 466 | + </Show> | ||
| 467 | + </div> | ||
| 468 | + </Show> | ||
| 328 | {/* The jump control floats over the bottom of the transcript, next to | 469 | {/* The jump control floats over the bottom of the transcript, next to |
| 329 | the newest message it leads to, instead of sitting up by search. */} | 470 | the newest message it leads to, instead of sitting up by search. */} |
| 330 | <div class="relative flex min-h-0 flex-1 flex-col"> | 471 | <div class="relative flex min-h-0 flex-1 flex-col"> |
| @@ -342,10 +483,16 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 342 | <div class="flex flex-col gap-3"> | 483 | <div class="flex flex-col gap-3"> |
| 343 | <For each={filteredMessages()}> | 484 | <For each={filteredMessages()}> |
| 344 | {(message) => ( | 485 | {(message) => ( |
| 345 | - <div class={message.role === "user" ? "ml-8 rounded-lg rounded-br-sm bg-primary px-3.5 py-2.5 text-sm text-primary-foreground selection:bg-primary-foreground selection:text-primary-text" : "mr-6 rounded-lg rounded-bl-sm border border-border bg-card px-3.5 py-2.5 text-sm text-foreground shadow-sm"}> | 486 | + <div |
| 487 | + data-search-message-id={message.id} | ||
| 488 | + class={cn( | ||
| 489 | + message.role === "user" ? "ml-8 rounded-lg rounded-br-sm bg-primary px-3.5 py-2.5 text-sm text-primary-foreground selection:bg-primary-foreground selection:text-primary-text" : "mr-6 rounded-lg rounded-bl-sm border border-border bg-card px-3.5 py-2.5 text-sm text-foreground shadow-sm", | ||
| 490 | + activeSearchHit()?.messageId === message.id && "ring-2 ring-primary/35", | ||
| 491 | + )} | ||
| 492 | + > | ||
| 346 | <Switch> | 493 | <Switch> |
| 347 | <Match when={message.role === "user"}> | 494 | <Match when={message.role === "user"}> |
| 348 | - <p class="whitespace-pre-wrap leading-6">{message.content}</p> | 495 | + <p class="whitespace-pre-wrap leading-6">{highlightedMessageText(message.content, searchQuery())}</p> |
| 349 | </Match> | 496 | </Match> |
| 350 | <Match when={message.status === "failed" && !message.content}> | 497 | <Match when={message.status === "failed" && !message.content}> |
| 351 | <p class="whitespace-pre-wrap leading-6">{failureMessage(message.error_code)}</p> | 498 | <p class="whitespace-pre-wrap leading-6">{failureMessage(message.error_code)}</p> |
| @@ -356,9 +503,8 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | |||
| 356 | <span class="flex items-center gap-2 text-muted-foreground"><IconBotSquare class="h-4 w-4 text-primary-text" /><span class="animate-pulse"><CelebiThinkingLabel /></span></span> | 503 | <span class="flex items-center gap-2 text-muted-foreground"><IconBotSquare class="h-4 w-4 text-primary-text" /><span class="animate-pulse"><CelebiThinkingLabel /></span></span> |
| 357 | </Match> | 504 | </Match> |
| 358 | <Match when={true}> | 505 | <Match when={true}> |
| 359 | - <CelebiMarkdown text={visibleContent(message)} /> | 506 | + <CelebiMarkdown text={visibleContent(message)} showCursor={isRevealing(message)} searchQuery={searchQuery()} /> |
| 360 | <Show when={isRevealing(message)}> | 507 | <Show when={isRevealing(message)}> |
| 361 | - <span class="ml-1 inline-block h-3.5 w-0.5 animate-pulse rounded-full bg-primary align-middle" aria-hidden="true" /> | ||
| 362 | <button | 508 | <button |
| 363 | type="button" | 509 | type="button" |
| 364 | class="mt-2 block text-xs text-muted-foreground underline-offset-2 hover:text-foreground hover:underline" | 510 | class="mt-2 block text-xs text-muted-foreground underline-offset-2 hover:text-foreground hover:underline" |
src/components/ui/data-table-search.tsxmodified+6 -1
| @@ -15,6 +15,9 @@ export type DataTableSearchProps = { | |||
| 15 | * honest: it must list what the filter really reads, nothing more. | 15 | * honest: it must list what the filter really reads, nothing more. |
| 16 | */ | 16 | */ |
| 17 | hint?: string; | 17 | hint?: string; |
| 18 | + /** Optional styling for the focused hint, e.g. a wider panel search hint. */ | ||
| 19 | + hintClass?: string; | ||
| 20 | + onKeyDown?: (event: KeyboardEvent) => void; | ||
| 18 | class?: string; | 21 | class?: string; |
| 19 | }; | 22 | }; |
| 20 | 23 | ||
| @@ -37,6 +40,8 @@ export function DataTableSearch(props: DataTableSearchProps) { | |||
| 37 | // Escape empties the box instead of only blurring it: a filter the | 40 | // Escape empties the box instead of only blurring it: a filter the |
| 38 | // keyboard cannot undo leaves the list silently narrowed. | 41 | // keyboard cannot undo leaves the list silently narrowed. |
| 39 | onKeyDown={(event: KeyboardEvent) => { | 42 | onKeyDown={(event: KeyboardEvent) => { |
| 43 | + props.onKeyDown?.(event); | ||
| 44 | + if (event.defaultPrevented) return; | ||
| 40 | if (event.key !== "Escape" || !props.value) return; | 45 | if (event.key !== "Escape" || !props.value) return; |
| 41 | event.preventDefault(); | 46 | event.preventDefault(); |
| 42 | event.stopPropagation(); | 47 | event.stopPropagation(); |
| @@ -61,7 +66,7 @@ export function DataTableSearch(props: DataTableSearchProps) { | |||
| 61 | <Show when={props.hint && focused()}> | 66 | <Show when={props.hint && focused()}> |
| 62 | <p | 67 | <p |
| 63 | id={hintId} | 68 | id={hintId} |
| 64 | - class="absolute left-0 top-full z-30 mt-1 w-full min-w-max max-w-[22rem] rounded-lg border border-border/80 bg-popover px-2.5 py-1.5 text-[11px] leading-snug text-muted-foreground shadow-md" | 69 | + class={cn("absolute left-0 top-full z-30 mt-1 w-full min-w-max max-w-[22rem] rounded-lg border border-border/80 bg-popover px-2.5 py-1.5 text-[11px] leading-snug text-muted-foreground shadow-md", props.hintClass)} |
| 65 | > | 70 | > |
| 66 | {props.hint} | 71 | {props.hint} |
| 67 | </p> | 72 | </p> |
src/i18n/messages.tsmodified+5 -2
| @@ -31,6 +31,7 @@ export type MessageKey = | |||
| 31 | | "ai.newChat" | 31 | | "ai.newChat" |
| 32 | | "ai.chats" | 32 | | "ai.chats" |
| 33 | | "ai.noChats" | 33 | | "ai.noChats" |
| 34 | + | "ai.searchCount" | ||
| 34 | | "ai.noSearchResults" | 35 | | "ai.noSearchResults" |
| 35 | | "aiHub.description" | 36 | | "aiHub.description" |
| 36 | | "aiHub.tab.studio" | 37 | | "aiHub.tab.studio" |
| @@ -2196,7 +2197,7 @@ const en: Dict = { | |||
| 2196 | "ai.thinking1": "Çelebi is thinking…", | 2197 | "ai.thinking1": "Çelebi is thinking…", |
| 2197 | "ai.thinking2": "Looking into it…", | 2198 | "ai.thinking2": "Looking into it…", |
| 2198 | "ai.thinking3": "Almost there…", | 2199 | "ai.thinking3": "Almost there…", |
| 2199 | - "ai.hint": "Enter to send · Shift+Enter for a new line · AI-generated", | 2200 | + "ai.hint": "Enter to send · Shift+Enter for a new line · Çelebi AI", |
| 2200 | "ai.goTo": "Go to the page", | 2201 | "ai.goTo": "Go to the page", |
| 2201 | "ai.didYouMean": "Did you mean one of these?", | 2202 | "ai.didYouMean": "Did you mean one of these?", |
| 2202 | "ai.skipTyping": "Show the whole answer", | 2203 | "ai.skipTyping": "Show the whole answer", |
| @@ -2206,6 +2207,7 @@ const en: Dict = { | |||
| 2206 | "ai.newChat": "New chat", | 2207 | "ai.newChat": "New chat", |
| 2207 | "ai.chats": "Chats", | 2208 | "ai.chats": "Chats", |
| 2208 | "ai.noChats": "No chats yet. Start one and it will appear here.", | 2209 | "ai.noChats": "No chats yet. Start one and it will appear here.", |
| 2210 | + "ai.searchCount": "{current} / {total}", | ||
| 2209 | "ai.noSearchResults": "No messages match your search.", | 2211 | "ai.noSearchResults": "No messages match your search.", |
| 2210 | "aiHub.description": "Every AI surface in one place: note outputs and podcasts, student analysis, and Çelebi.", | 2212 | "aiHub.description": "Every AI surface in one place: note outputs and podcasts, student analysis, and Çelebi.", |
| 2211 | "aiHub.tab.studio": "Note studio", | 2213 | "aiHub.tab.studio": "Note studio", |
| @@ -4386,7 +4388,7 @@ const tr: Dict = { | |||
| 4386 | "ai.thinking1": "Çelebi düşünüyor…", | 4388 | "ai.thinking1": "Çelebi düşünüyor…", |
| 4387 | "ai.thinking2": "Araştırıyor…", | 4389 | "ai.thinking2": "Araştırıyor…", |
| 4388 | "ai.thinking3": "Neredeyse hazır…", | 4390 | "ai.thinking3": "Neredeyse hazır…", |
| 4389 | - "ai.hint": "Enter gönder · Shift+Enter alt satır · Yapay zekâ üretimi", | 4391 | + "ai.hint": "Enter gönder · Shift+Enter alt satır · Çelebi AI", |
| 4390 | "ai.goTo": "Sayfaya git", | 4392 | "ai.goTo": "Sayfaya git", |
| 4391 | "ai.didYouMean": "Bunu mu demek istedin?", | 4393 | "ai.didYouMean": "Bunu mu demek istedin?", |
| 4392 | "ai.skipTyping": "Cevabın tamamını göster", | 4394 | "ai.skipTyping": "Cevabın tamamını göster", |
| @@ -4396,6 +4398,7 @@ const tr: Dict = { | |||
| 4396 | "ai.newChat": "Yeni sohbet", | 4398 | "ai.newChat": "Yeni sohbet", |
| 4397 | "ai.chats": "Sohbetler", | 4399 | "ai.chats": "Sohbetler", |
| 4398 | "ai.noChats": "Henüz sohbet yok. Başlattığın sohbetler burada görünür.", | 4400 | "ai.noChats": "Henüz sohbet yok. Başlattığın sohbetler burada görünür.", |
| 4401 | + "ai.searchCount": "{current} / {total}", | ||
| 4399 | "ai.noSearchResults": "Aramanla eşleşen mesaj yok.", | 4402 | "ai.noSearchResults": "Aramanla eşleşen mesaj yok.", |
| 4400 | "aiHub.description": "Tüm yapay zekâ araçları tek yerde: not çıktıları ve podcast, öğrenci analizi ve Çelebi.", | 4403 | "aiHub.description": "Tüm yapay zekâ araçları tek yerde: not çıktıları ve podcast, öğrenci analizi ve Çelebi.", |
| 4401 | "aiHub.tab.studio": "Not stüdyosu", | 4404 | "aiHub.tab.studio": "Not stüdyosu", |
src/lib/search-text.test.tsmodified+14 -1
| @@ -1,5 +1,5 @@ | |||
| 1 | import { describe, expect, it } from "vitest"; | 1 | import { describe, expect, it } from "vitest"; |
| 2 | -import { foldSearchText, matchesSearch } from "@/lib/search-text"; | 2 | +import { foldSearchText, matchesSearch, searchMatchRanges } from "@/lib/search-text"; |
| 3 | 3 | ||
| 4 | describe("foldSearchText", () => { | 4 | describe("foldSearchText", () => { |
| 5 | it("folds Turkish letters onto their ASCII shape", () => { | 5 | it("folds Turkish letters onto their ASCII shape", () => { |
| @@ -48,3 +48,16 @@ describe("matchesSearch", () => { | |||
| 48 | expect(matchesSearch("ayse", null, undefined)).toBe(false); | 48 | expect(matchesSearch("ayse", null, undefined)).toBe(false); |
| 49 | }); | 49 | }); |
| 50 | }); | 50 | }); |
| 51 | + | ||
| 52 | +describe("searchMatchRanges", () => { | ||
| 53 | + it("keeps original offsets while folding Turkish letters", () => { | ||
| 54 | + expect(searchMatchRanges("Öğretmen Ayşe", "ogretmen")).toEqual([{ start: 0, end: 8 }]); | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + it("returns every occurrence in reading order", () => { | ||
| 58 | + expect(searchMatchRanges("Sınav bugün, sınav yarın", "sinav")).toEqual([ | ||
| 59 | + { start: 0, end: 5 }, | ||
| 60 | + { start: 13, end: 18 }, | ||
| 61 | + ]); | ||
| 62 | + }); | ||
| 63 | +}); | ||
src/lib/search-text.tsmodified+43 -0
| @@ -38,3 +38,46 @@ export function matchesSearch(query: string, ...fields: (string | null | undefin | |||
| 38 | const haystack = fields.map(foldSearchText).join(" "); | 38 | const haystack = fields.map(foldSearchText).join(" "); |
| 39 | return terms.every((term) => haystack.includes(term)); | 39 | return terms.every((term) => haystack.includes(term)); |
| 40 | } | 40 | } |
| 41 | + | ||
| 42 | +export type SearchMatchRange = { start: number; end: number }; | ||
| 43 | + | ||
| 44 | +/** | ||
| 45 | + * Finds the visible ranges for a query while keeping the original string's | ||
| 46 | + * offsets. Search stays Turkish-aware, but the caller can still render the | ||
| 47 | + * matching characters in their original spelling. | ||
| 48 | + */ | ||
| 49 | +export function searchMatchRanges(value: string, query: string): SearchMatchRange[] { | ||
| 50 | + const terms = foldSearchText(query).split(/\s+/).filter(Boolean); | ||
| 51 | + if (terms.length === 0) return []; | ||
| 52 | + | ||
| 53 | + const foldedChars: string[] = []; | ||
| 54 | + const sourceRanges: Array<{ start: number; end: number }> = []; | ||
| 55 | + let sourceOffset = 0; | ||
| 56 | + for (const char of value) { | ||
| 57 | + const folded = foldSearchText(char); | ||
| 58 | + for (const foldedChar of folded) { | ||
| 59 | + foldedChars.push(foldedChar); | ||
| 60 | + sourceRanges.push({ start: sourceOffset, end: sourceOffset + char.length }); | ||
| 61 | + } | ||
| 62 | + sourceOffset += char.length; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + const foldedValue = foldedChars.join(""); | ||
| 66 | + const ranges: SearchMatchRange[] = []; | ||
| 67 | + for (const term of terms) { | ||
| 68 | + let from = 0; | ||
| 69 | + while (from < foldedValue.length) { | ||
| 70 | + const index = foldedValue.indexOf(term, from); | ||
| 71 | + if (index < 0) break; | ||
| 72 | + const endIndex = index + term.length - 1; | ||
| 73 | + const start = sourceRanges[index]?.start; | ||
| 74 | + const end = sourceRanges[endIndex]?.end; | ||
| 75 | + if (start !== undefined && end !== undefined) ranges.push({ start, end }); | ||
| 76 | + from = index + Math.max(term.length, 1); | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + return ranges | ||
| 81 | + .filter((range, index, all) => all.findIndex((other) => other.start === range.start && other.end === range.end) === index) | ||
| 82 | + .sort((a, b) => a.start - b.start || a.end - b.end); | ||
| 83 | +} | ||