Skip to content
TypeScript

perf(web): virtualize command palette results (#15266)

oliverfffe6e6c82c1
diff --git a/apps/web/src/components/CommandPalette.logic.test.ts b/apps/web/src/components/CommandPalette.logic.test.ts
index 916b66a32b3..2bd7c118c69 100644
--- a/apps/web/src/components/CommandPalette.logic.test.ts
+++ b/apps/web/src/components/CommandPalette.logic.test.ts
@@ -5,12 +5,14 @@ import { makeThreadFixture } from "../test-fixtures";
import {
buildBrowseGroups,
buildCommandPaletteProjectMetadata,
+ buildCommandPaletteRows,
buildProjectActionItems,
buildThreadActionItems,
buildLinkedThreadActionItems,
enumerateCommandPaletteItems,
filterPinnedBrowseEntries,
filterCommandPaletteGroups,
+ findHighlightedCommandPaletteItem,
reduceCommandPaletteUiState,
type CommandPaletteActionItem,
type CommandPaletteGroup,
@@ -849,3 +851,46 @@ describe("filterCommandPaletteGroups", () => {
]);
});
});
+
+describe("virtualized command palette rows", () => {
+ const action = (value: string, disabled = false): CommandPaletteActionItem => ({
+ kind: "action",
+ value,
+ searchTerms: [],
+ title: value,
+ icon: null,
+ ...(disabled ? { disabled } : {}),
+ run: async () => {},
+ });
+ const groups: CommandPaletteGroup[] = [
+ { value: "actions", label: "Actions", items: [action("new-thread"), action("offline", true)] },
+ { value: "threads", label: "Threads", items: [action("thread-a"), action("thread-b")] },
+ ];
+
+ it("keeps group order and headings while indexing only enabled items", () => {
+ const { rows, itemValues, rowIndexByItemIndex } = buildCommandPaletteRows(groups);
+
+ expect(rows.map((row) => (row.kind === "label" ? `# ${row.label}` : row.key))).toEqual([
+ "# Actions",
+ "actions:new-thread",
+ "actions:offline",
+ "# Threads",
+ "threads:thread-a",
+ "threads:thread-b",
+ ]);
+ expect(itemValues).toEqual(["new-thread", "thread-a", "thread-b"]);
+ expect(rowIndexByItemIndex).toEqual([1, 4, 5]);
+ expect(rows.flatMap((row) => (row.kind === "item" ? [row.itemIndex] : []))).toEqual([
+ 0,
+ null,
+ 1,
+ 2,
+ ]);
+ });
+
+ it("resolves Enter to the highlighted item without needing its row mounted", () => {
+ expect(findHighlightedCommandPaletteItem(groups, "thread-b")?.value).toBe("thread-b");
+ expect(findHighlightedCommandPaletteItem(groups, "offline")).toBeNull();
+ expect(findHighlightedCommandPaletteItem(groups, null)).toBeNull();
+ });
+});
diff --git a/apps/web/src/components/CommandPalette.logic.ts b/apps/web/src/components/CommandPalette.logic.ts
index d031f36ed35..49e963f6831 100644
--- a/apps/web/src/components/CommandPalette.logic.ts
+++ b/apps/web/src/components/CommandPalette.logic.ts
@@ -172,6 +172,65 @@ export interface CommandPaletteView {
readonly initialQuery?: string;
}
+export type CommandPaletteRow =
+ | {
+ readonly kind: "label";
+ readonly key: string;
+ readonly label: string;
+ readonly first: boolean;
+ }
+ | {
+ readonly kind: "item";
+ readonly key: string;
+ readonly item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
+ /** Position among enabled items, or null for disabled rows the keyboard skips. */
+ readonly itemIndex: number | null;
+ };
+
+/**
+ * Flattens groups into the rows a virtualized list renders. `itemValues` is the
+ * highlightable item order Base UI navigates; `rowIndexByItemIndex` maps a
+ * highlight back to its row for scrolling.
+ */
+export function buildCommandPaletteRows(groups: ReadonlyArray<CommandPaletteGroup>) {
+ const rows: CommandPaletteRow[] = [];
+ const itemValues: string[] = [];
+ const rowIndexByItemIndex: number[] = [];
+ for (const group of groups) {
+ if (group.label) {
+ rows.push({
+ kind: "label",
+ key: `group:${group.value}`,
+ label: group.label,
+ first: rows.length === 0,
+ });
+ }
+ for (const item of group.items) {
+ const itemIndex = item.disabled ? null : itemValues.length;
+ if (itemIndex !== null) {
+ itemValues.push(item.value);
+ rowIndexByItemIndex.push(rows.length);
+ }
+ rows.push({ kind: "item", key: `${group.value}:${item.value}`, item, itemIndex });
+ }
+ }
+ return { rows, itemValues, rowIndexByItemIndex };
+}
+
+/** The enabled item Enter should run for a highlight, whether or not its row is mounted. */
+export function findHighlightedCommandPaletteItem(
+ groups: ReadonlyArray<CommandPaletteGroup>,
+ highlightedItemValue: string | null,
+): CommandPaletteActionItem | CommandPaletteSubmenuItem | null {
+ if (highlightedItemValue === null) return null;
+ for (const group of groups) {
+ for (const item of group.items) {
+ if (item.value === highlightedItemValue && !item.disabled) return item;
+ }
+ }
+ return null;
+}
+
export function enumerateCommandPaletteItems(
items: ReadonlyArray<CommandPaletteActionItem>,
): CommandPaletteActionItem[] {
diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx
index aad0e71a06b..f7b78b5ae67 100644
--- a/apps/web/src/components/CommandPalette.tsx
+++ b/apps/web/src/components/CommandPalette.tsx
@@ -77,6 +77,7 @@ import {
type KeyboardEvent,
type ReactNode,
} from "react";
+import type { LegendListRef } from "@legendapp/list/react";
import { useAtomValue } from "@effect/atom-react";
import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
@@ -157,7 +158,9 @@ import {
buildRootGroups,
buildThreadActionItems,
buildLinkedThreadActionItems,
+ buildCommandPaletteRows,
enumerateCommandPaletteItems,
+ findHighlightedCommandPaletteItem,
type CommandPaletteActionItem,
type CommandPaletteOpenIntent,
type CommandPaletteSubmenuItem,
@@ -174,7 +177,10 @@ import {
import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic";
import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
import { CommandPaletteContent } from "./CommandPaletteContent";
-import { CommandPaletteResults } from "./CommandPaletteResults";
+import {
+ CommandPaletteVirtualizedResults,
+ scrollCommandPaletteRowIntoView,
+} from "./CommandPaletteResults";
import { AzureDevOpsIcon, BitbucketIcon, GitHubIcon, GitLabIcon, ForgejoIcon } from "./Icons";
import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
import { Checkbox } from "./ui/checkbox";
@@ -694,6 +700,15 @@ function OpenCommandPaletteDialog(props: {
const deferredQuery = useDeferredValue(query);
const isActionsOnly = deferredQuery.startsWith(">");
const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
+ const resultListRef = useRef<LegendListRef | null>(null);
+ // Typing or entering a submenu clears the highlight. Base UI keeps its own on the
+ // first row, but the palette shows none until the user navigates, and the first
+ // ArrowDown lands on it.
+ const highlightClearedRef = useRef(false);
+ function clearTypedHighlight(): void {
+ highlightClearedRef.current = true;
+ setHighlightedItemValue(null);
+ }
const clientSettings = useClientSettings();
const createProject = useAtomCommand(projectEnvironment.create, {
reportFailure: false,
@@ -1425,7 +1440,7 @@ function OpenCommandPaletteDialog(props: {
? {
source: match.source,
snippet: match.snippet,
- query: threadSearchQuery,
+ query: threadSearch.query,
}
: undefined;
},
@@ -1446,7 +1461,7 @@ function OpenCommandPaletteDialog(props: {
projectTitleById,
providerEntryByEnvironmentAndInstanceId,
threadContentMatchByKey,
- threadSearchQuery,
+ threadSearch.query,
threads,
],
);
@@ -1463,6 +1478,7 @@ function OpenCommandPaletteDialog(props: {
...(view.initialQuery ? { initialQuery: view.initialQuery } : {}),
},
]);
+ highlightClearedRef.current = true;
setHighlightedItemValue(null);
setQuery(view.initialQuery ?? "");
},
@@ -1494,7 +1510,7 @@ function OpenCommandPaletteDialog(props: {
function handleQueryChange(nextQuery: string): void {
browseNavigation.invalidate();
- setHighlightedItemValue(null);
+ clearTypedHighlight();
setQuery(nextQuery);
if (nextQuery === "" && currentView?.initialQuery) {
popView();
@@ -2938,6 +2954,9 @@ function OpenCommandPaletteDialog(props: {
} else if (isBrowsing) {
displayedGroups = relativePathNeedsActiveProject ? [] : browseGroups;
}
+ const resultRows = buildCommandPaletteRows(displayedGroups);
+ const autoHighlightsFirstRow =
+ !isBrowsing && !isRemoteProjectCloneFlow && newProjectFlow === null;
const inputPlaceholder =
newProjectFlow !== null
@@ -3083,6 +3102,43 @@ function OpenCommandPaletteDialog(props: {
if (event.key === "Backspace" && query === "" && isSubmenu) {
event.preventDefault();
popView();
+ return;
+ }
+
+ // Base UI ignores navigation keys with modifiers, so these fallbacks do too.
+ if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) return;
+ // Base UI only keeps a hidden highlight on the first row when it auto-highlights.
+ const firstItemValue = autoHighlightsFirstRow ? resultRows.itemValues[0] : undefined;
+ if (
+ event.key === "ArrowDown" &&
+ highlightClearedRef.current &&
+ firstItemValue &&
+ !event.nativeEvent.isComposing
+ ) {
+ (event as typeof event & { preventBaseUIHandler?: () => void }).preventBaseUIHandler?.();
+ event.preventDefault();
+ highlightClearedRef.current = false;
+ setHighlightedItemValue(firstItemValue);
+ scrollCommandPaletteRowIntoView(
+ resultListRef.current,
+ resultRows.rowIndexByItemIndex[0] ?? 0,
+ );
+ return;
+ }
+
+ // Base UI clicks the highlighted row on Enter, which does nothing once the
+ // virtualized list has unmounted it, so run the tracked highlight directly.
+ if (event.key === "Enter" && !event.nativeEvent.isComposing && event.keyCode !== 229) {
+ const highlightedItem = findHighlightedCommandPaletteItem(
+ displayedGroups,
+ highlightedItemValue ?? firstItemValue ?? null,
+ );
+ if (highlightedItem) {
+ (event as typeof event & { preventBaseUIHandler?: () => void }).preventBaseUIHandler?.();
+ event.preventDefault();
+ event.stopPropagation();
+ executeItem(highlightedItem);
+ }
}
}
@@ -3349,9 +3405,7 @@ function OpenCommandPaletteDialog(props: {
<CommandPaletteContent
key={`${viewStack.length}-${browseGeneration}-${isBrowsing}-${newProjectFlow ? "new-project" : (addProjectCloneFlow?.step ?? "none")}`}
aria-label="Command palette"
- autoHighlight={
- isBrowsing || isRemoteProjectCloneFlow || newProjectFlow !== null ? false : "always"
- }
+ autoHighlight={autoHighlightsFirstRow ? "always" : false}
footerActionLabel={footerActionLabel}
footerTrailing={footerTrailing}
inputAccessory={inputAccessory}
@@ -3387,8 +3441,16 @@ function OpenCommandPaletteDialog(props: {
onKeyDown: handleKeyDown,
}}
mode="none"
- onItemHighlighted={(value) => {
+ items={resultRows.itemValues}
+ virtualized
+ onItemHighlighted={(value, eventDetails) => {
+ if (eventDetails.reason === "none" && highlightClearedRef.current) return;
+ highlightClearedRef.current = false;
setHighlightedItemValue(typeof value === "string" ? value : null);
+ const rowIndex = resultRows.rowIndexByItemIndex[eventDetails.index];
+ if (eventDetails.reason === "keyboard" && rowIndex !== undefined) {
+ scrollCommandPaletteRowIntoView(resultListRef.current, rowIndex);
+ }
}}
onValueChange={handleQueryChange}
showBackHint={isSubmenu}
@@ -3426,8 +3488,9 @@ function OpenCommandPaletteDialog(props: {
</div>
</div>
) : null}
- <CommandPaletteResults
- groups={displayedGroups}
+ <CommandPaletteVirtualizedResults
+ rows={resultRows.rows}
+ listRef={resultListRef}
highlightedItemValue={highlightedItemValue}
isActionsOnly={isActionsOnly}
keybindings={keybindings}
diff --git a/apps/web/src/components/CommandPaletteResults.tsx b/apps/web/src/components/CommandPaletteResults.tsx
index 4be4aee17ff..b909a0c97e4 100644
--- a/apps/web/src/components/CommandPaletteResults.tsx
+++ b/apps/web/src/components/CommandPaletteResults.tsx
@@ -1,9 +1,13 @@
import { type ResolvedKeybindingsConfig } from "@t3tools/contracts";
+import { LegendList, type LegendListRef } from "@legendapp/list/react";
import { ChevronRightIcon } from "lucide-react";
+import { type RefObject, useCallback, useState } from "react";
+import { cn } from "~/lib/utils";
import { shortcutLabelForCommand } from "../keybindings";
import {
type CommandPaletteActionItem,
type CommandPaletteGroup,
+ type CommandPaletteRow,
type CommandPaletteSubmenuItem,
} from "./CommandPalette.logic";
import {
@@ -12,8 +16,11 @@ import {
CommandGroupLabel,
CommandItem,
CommandList,
+ CommandListHeading,
+ CommandListVirtualized,
CommandShortcut,
} from "./ui/command";
+import { getVirtualizedScrollFadeClassName } from "./ui/scroll-area";
import { ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
interface CommandPaletteResultsProps {
@@ -25,16 +32,20 @@ interface CommandPaletteResultsProps {
onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
}
+function CommandPaletteEmptyState(props: { emptyStateMessage?: string; isActionsOnly: boolean }) {
+ return (
+ <div className="py-10 text-center text-sm text-muted-foreground">
+ {props.emptyStateMessage ??
+ (props.isActionsOnly
+ ? "No matching actions."
+ : "No matching commands, projects, or threads.")}
+ </div>
+ );
+}
+
export function CommandPaletteResults(props: CommandPaletteResultsProps) {
if (props.groups.length === 0) {
- return (
- <div className="py-10 text-center text-sm text-muted-foreground">
- {props.emptyStateMessage ??
- (props.isActionsOnly
- ? "No matching actions."
- : "No matching commands, projects, or threads.")}
- </div>
- );
+ return <CommandPaletteEmptyState {...props} />;
}
return (
@@ -63,6 +74,87 @@ export function CommandPaletteResults(props: CommandPaletteResultsProps) {
);
}
+/**
+ * Scrolls a keyboard highlight into view the way the unvirtualized list did:
+ * nearest edge, clear of the scroll fade. Rows outside the rendered window
+ * fall back to the list's own scrolling.
+ */
+export function scrollCommandPaletteRowIntoView(list: LegendListRef | null, rowIndex: number) {
+ const element = list?.getState?.().elementAtIndex(rowIndex);
+ if (element instanceof HTMLElement) {
+ element.scrollIntoView({ block: "nearest" });
+ return;
+ }
+ void list?.scrollIndexIntoView?.({ index: rowIndex, animated: false });
+}
+
+interface CommandPaletteVirtualizedResultsProps extends Omit<CommandPaletteResultsProps, "groups"> {
+ rows: ReadonlyArray<CommandPaletteRow>;
+ listRef: RefObject<LegendListRef | null>;
+}
+
+/**
+ * Renders only the visible rows. The parent passes the flat item order to the
+ * Command root as `items` with `virtualized`, scrolls keyboard highlights into
+ * view through `listRef`, and runs Enter itself since the row may be unmounted.
+ */
+export function CommandPaletteVirtualizedResults(props: CommandPaletteVirtualizedResultsProps) {
+ const { listRef } = props;
+ const [scrollFade, setScrollFade] = useState({ top: false, bottom: false });
+ const updateScrollFade = useCallback(() => {
+ const scrollElement = listRef.current?.getScrollableNode?.();
+ if (!(scrollElement instanceof HTMLElement)) return;
+ const top = scrollElement.scrollTop > 1;
+ const bottom =
+ scrollElement.scrollHeight - scrollElement.clientHeight - scrollElement.scrollTop > 1;
+ setScrollFade((current) =>
+ current.top === top && current.bottom === bottom ? current : { top, bottom },
+ );
+ }, [listRef]);
+
+ if (props.rows.length === 0) {
+ return <CommandPaletteEmptyState {...props} />;
+ }
+
+ return (
+ <CommandListVirtualized>
+ <LegendList<CommandPaletteRow>
+ ref={listRef}
+ data={props.rows}
+ keyExtractor={(row) => row.key}
+ getItemType={(row) => row.kind}
+ extraData={props.highlightedItemValue}
+ renderItem={({ item: row }) =>
+ row.kind === "label" ? (
+ <div className={row.first ? undefined : "pt-1.5"}>
+ <CommandListHeading>{row.label}</CommandListHeading>
+ </div>
+ ) : row.itemIndex === null ? (
+ <DisabledCommandPaletteResultRow item={row.item} />
+ ) : (
+ <CommandPaletteResultRow
+ index={row.itemIndex}
+ item={row.item}
+ keybindings={props.keybindings}
+ isActive={props.highlightedItemValue === row.item.value}
+ onExecuteItem={props.onExecuteItem}
+ />
+ )
+ }
+ estimatedItemSize={40}
+ drawDistance={400}
+ onLayout={updateScrollFade}
+ onScroll={updateScrollFade}
+ contentContainerClassName="px-2"
+ className={cn(
+ "min-h-0 scroll-py-6 overflow-x-hidden overscroll-y-contain py-2",
+ getVirtualizedScrollFadeClassName(scrollFade),
+ )}
+ />
+ </CommandListVirtualized>
+ );
+}
+
function DisabledCommandPaletteResultRow(props: {
item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
}) {
@@ -96,6 +188,7 @@ function DisabledCommandPaletteResultRow(props: {
}
function CommandPaletteResultRow(props: {
+ index?: number;
item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
isActive: boolean;
keybindings: ResolvedKeybindingsConfig;
@@ -107,6 +200,7 @@ function CommandPaletteResultRow(props: {
return (
<CommandItem
+ {...(props.index === undefined ? {} : { index: props.index })}
value={props.item.value}
active={props.isActive}
onMouseDown={(event) => {
diff --git a/apps/web/src/components/ui/autocomplete.tsx b/apps/web/src/components/ui/autocomplete.tsx
index 8251d8c24b7..548efd94489 100644
--- a/apps/web/src/components/ui/autocomplete.tsx
+++ b/apps/web/src/components/ui/autocomplete.tsx
@@ -146,11 +146,35 @@ function AutocompleteGroup({ className, ...props }: AutocompletePrimitive.Group.
);
}
+const GROUP_LABEL_CLASS_NAME = "px-2 py-1.5 font-medium text-muted-foreground text-xs";
+
function AutocompleteGroupLabel({ className, ...props }: AutocompletePrimitive.GroupLabel.Props) {
return (
<AutocompletePrimitive.GroupLabel
- className={cn("px-2 py-1.5 font-medium text-muted-foreground text-xs", className)}
+ className={cn(GROUP_LABEL_CLASS_NAME, className)}
+ data-slot="autocomplete-group-label"
+ {...props}
+ />
+ );
+}
+
+// A group label for virtualized lists, where rows cannot be wrapped in a group.
+function AutocompleteListHeading({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ className={cn(GROUP_LABEL_CLASS_NAME, className)}
data-slot="autocomplete-group-label"
+ role="presentation"
+ {...props}
+ />
+ );
+}
+
+function AutocompleteListVirtualized({ className, ...props }: AutocompletePrimitive.List.Props) {
+ return (
+ <AutocompletePrimitive.List
+ className={cn("size-full min-w-0", className)}
+ data-slot="autocomplete-list"
{...props}
/>
);
@@ -226,6 +250,8 @@ export {
AutocompleteGroupLabel,
AutocompleteEmpty,
AutocompleteList,
+ AutocompleteListHeading,
+ AutocompleteListVirtualized,
AutocompleteClear,
AutocompleteCollection,
};
diff --git a/apps/web/src/components/ui/command.tsx b/apps/web/src/components/ui/command.tsx
index d3a83865151..b3a473ba1d2 100644
--- a/apps/web/src/components/ui/command.tsx
+++ b/apps/web/src/components/ui/command.tsx
@@ -12,6 +12,8 @@ import {
AutocompleteInput,
AutocompleteItem,
AutocompleteList,
+ AutocompleteListHeading,
+ AutocompleteListVirtualized,
} from "~/components/ui/autocomplete";
import { DIALOG_BACKDROP_CLASS, DIALOG_POPUP_CLASS } from "~/components/ui/dialog-styles";
import { Button } from "~/components/ui/button";
@@ -123,6 +125,29 @@ function CommandList({ className, ...props }: React.ComponentProps<typeof Autoco
);
}
+// Pair with a LegendList child; the list owns scrolling and spacing.
+function CommandListVirtualized({
+ className,
+ ...props
+}: React.ComponentProps<typeof AutocompleteListVirtualized>) {
+ return (
+ <AutocompleteListVirtualized
+ className={cn("flex max-h-[inherit] flex-col", className)}
+ data-slot="command-list"
+ {...props}
+ />
+ );
+}
+
+function CommandListHeading({
+ className,
+ ...props
+}: React.ComponentProps<typeof AutocompleteListHeading>) {
+ return (
+ <AutocompleteListHeading className={className} data-slot="command-group-label" {...props} />
+ );
+}
+
function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
@@ -229,6 +254,8 @@ export {
CommandInput,
CommandItem,
CommandList,
+ CommandListHeading,
+ CommandListVirtualized,
CommandPanel,
CommandShortcut,
};
diff --git a/apps/web/src/state/queries.threadSearch.test.tsx b/apps/web/src/state/queries.threadSearch.test.tsx
new file mode 100644
index 00000000000..82ee2acd544
--- /dev/null
+++ b/apps/web/src/state/queries.threadSearch.test.tsx
@@ -0,0 +1,64 @@
+import { EnvironmentId } from "@t3tools/contracts";
+import { act } from "react";
+import { create, type ReactTestRenderer } from "react-test-renderer";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+
+import { useThreadSearch } from "./queries";
+
+const searchResults = vi.hoisted(() => ({
+ matches: [{ threadId: "thread-1", source: "user", snippet: "a link" }],
+ isLoading: false,
+}));
+vi.mock("@effect/atom-react", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@effect/atom-react")>()),
+ useAtomValue: (atom: { label?: ReadonlyArray<unknown> }) =>
+ String(atom.label?.[0]).includes("empty") ? { matches: [], isLoading: false } : searchResults,
+}));
+
+const environmentIds = [EnvironmentId.make("local")];
+type ThreadSearch = ReturnType<typeof useThreadSearch>;
+
+function Probe(props: { query: string; onResult: (result: ThreadSearch) => void }) {
+ props.onResult(useThreadSearch(environmentIds, props.query));
+ return null;
+}
+
+describe("useThreadSearch", () => {
+ let renderer: ReactTestRenderer | null = null;
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.stubGlobal("window", globalThis);
+ });
+ afterEach(() => {
+ act(() => renderer?.unmount());
+ renderer = null;
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ });
+
+ it("keeps the settled query and matches stable while typing, then reports the settled query", () => {
+ const results: ThreadSearch[] = [];
+ const render = (query: string) => {
+ const element = <Probe query={query} onResult={(result) => results.push(result)} />;
+ act(() => {
+ if (renderer) renderer.update(element);
+ else renderer = create(element);
+ });
+ return results.at(-1)!;
+ };
+
+ render("");
+ const typing = ["li", "lin", "link "].map(render);
+ expect(typing.map((result) => result.query)).toEqual(["", "", ""]);
+ expect(new Set(typing.map((result) => result.matches)).size).toBe(1);
+ expect(typing.every((result) => result.isPending)).toBe(true);
+
+ act(() => {
+ vi.advanceTimersByTime(200);
+ });
+ const settled = results.at(-1)!;
+ expect(settled.query).toBe("link");
+ expect(settled.matches).toBe(searchResults.matches);
+ expect(settled.isPending).toBe(false);
+ });
+});
diff --git a/apps/web/src/state/queries.ts b/apps/web/src/state/queries.ts
index 15211efab4e..01bfa8864bb 100644
--- a/apps/web/src/state/queries.ts
+++ b/apps/web/src/state/queries.ts
@@ -72,6 +72,8 @@ export function useThreadSearch(
query: string,
): {
readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
+ /** The settled query `matches` came from; it only changes when results do. */
+ readonly query: string;
readonly isPending: boolean;
} {
const normalizedQuery = query.trim();
@@ -88,6 +90,7 @@ export function useThreadSearch(
const isDebouncing = canSearch && normalizedQuery !== debouncedQuery;
return {
matches: isDebouncing ? EMPTY_THREAD_SEARCH_MATCHES : result.matches,
+ query: settledQuery ?? "",
isPending: canSearch && (isDebouncing || result.isLoading),
};
}