From 90327098fab707321d07c804016981f9b587ce8e Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Mon, 16 Nov 2026 10:37:57 -0500 Subject: [PATCH] Knowledge search: a base tab or Search Code runs the typed words again there The tabs beside the built-in search only changed which bases the next press of Search would ask, and added to each other; "Search Code" in a base's panel closed the panel and did the same, so the results on screen never changed. Now a tab picks one base and runs what is typed (or the search on screen) again there, focus stays in the box after a click, the panel hands focus back to the box, the last answer stays on screen marked busy until the new one is in, and a slow answer for an earlier tab never replaces a later one. --- dashboard/e2e/knowledge-builtin.spec.ts | 171 +++++++++++++++++- .../src/components/knowledge/builtin.tsx | 117 ++++++++---- 2 files changed, 248 insertions(+), 40 deletions(-) diff --git a/dashboard/e2e/knowledge-builtin.spec.ts b/dashboard/e2e/knowledge-builtin.spec.ts index dbdfc091..8dd614a7 100644 --- a/dashboard/e2e/knowledge-builtin.spec.ts +++ b/dashboard/e2e/knowledge-builtin.spec.ts @@ -290,12 +290,11 @@ test.describe("built-in knowledge bases", () => { await shot(page, "knowledge-builtin-share-1280", false); await page.keyboard.press("Escape"); - // Filter to Work: only Work is searched. + // Filter to Work: the words run again, in Work only. db.calls.length = 0; await page.getByTestId("filter-work").click(); await expect(page.getByTestId("filter-work")).toHaveAttribute("aria-pressed", "true"); await expect(page.getByTestId("filter-all")).toHaveAttribute("aria-pressed", "false"); - await page.getByTestId("builtin-search-go").click(); await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/work/search?q=refunds"]); await expect(grouped.getByTestId("builtin-summary")).toHaveText("1 result for \u201crefunds\u201d in Work"); await page.getByTestId("builtin-clear").click(); @@ -412,3 +411,171 @@ test.describe("built-in knowledge bases", () => { await shot(page, "shared-closed-1280"); }); }); + +// The base tabs beside the search box, and "Search Code" in a base's panel: +// each picks the one base to search, keeps the words typed and runs them again +// there, without taking focus from the box or blanking the results meanwhile. +test.describe("built-in search: switching the base", () => { + const box = (page: Page) => page.getByRole("searchbox", { name: "Search the built-in bases" }); + const summary = (page: Page) => page.getByTestId("builtin-summary"); + const pressed = (page: Page) => + page + .getByTestId("builtin-filters") + .getByRole("button") + .evaluateAll((els) => els.filter((e) => e.getAttribute("aria-pressed") === "true").map((e) => e.getAttribute("data-testid"))); + + async function searched(page: Page, db: Db) { + await page.goto("/knowledge/"); + await expect(page.getByTestId("builtin-card")).toHaveCount(6); + await box(page).fill("refunds"); + await box(page).press("Enter"); + await expect(summary(page)).toHaveText("3 results for “refunds” in all bases open to you"); + await expect.poll(() => searches(db).length).toBe(6); + db.calls.length = 0; + } + + test("a tab is one base: the typed words run again there, and focus stays in the box", async ({ page }) => { + const db = await mock(page, true); + await searched(page, db); + await box(page).focus(); + + await page.getByTestId("filter-logs").click(); + expect(await pressed(page)).toEqual(["filter-logs"]); + await expect(box(page)).toHaveAttribute("placeholder", "Search Logs"); + await expect(box(page)).toHaveValue("refunds"); + await expect(box(page)).toBeFocused(); + await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/logs/search?q=refunds"]); + await expect(page.getByTestId("builtin-search-notes")).toContainText("Logs couldn't be searched: Logs didn't answer in time."); + await expect(summary(page)).toHaveText("0 results for “refunds” in Logs"); + + // Another tab replaces it rather than adding to it. + db.calls.length = 0; + await page.getByTestId("filter-code").click(); + expect(await pressed(page)).toEqual(["filter-code"]); + await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/code/search?q=refunds"]); + await expect(summary(page)).toHaveText("2 results for “refunds” in Code"); + await expect(box(page)).toBeFocused(); + + // All searches every open base again. + db.calls.length = 0; + await page.getByTestId("filter-all").click(); + expect(await pressed(page)).toEqual(["filter-all"]); + await expect.poll(() => searches(db).length).toBe(6); + await expect(summary(page)).toHaveText("3 results for “refunds” in all bases open to you"); + await noSideways(page); + }); + + test("Search Code in the panel switches the search to Code", async ({ page }) => { + const db = await mock(page, true); + await searched(page, db); + await card(page, "code").getByTestId("builtin-open").click(); + const panel = page.getByTestId("builtin-panel"); + await panel.getByRole("button", { name: "Search Code" }).click(); + await expect(panel).toHaveCount(0); + expect(await pressed(page)).toEqual(["filter-code"]); + await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/code/search?q=refunds"]); + await expect(summary(page)).toHaveText("2 results for “refunds” in Code"); + await expect(box(page)).toHaveValue("refunds"); + await expect(box(page)).toBeFocused(); + // Still there a moment later: nothing hands focus back to the card. + await page.waitForTimeout(300); + await expect(box(page)).toBeFocused(); + }); + + test("with nothing typed a tab only narrows; the next search runs there", async ({ page }) => { + const db = await mock(page, true); + await page.goto("/knowledge/"); + await expect(page.getByTestId("builtin-card")).toHaveCount(6); + await page.getByTestId("filter-work").click(); + await expect(box(page)).toHaveAttribute("placeholder", "Search Work"); + await page.waitForTimeout(300); + expect(searches(db)).toEqual([]); + await expect(page.getByTestId("builtin-grouped")).toHaveCount(0); + await box(page).fill("refunds"); + await box(page).press("Enter"); + await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/work/search?q=refunds"]); + await expect(summary(page)).toHaveText("1 result for “refunds” in Work"); + }); + + test("keyboard: Tab from the box reaches the tabs, Space picks one, Escape closes the panel", async ({ page }) => { + const db = await mock(page, true); + await searched(page, db); + await box(page).focus(); + // Box, its Search button, then the tabs in order. + await page.keyboard.press("Tab"); + await expect(page.getByTestId("builtin-search-go")).toBeFocused(); + await page.keyboard.press("Tab"); + await expect(page.getByTestId("filter-all")).toBeFocused(); + await page.keyboard.press("Tab"); + await page.keyboard.press("Tab"); + await expect(page.getByTestId("filter-work")).toBeFocused(); + await page.keyboard.press("Space"); + expect(await pressed(page)).toEqual(["filter-work"]); + // A keyboard pick leaves focus on the tab, so the next Tab carries on. + await expect(page.getByTestId("filter-work")).toBeFocused(); + await expect.poll(() => searches(db)).toEqual(["GET /api/knowledge/builtin/work/search?q=refunds"]); + await expect(summary(page)).toHaveText("1 result for “refunds” in Work"); + // Back along Code, All and the Search button to the box. + for (let i = 0; i < 4; i++) await page.keyboard.press("Shift+Tab"); + await expect(box(page)).toBeFocused(); + + await card(page, "logs").getByTestId("builtin-open").click(); + await expect(page.getByTestId("builtin-panel")).toBeVisible(); + await page.keyboard.press("Escape"); + await expect(page.getByTestId("builtin-panel")).toHaveCount(0); + expect(await pressed(page)).toEqual(["filter-work"]); + }); + + test("a new base's answer replaces the old one without blanking it first", async ({ page }) => { + const db = await mock(page, true); + await searched(page, db); + // Hold the next answer so the moment between can be looked at. + let release!: () => void; + const held = new Promise((r) => (release = r)); + await page.route(/\/api\/knowledge\/builtin\/work\/search/, async (route) => { + await held; + await route.fallback(); + }); + await page.getByTestId("filter-work").click(); + const grouped = page.getByTestId("builtin-grouped"); + await expect(grouped).toHaveAttribute("aria-busy", "true"); + await expect(grouped.getByTestId("builtin-group")).toHaveCount(2); + await expect(page.getByTestId("builtin-search-go")).toHaveText("Searching…"); + release(); + await expect(summary(page)).toHaveText("1 result for “refunds” in Work"); + await expect(grouped).toHaveAttribute("aria-busy", "false"); + await expect(grouped.getByTestId("builtin-group")).toHaveCount(1); + }); + + test("a quick second tab wins over a slow first one", async ({ page }) => { + const db = await mock(page, true); + await searched(page, db); + let release!: () => void; + const held = new Promise((r) => (release = r)); + await page.route(/\/api\/knowledge\/builtin\/code\/search/, async (route) => { + await held; + await route.fallback(); + }); + await page.getByTestId("filter-code").click(); + await page.getByTestId("filter-work").click(); + await expect(summary(page)).toHaveText("1 result for “refunds” in Work"); + release(); + await page.waitForTimeout(300); + await expect(summary(page)).toHaveText("1 result for “refunds” in Work"); + }); + + test("at phone width the tabs wrap inside the screen and a pick moves nothing sideways", async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); + const db = await mock(page, true); + await searched(page, db); + const tabs = page.getByTestId("builtin-filters").getByRole("button"); + const inside = await tabs.evaluateAll((els) => els.every((e) => e.getBoundingClientRect().right <= window.innerWidth && e.getBoundingClientRect().left >= 0)); + expect(inside).toBe(true); + const before = await box(page).boundingBox(); + await page.getByTestId("filter-infra").click(); + await expect(summary(page)).toContainText("in Infrastructure"); + const after = await box(page).boundingBox(); + expect(after).toEqual(before); + await noSideways(page); + }); +}); diff --git a/dashboard/src/components/knowledge/builtin.tsx b/dashboard/src/components/knowledge/builtin.tsx index b8f2e117..78664d29 100644 --- a/dashboard/src/components/knowledge/builtin.tsx +++ b/dashboard/src/components/knowledge/builtin.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useId, useRef, useState, type ReactNode, type RefObject } from "react"; +import { useEffect, useId, useRef, useState, type MouseEvent as ReactMouseEvent, type ReactNode, type RefObject } from "react"; import { Callout } from "@/components/callout"; import { CopyButton } from "@/components/copy-button"; import { Drawer } from "@/components/drawer"; @@ -84,7 +84,8 @@ export function BuiltinBases() { const [list, setList] = useState(null); const [failed, setFailed] = useState(false); const [openKey, setOpenKey] = useState(null); - const [scope, setScope] = useState([]); + // The one base the search is narrowed to; null searches every open base. + const [scope, setScope] = useState(null); const searchRef = useRef(null); const openerRef = useRef(null); @@ -155,10 +156,11 @@ export function BuiltinBases() { onClose={() => setOpenKey(null)} onChange={replace} onSearch={(k) => { - setScope([k]); + // The panel hands focus back to the search box, not the card, and + // the search box runs what is typed again in this base. + openerRef.current = searchRef.current; + setScope(k); setOpenKey(null); - // After the panel hands focus back to the card. - setTimeout(() => searchRef.current?.focus(), 50); }} /> @@ -314,41 +316,60 @@ function CrossSearch({ inputRef, }: { bases: BuiltinBase[]; - scope: BuiltinKey[]; - setScope: (s: BuiltinKey[]) => void; + scope: BuiltinKey | null; + setScope: (s: BuiltinKey | null) => void; inputRef: RefObject; }) { const id = useId(); const [q, setQ] = useState(""); - const [asked, setAsked] = useState(""); - const [groups, setGroups] = useState(null); - const [skipped, setSkipped] = useState([]); + // The answer on screen: the words, where they were searched, what came back. + const [shown, setShown] = useState<{ query: string; where: string; groups: Group[]; skipped: BuiltinBase[] } | null>(null); const [busy, setBusy] = useState(false); const ask = useRef(0); const open = bases.filter((b) => b.access.open && !b.audience.paused); - const chosen = scope.length ? open.filter((b) => scope.includes(b.key)) : open; + const chosen = scope ? open.filter((b) => b.key === scope) : open; - const run = async () => { - const query = q.trim(); + const run = async (query: string) => { if (!query || chosen.length === 0) return; const n = ++ask.current; + const where = scope ? chosen[0].name : "all bases open to you"; + // Named so nobody reads "nothing matched" as "nothing there". + const skipped = scope ? [] : bases.filter((b) => !open.includes(b)); + // The last answer stays on screen, marked busy, until this one is in. setBusy(true); const settled = await Promise.allSettled(chosen.map((b) => builtinBases.search(b.key, query))); if (n !== ask.current) return; - setGroups( - chosen.map((b, i) => { - const s = settled[i]; - return s.status === "fulfilled" ? { key: b.key, name: b.name, found: s.value } : { key: b.key, name: b.name, error: s.reason instanceof Error ? s.reason.message : "The search failed." }; - }), - ); - // Named so nobody reads "nothing matched" as "nothing there". - setSkipped(scope.length ? [] : bases.filter((b) => !open.includes(b))); - setAsked(query); + const groups = chosen.map((b, i): Group => { + const s = settled[i]; + return s.status === "fulfilled" ? { key: b.key, name: b.name, found: s.value } : { key: b.key, name: b.name, error: s.reason instanceof Error ? s.reason.message : "The search failed." }; + }); + setShown({ query, where, groups, skipped }); setBusy(false); }; - const toggle = (k: BuiltinKey) => setScope(scope.includes(k) ? scope.filter((x) => x !== k) : [...scope, k]); - const where = scope.length === 0 ? "all bases open to you" : chosen.map((b) => b.name).join(", "); + // A new base (a tab, or "Search Code" in a panel) runs the words again + // there: what is typed, or else the search on screen. + const runLatest = useRef(run); + runLatest.current = run; + const shownQuery = useRef(""); + shownQuery.current = shown?.query ?? ""; + const typed = useRef(""); + typed.current = q.trim(); + const firstScope = useRef(true); + useEffect(() => { + if (firstScope.current) { + firstScope.current = false; + return; + } + runLatest.current(typed.current || shownQuery.current); + }, [scope]); + + const pick = (k: BuiltinKey | null, e: ReactMouseEvent) => { + if (k !== scope) setScope(k); + // A click keeps typing in the box; a key press (Space, Enter: detail 0) + // leaves focus on the tab so Tab carries on along them. + if (e.detail > 0) inputRef.current?.focus(); + }; return (
@@ -358,7 +379,7 @@ function CrossSearch({ className="relative min-w-0 flex-1 basis-72" onSubmit={(e) => { e.preventDefault(); - run(); + run(q.trim()); }} >
- {groups && ( + {shown && ( { ask.current++; - setGroups(null); - setAsked(""); + setShown(null); setBusy(false); }} /> @@ -424,13 +445,29 @@ function CrossSearch({ ); } -function FilterChip({ pressed, disabled, onClick, children, testId, title }: { pressed: boolean; disabled?: boolean; onClick: () => void; children: ReactNode; testId?: string; title?: string }) { +function FilterChip({ + pressed, + disabled, + onClick, + children, + testId, + title, +}: { + pressed: boolean; + disabled?: boolean; + onClick: (e: ReactMouseEvent) => void; + children: ReactNode; + testId?: string; + title?: string; +}) { return (