Skip to content

Commit 7faa530

Browse files
authored
fix(query-builder): Switch to another section when the selected one no longer exists (#82385)
If you rerender the same search bar with different sections, it will keep the old section selected even if it doesn't exist. This fixes that.
1 parent 70f801a commit 7faa530

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

static/app/components/searchQueryBuilder/index.spec.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,35 @@ describe('SearchQueryBuilder', function () {
520520
).toBeInTheDocument();
521521
});
522522

523+
it('switches to keys menu when recent searches no longer exist', async function () {
524+
const {rerender} = render(
525+
<SearchQueryBuilder
526+
{...defaultProps}
527+
recentSearches={SavedSearchType.ISSUE}
528+
initialQuery=""
529+
/>
530+
);
531+
532+
await userEvent.click(getLastInput());
533+
534+
// Recent should be selected
535+
expect(screen.getByRole('button', {name: 'Recent'})).toHaveAttribute(
536+
'aria-selected',
537+
'true'
538+
);
539+
540+
// Rerender without recent searches
541+
rerender(<SearchQueryBuilder {...defaultProps} />);
542+
543+
// Recent should not exist anymore
544+
expect(screen.queryByRole('button', {name: 'Recent'})).not.toBeInTheDocument();
545+
// All should be selected
546+
expect(screen.getByRole('button', {name: 'All'})).toHaveAttribute(
547+
'aria-selected',
548+
'true'
549+
);
550+
});
551+
523552
it('when selecting a recent search, should reset query and call onSearch', async function () {
524553
const mockOnSearch = jest.fn();
525554
const mockCreateRecentSearch = MockApiClient.addMockResponse({

static/app/components/searchQueryBuilder/tokens/filterKeyListBox/index.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ function useHighlightFirstOptionOnSectionChange({
178178
]);
179179
}
180180

181+
// If the selected section no longer exists, switch to the first valid section
182+
function useSwitchToValidSection({
183+
sections,
184+
selectedSection,
185+
setSelectedSection,
186+
}: {
187+
sections: Section[];
188+
selectedSection: Key | null;
189+
setSelectedSection: (section: string) => void;
190+
}) {
191+
useEffect(() => {
192+
if (!selectedSection || !sections.length) {
193+
return;
194+
}
195+
196+
const section = sections.find(s => s.value === selectedSection);
197+
if (!section) {
198+
setSelectedSection(sections[0].value);
199+
}
200+
}, [sections, selectedSection, setSelectedSection]);
201+
}
202+
181203
function FilterKeyMenuContent<T extends SelectOptionOrSectionWithKey<string>>({
182204
recentFilters,
183205
selectedSection,
@@ -287,6 +309,8 @@ export function FilterKeyListBox<T extends SelectOptionOrSectionWithKey<string>>
287309
isOpen,
288310
});
289311

312+
useSwitchToValidSection({sections, selectedSection, setSelectedSection});
313+
290314
const fullWidth = !query;
291315
const showDetailsPane = fullWidth && selectedSection !== RECENT_SEARCH_CATEGORY_VALUE;
292316

0 commit comments

Comments
 (0)