An open source design system that's fully customizable and agent ready
TypeScript
fix(CommandPalette): discard in-flight search responses on close (#3896)
Shahrier Islam Arhame18b6dba7ec0
diff --git a/.changeset/commandpalette-stale-close.md b/.changeset/commandpalette-stale-close.mdnew file mode 100644index 0000000000..15c7dcd27a--- /dev/null+++ b/.changeset/commandpalette-stale-close.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] CommandPalette: discard in-flight search responses when the palette closes++Closing the palette while a search was still in flight let the late response re-commit the abandoned query and results into the closed palette, which showed up as a ghost query on reopen. Closing now invalidates any pending request.+@arham766diff --git a/packages/core/src/CommandPalette/CommandPalette.test.tsx b/packages/core/src/CommandPalette/CommandPalette.test.tsxindex 06a548b102..68951e9e4a 100644--- a/packages/core/src/CommandPalette/CommandPalette.test.tsx+++ b/packages/core/src/CommandPalette/CommandPalette.test.tsx@@ -9,6 +9,7 @@import {describe, it, expect, vi, beforeEach} from 'vitest';import {render, screen, waitFor, fireEvent, act} from '@testing-library/react';+import {useState} from 'react';import {CommandPalette} from './CommandPalette';import {createStaticSource} from '@astryxdesign/core/Typeahead';import type {SearchSource, SearchableItem} from '@astryxdesign/core/Typeahead';@@ -274,6 +275,59 @@ describe('CommandPalette', () => {);});+ it('discards a search response that resolves after the palette closed', async () => {+ // A source whose search resolves only when released, and no cancel()+ // implementation — closing the palette must invalidate the request itself.+ const resolvers: ((items: SearchableItem[]) => void)[] = [];+ const source: SearchSource = {+ bootstrap: () => [],+ async search(): Promise<SearchableItem[]> {+ return new Promise<SearchableItem[]>(resolve => {+ resolvers.push(resolve);+ });+ },+ };++ function Harness() {+ const [isOpen, setIsOpen] = useState(true);+ return (+ <CommandPalette+ isOpen={isOpen}+ onOpenChange={setIsOpen}+ searchSource={source}+ />+ );+ }+ render(<Harness />);++ const input = screen.getByRole('combobox');+ fireEvent.change(input, {target: {value: 'a'}});+ await waitFor(() => expect(resolvers).toHaveLength(1));++ // Close while the search is still in flight.+ const dialog = screen.getByRole('dialog');+ const escapeEvent = new KeyboardEvent('keydown', {+ key: 'Escape',+ bubbles: true,+ cancelable: true,+ });+ dialog.dispatchEvent(escapeEvent);+ await waitFor(() =>+ expect(screen.getByRole('dialog', {hidden: true})).not.toHaveAttribute(+ 'open',+ ),+ );++ // The stale response arrives after close — it must not re-commit the+ // abandoned query/results into the closed palette.+ await act(async () => {+ resolvers[0]([{id: 'stale', label: 'Stale item'}]);+ });++ expect(screen.getByRole('combobox', {hidden: true})).toHaveValue('');+ expect(screen.queryByText('Stale item')).not.toBeInTheDocument();+ });+it('does not move the highlight while typing in the search input', async () => {// The palette's input already filters; letting the shared combobox// typeahead also chase prefixes would drag the highlight — and Enter —diff --git a/packages/core/src/CommandPalette/CommandPalette.tsx b/packages/core/src/CommandPalette/CommandPalette.tsxindex 110775fbeb..87371fd1cc 100644--- a/packages/core/src/CommandPalette/CommandPalette.tsx+++ b/packages/core/src/CommandPalette/CommandPalette.tsx@@ -319,6 +319,11 @@ export function CommandPalette<T extends SearchableItem = SearchableItem>({);const handleClose = useCallback(() => {+ // Invalidate any in-flight search. Most sources don't implement cancel(),+ // and a response that resolves after close would still pass runSearch's+ // version check and re-commit the stale query/results into the closed+ // palette (visible as a ghost query on reopen while bootstrap is pending).+ searchVersionRef.current++;// Reset both committed and optimistic search on closesetSearch('');setSearchResults([]);