-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(dashboards): Add global filter selector component #101196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ahmed-Labs
merged 6 commits into
master
from
ahmedmo/feat/dashboard-global-filters-component
Oct 9, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f133ee
create global filter selector component
Ahmed-Labs 1dd9af8
unit test global filter selector
Ahmed-Labs f5dbaad
add empty message placeholder to indicate filter values being fetched
Ahmed-Labs b6c3bb3
remove unused imports
Ahmed-Labs f88c2a5
add filter selector placeholder to filters bar
Ahmed-Labs 4acf4b5
add comments to global filter type for clarity
Ahmed-Labs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
static/app/views/dashboards/globalFilter/filterSelector.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; | ||
|
||
import {FieldKind} from 'sentry/utils/fields'; | ||
import FilterSelector from 'sentry/views/dashboards/globalFilter/filterSelector'; | ||
import {WidgetType, type GlobalFilter} from 'sentry/views/dashboards/types'; | ||
|
||
describe('FilterSelector', () => { | ||
const mockOnUpdateFilter = jest.fn(); | ||
const mockOnRemoveFilter = jest.fn(); | ||
|
||
const mockGlobalFilter: GlobalFilter = { | ||
dataset: WidgetType.ERRORS, | ||
tag: { | ||
key: 'browser', | ||
name: 'Browser', | ||
kind: FieldKind.FIELD, | ||
}, | ||
value: '', | ||
}; | ||
beforeEach(() => { | ||
MockApiClient.clearMockResponses(); | ||
|
||
// Mock tags endpoint | ||
MockApiClient.addMockResponse({ | ||
url: '/organizations/org-slug/tags/', | ||
body: [], | ||
}); | ||
|
||
// Mock custom measurements endpoint | ||
MockApiClient.addMockResponse({ | ||
url: '/organizations/org-slug/measurements-meta/', | ||
body: {}, | ||
}); | ||
|
||
// Mock tag values endpoint | ||
MockApiClient.addMockResponse({ | ||
url: `/organizations/org-slug/tags/${mockGlobalFilter.tag.key}/values/`, | ||
body: [ | ||
{name: 'chrome', value: 'chrome', count: 100}, | ||
{name: 'firefox', value: 'firefox', count: 50}, | ||
{name: 'safari', value: 'safari', count: 25}, | ||
], | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders all filter values', async () => { | ||
render( | ||
<FilterSelector | ||
globalFilter={mockGlobalFilter} | ||
onUpdateFilter={mockOnUpdateFilter} | ||
onRemoveFilter={mockOnRemoveFilter} | ||
/> | ||
); | ||
|
||
const button = screen.getByRole('button', {name: mockGlobalFilter.tag.key + ':'}); | ||
await userEvent.click(button); | ||
|
||
expect(screen.getByText('chrome')).toBeInTheDocument(); | ||
expect(screen.getByText('firefox')).toBeInTheDocument(); | ||
expect(screen.getByText('safari')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onUpdateFilter when options are selected', async () => { | ||
render( | ||
<FilterSelector | ||
globalFilter={mockGlobalFilter} | ||
onUpdateFilter={mockOnUpdateFilter} | ||
onRemoveFilter={mockOnRemoveFilter} | ||
/> | ||
); | ||
|
||
const button = screen.getByRole('button', {name: mockGlobalFilter.tag.key + ':'}); | ||
await userEvent.click(button); | ||
|
||
await userEvent.click(screen.getByRole('checkbox', {name: 'Select firefox'})); | ||
await userEvent.click(screen.getByRole('checkbox', {name: 'Select chrome'})); | ||
await userEvent.click(screen.getByRole('button', {name: 'Apply'})); | ||
|
||
expect(mockOnUpdateFilter).toHaveBeenCalledWith({ | ||
...mockGlobalFilter, | ||
value: 'browser:[firefox,chrome]', | ||
}); | ||
|
||
await userEvent.click(button); | ||
await userEvent.click(screen.getByRole('row', {name: 'chrome'})); | ||
|
||
expect(mockOnUpdateFilter).toHaveBeenCalledWith({ | ||
...mockGlobalFilter, | ||
value: 'browser:[chrome]', | ||
}); | ||
}); | ||
|
||
it('parses the initial value of the global filter', async () => { | ||
render( | ||
<FilterSelector | ||
globalFilter={{...mockGlobalFilter, value: 'browser:[firefox,chrome]'}} | ||
onUpdateFilter={mockOnUpdateFilter} | ||
onRemoveFilter={mockOnRemoveFilter} | ||
/> | ||
); | ||
|
||
const button = screen.getByRole('button', {name: mockGlobalFilter.tag.key + ':'}); | ||
await userEvent.click(button); | ||
|
||
expect(screen.getByRole('checkbox', {name: 'Select firefox'})).toBeChecked(); | ||
expect(screen.getByRole('checkbox', {name: 'Select chrome'})).toBeChecked(); | ||
}); | ||
|
||
it('calls onRemoveFilter when remove button is clicked', async () => { | ||
render( | ||
<FilterSelector | ||
globalFilter={mockGlobalFilter} | ||
onUpdateFilter={mockOnUpdateFilter} | ||
onRemoveFilter={mockOnRemoveFilter} | ||
/> | ||
); | ||
|
||
const button = screen.getByRole('button', {name: mockGlobalFilter.tag.key + ':'}); | ||
await userEvent.click(button); | ||
await userEvent.click(screen.getByRole('button', {name: 'Remove Filter'})); | ||
|
||
expect(mockOnRemoveFilter).toHaveBeenCalledWith(mockGlobalFilter); | ||
}); | ||
}); |
129 changes: 129 additions & 0 deletions
129
static/app/views/dashboards/globalFilter/filterSelector.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import {useMemo, useState} from 'react'; | ||
|
||
import {Button} from 'sentry/components/core/button'; | ||
import {HybridFilter} from 'sentry/components/organizations/hybridFilter'; | ||
import {MutableSearch} from 'sentry/components/searchSyntax/mutableSearch'; | ||
import {t} from 'sentry/locale'; | ||
import {keepPreviousData, useQuery} from 'sentry/utils/queryClient'; | ||
import {useDebouncedValue} from 'sentry/utils/useDebouncedValue'; | ||
import usePageFilters from 'sentry/utils/usePageFilters'; | ||
import {getDatasetConfig} from 'sentry/views/dashboards/datasetConfig/base'; | ||
import {getDatasetLabel} from 'sentry/views/dashboards/globalFilter/addFilter'; | ||
import FilterSelectorTrigger from 'sentry/views/dashboards/globalFilter/filterSelectorTrigger'; | ||
import type {GlobalFilter} from 'sentry/views/dashboards/types'; | ||
|
||
type FilterSelectorProps = { | ||
globalFilter: GlobalFilter; | ||
onRemoveFilter: (filter: GlobalFilter) => void; | ||
onUpdateFilter: (filter: GlobalFilter) => void; | ||
}; | ||
|
||
function FilterSelector({ | ||
globalFilter, | ||
onRemoveFilter, | ||
onUpdateFilter, | ||
}: FilterSelectorProps) { | ||
// Parse global filter condition to retrieve initial state | ||
const initialValues = useMemo(() => { | ||
const mutableSearch = new MutableSearch(globalFilter.value); | ||
return mutableSearch.getFilterValues(globalFilter.tag.key); | ||
}, [globalFilter]); | ||
|
||
const [activeFilterValues, setActiveFilterValues] = useState<string[]>(initialValues); | ||
|
||
const {dataset, tag} = globalFilter; | ||
const {selection} = usePageFilters(); | ||
const dataProvider = getDatasetConfig(dataset).useSearchBarDataProvider!({ | ||
pageFilters: selection, | ||
}); | ||
|
||
const baseQueryKey = useMemo(() => ['global-dashboard-filters-tag-values', tag], [tag]); | ||
const queryKey = useDebouncedValue(baseQueryKey); | ||
|
||
const queryResult = useQuery<string[]>({ | ||
// Disable exhaustive deps because we want to debounce the query key above | ||
// eslint-disable-next-line @tanstack/query/exhaustive-deps | ||
queryKey, | ||
queryFn: async () => { | ||
const result = await dataProvider?.getTagValues(tag, ''); | ||
return result ?? []; | ||
}, | ||
placeholderData: keepPreviousData, | ||
enabled: true, | ||
}); | ||
|
||
const {data, isFetching} = queryResult; | ||
const options = useMemo(() => { | ||
if (!data) return []; | ||
return data.map(value => ({ | ||
label: value, | ||
value, | ||
})); | ||
}, [data]); | ||
|
||
const handleChange = (opts: string[]) => { | ||
setActiveFilterValues(opts); | ||
|
||
// Build filter condition string | ||
const filterValue = () => { | ||
if (opts.length === 0) { | ||
return ''; | ||
} | ||
const mutableSearch = new MutableSearch(''); | ||
return mutableSearch.addFilterValueList(tag.key, opts).toString(); | ||
}; | ||
|
||
onUpdateFilter({ | ||
...globalFilter, | ||
value: filterValue(), | ||
}); | ||
}; | ||
|
||
return ( | ||
<HybridFilter | ||
checkboxPosition="leading" | ||
searchable | ||
disabled={false} | ||
options={options} | ||
value={activeFilterValues} | ||
defaultValue={[]} | ||
onChange={handleChange} | ||
sizeLimit={10} | ||
sizeLimitMessage={t('Use search to find more filter values…')} | ||
onReset={() => { | ||
setActiveFilterValues([]); | ||
onUpdateFilter({ | ||
...globalFilter, | ||
value: '', | ||
}); | ||
}} | ||
emptyMessage={ | ||
isFetching ? t('Loading filter values...') : t('No filter values found') | ||
} | ||
menuTitle={t('%s filter', getDatasetLabel(dataset))} | ||
menuHeaderTrailingItems={ | ||
<Button | ||
aria-label={t('Remove Filter')} | ||
borderless | ||
size="xs" | ||
priority="link" | ||
onClick={() => onRemoveFilter(globalFilter)} | ||
> | ||
{t('Remove')} | ||
</Button> | ||
} | ||
triggerProps={{ | ||
children: ( | ||
<FilterSelectorTrigger | ||
globalFilter={globalFilter} | ||
activeFilterValues={activeFilterValues} | ||
options={options} | ||
queryResult={queryResult} | ||
/> | ||
), | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export default FilterSelector; |
80 changes: 80 additions & 0 deletions
80
static/app/views/dashboards/globalFilter/filterSelectorTrigger.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import styled from '@emotion/styled'; | ||
|
||
import {Badge} from 'sentry/components/core/badge'; | ||
import type {SelectOption} from 'sentry/components/core/compactSelect/types'; | ||
import LoadingIndicator from 'sentry/components/loadingIndicator'; | ||
import TextOverflow from 'sentry/components/textOverflow'; | ||
import {t} from 'sentry/locale'; | ||
import {space} from 'sentry/styles/space'; | ||
import {prettifyTagKey} from 'sentry/utils/fields'; | ||
import type {UseQueryResult} from 'sentry/utils/queryClient'; | ||
import type {GlobalFilter} from 'sentry/views/dashboards/types'; | ||
|
||
type FilterSelectorTriggerProps = { | ||
activeFilterValues: string[]; | ||
globalFilter: GlobalFilter; | ||
options: Array<SelectOption<string>>; | ||
queryResult: UseQueryResult<string[], Error>; | ||
}; | ||
|
||
function FilterSelectorTrigger({ | ||
globalFilter, | ||
activeFilterValues, | ||
options, | ||
queryResult, | ||
}: FilterSelectorTriggerProps) { | ||
const {isFetching} = queryResult; | ||
const {tag} = globalFilter; | ||
|
||
const shouldShowBadge = | ||
!isFetching && | ||
activeFilterValues.length > 1 && | ||
activeFilterValues.length !== options.length; | ||
const isAllSelected = | ||
activeFilterValues.length === 0 || activeFilterValues.length === options.length; | ||
|
||
return ( | ||
<ButtonLabelWrapper> | ||
<TextOverflow> | ||
{prettifyTagKey(tag.key)}:{' '} | ||
{!isFetching && ( | ||
<span style={{fontWeight: 'normal'}}> | ||
{isAllSelected ? t('All') : activeFilterValues[0]} | ||
</span> | ||
)} | ||
</TextOverflow> | ||
{isFetching && <StyledLoadingIndicator size={14} />} | ||
{shouldShowBadge && ( | ||
<StyledBadge type="default">{`+${activeFilterValues.length - 1}`}</StyledBadge> | ||
)} | ||
</ButtonLabelWrapper> | ||
); | ||
} | ||
|
||
export default FilterSelectorTrigger; | ||
|
||
const StyledLoadingIndicator = styled(LoadingIndicator)` | ||
&& { | ||
margin: 0; | ||
margin-left: ${space(0.5)}; | ||
} | ||
`; | ||
|
||
const StyledBadge = styled(Badge)` | ||
flex-shrink: 0; | ||
height: 16px; | ||
line-height: 16px; | ||
min-width: 16px; | ||
border-radius: 16px; | ||
font-size: 10px; | ||
padding: 0 ${space(0.5)}; | ||
`; | ||
|
||
const ButtonLabelWrapper = styled('span')` | ||
width: 100%; | ||
text-align: left; | ||
align-items: center; | ||
display: inline-grid; | ||
grid-template-columns: 1fr auto; | ||
line-height: 1; | ||
`; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.