From c59cd1dc112bcd0c01b87d34edadb0525dc57dce Mon Sep 17 00:00:00 2001 From: Cato <41178744+catomean@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:40:56 +0100 Subject: [PATCH] refactor(palette): search as you type through searchkit's useSearchAsYouType (#380) The command palette hand-rolled its own debounce, abort and stale-answer drop. @bitbaum/searchkit/react's useSearchAsYouType does exactly that, and the org rule is that a package capability is used, not re-implemented. The local useSearch is gone; the palette keeps its cache (an earlier query answers without a request), its keyboard behaviour and its rows. What the reader sees: the last research results stay on screen while a newer query loads instead of blanking out, the "All N results" count only shows for the query actually answered, and a failed research search now says so on the last row instead of failing silently. package-plan.json binds CommandPalette.tsx to useSearchAsYouType under the search block; README describes the new behaviour. Claude-Session: https://claude.ai/code/session_019MDtmuvTGrpo3VChP3skH8 Co-authored-by: Claude --- README.md | 4 +- components/shell/CommandPalette.tsx | 59 ++++++++++++++--------------- package-plan.json | 4 ++ 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a2fed99..ed95438 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,9 @@ palette. in the account menu, never in the research list. - **⌘K / Ctrl+K / "/"** opens the command palette: every page (including the reader views, data and project pages that have no sidebar row) plus live - results from the search index; Enter on the last row opens `/search`. + results from the search index, typed through `@bitbaum/searchkit/react`'s + `useSearchAsYouType` (the last answer stays while a newer one loads, and a + failed search says so); Enter on the last row opens `/search`. - **Footer**: sitekit's `SiteFooter` (application appearance): About, Notes, Changelog, Roadmap, Join, Source, Correction, and the freshness badge. diff --git a/components/shell/CommandPalette.tsx b/components/shell/CommandPalette.tsx index f331b94..a29633d 100644 --- a/components/shell/CommandPalette.tsx +++ b/components/shell/CommandPalette.tsx @@ -9,8 +9,9 @@ * /api/search, the same index behind /search, and the last row always opens * the full results page for the query. */ -import React, { useEffect, useId, useMemo, useRef, useState } from 'react'; +import React, { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'; import { useRouter } from 'next/navigation'; +import { useSearchAsYouType } from '@bitbaum/searchkit/react'; import { SEARCH_TYPE_LABEL, segments, type SearchResult } from '@/lib/search-types'; import { Highlight } from '@/components/portal/Highlight'; @@ -36,33 +37,24 @@ function pageRow(e: PaletteEntry): Row { return { key: e.key, href: e.href, group: e.group, title: e.label, sub: e.hint }; } -function useSearch(query: string): SearchResult | null { +/** + * Search as you type is searchkit's: each keystroke aborts the request before + * it and a late answer to an older query is dropped. While a newer query + * loads, the last answer stays on screen. What stays here is the cache, so + * backspacing to an earlier query costs no request. + */ +function useResearch(query: string) { const cache = useRef(new Map()); - const [latest, setLatest] = useState(null); - useEffect(() => { - if (query.length < 2) return; - const hit = cache.current.get(query); - const controller = new AbortController(); - const t = setTimeout( - () => { - if (hit) return setLatest(hit); - fetch(`/api/search?q=${encodeURIComponent(query)}`, { signal: controller.signal }) - .then((r) => (r.ok ? (r.json() as Promise) : null)) - .then((data) => { - if (!data) return; - cache.current.set(query, data); - setLatest(data); - }) - .catch(() => {}); - }, - hit ? 0 : DEBOUNCE_MS, - ); - return () => { - clearTimeout(t); - controller.abort(); - }; - }, [query]); - return query.length >= 2 && latest?.query === query ? latest : null; + const search = useCallback(async (text: string, signal: AbortSignal) => { + const hit = cache.current.get(text); + if (hit) return hit; + const r = await fetch(`/api/search?q=${encodeURIComponent(text)}`, { signal }); + if (!r.ok) throw new Error(`Search answered ${r.status}`); + const data = (await r.json()) as SearchResult; + cache.current.set(text, data); + return data; + }, []); + return useSearchAsYouType(query, search, { debounceMs: DEBOUNCE_MS, minLength: 2 }); } export function CommandPalette({ @@ -79,7 +71,9 @@ export function CommandPalette({ const [q, setQ] = useState(''); const [active, setActive] = useState(0); const query = q.trim(); - const result = useSearch(query); + const { results: result, error } = useResearch(query); + /** The answer on screen may be for an earlier query while this one loads. */ + const fresh = result?.query === query; useDialog(panel, onClose, input); const rows = useMemo(() => { @@ -102,11 +96,14 @@ export function CommandPalette({ key: '__all', href: searchHref(query), group: 'Search', - title: result ? `All ${result.total} results for “${query}”` : `Search for “${query}”`, - sub: 'Every bottleneck, company, country, policy, note and event.', + title: + result && fresh ? `All ${result.total} results for “${query}”` : `Search for “${query}”`, + sub: error + ? 'The research search did not answer. Open the full results page to try again.' + : 'Every bottleneck, company, country, policy, note and event.', }; return [...pages.slice(0, PAGE_LIMIT), ...research, all]; - }, [entries, query, result]); + }, [entries, query, result, fresh, error]); const current = Math.min(active, rows.length - 1); diff --git a/package-plan.json b/package-plan.json index 70f535e..0d576b0 100644 --- a/package-plan.json +++ b/package-plan.json @@ -140,6 +140,10 @@ { "file": "lib/search/build.ts", "symbols": ["createVocabulary"] + }, + { + "file": "components/shell/CommandPalette.tsx", + "symbols": ["useSearchAsYouType"] } ] },