-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(query-builder): Add basic support for selecting multiple values #71457
Merged
malwilley
merged 3 commits into
master
from
malwilley/feat/search-query-builder-multiple-values
May 28, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ import {type Reducer, useCallback, useReducer} from 'react'; | |||||
import { | ||||||
type ParseResultToken, | ||||||
TermOperator, | ||||||
type Token, | ||||||
Token, | ||||||
type TokenResult, | ||||||
} from 'sentry/components/searchSyntax/parser'; | ||||||
import {stringifyToken} from 'sentry/components/searchSyntax/utils'; | ||||||
|
@@ -35,11 +35,24 @@ type UpdateTokenValueAction = { | |||||
value: string; | ||||||
}; | ||||||
|
||||||
type MultiSelectFilterValueAction = { | ||||||
token: TokenResult<Token.FILTER>; | ||||||
type: 'TOGGLE_FILTER_VALUE'; | ||||||
value: string; | ||||||
}; | ||||||
|
||||||
type DeleteLastMultiSelectFilterValueAction = { | ||||||
token: TokenResult<Token.FILTER>; | ||||||
type: 'DELETE_LAST_MULTI_SELECT_FILTER_VALUE'; | ||||||
}; | ||||||
|
||||||
export type QueryBuilderActions = | ||||||
| DeleteTokenAction | ||||||
| UpdateFreeTextAction | ||||||
| UpdateFilterOpAction | ||||||
| UpdateTokenValueAction; | ||||||
| UpdateTokenValueAction | ||||||
| MultiSelectFilterValueAction | ||||||
| DeleteLastMultiSelectFilterValueAction; | ||||||
|
||||||
function removeQueryToken(query: string, token: TokenResult<Token>): string { | ||||||
return ( | ||||||
|
@@ -101,6 +114,70 @@ function updateFreeText( | |||||
}; | ||||||
} | ||||||
|
||||||
function updateFilterMultipleValues( | ||||||
state: QueryBuilderState, | ||||||
token: TokenResult<Token.FILTER>, | ||||||
values: string[] | ||||||
) { | ||||||
const uniqNonEmptyValues = Array.from( | ||||||
new Set(values.filter(value => value.length > 0)) | ||||||
); | ||||||
if (uniqNonEmptyValues.length === 0) { | ||||||
return {...state, query: replaceQueryToken(state.query, token.value, '')}; | ||||||
} | ||||||
|
||||||
const newValue = | ||||||
uniqNonEmptyValues.length > 1 | ||||||
? `[${uniqNonEmptyValues.join(',')}]` | ||||||
: uniqNonEmptyValues[0]; | ||||||
|
||||||
return {...state, query: replaceQueryToken(state.query, token.value, newValue)}; | ||||||
} | ||||||
|
||||||
function mutliSelectTokenValue( | ||||||
state: QueryBuilderState, | ||||||
action: MultiSelectFilterValueAction | ||||||
) { | ||||||
const tokenValue = action.token.value; | ||||||
|
||||||
switch (tokenValue.type) { | ||||||
case Token.VALUE_TEXT_LIST: | ||||||
case Token.VALUE_NUMBER_LIST: | ||||||
const values = tokenValue.items.map(item => item.value?.text ?? ''); | ||||||
const containsValue = values.includes(action.value); | ||||||
const newValues = containsValue | ||||||
? values.filter(value => value !== action.value) | ||||||
: [...values, action.value]; | ||||||
|
||||||
return updateFilterMultipleValues(state, action.token, newValues); | ||||||
default: | ||||||
if (tokenValue.text === action.value) { | ||||||
return updateFilterMultipleValues(state, action.token, ['']); | ||||||
} | ||||||
return updateFilterMultipleValues(state, action.token, [ | ||||||
tokenValue.text, | ||||||
action.value, | ||||||
]); | ||||||
} | ||||||
} | ||||||
|
||||||
function deleteLastMultiSelectTokenValue( | ||||||
state: QueryBuilderState, | ||||||
action: DeleteLastMultiSelectFilterValueAction | ||||||
) { | ||||||
const tokenValue = action.token.value; | ||||||
|
||||||
switch (tokenValue.type) { | ||||||
case Token.VALUE_TEXT_LIST: | ||||||
case Token.VALUE_NUMBER_LIST: | ||||||
const newValues = tokenValue.items.slice(0, -1).map(item => item.value?.text ?? ''); | ||||||
|
||||||
return updateFilterMultipleValues(state, action.token, newValues); | ||||||
default: | ||||||
return updateFilterMultipleValues(state, action.token, ['']); | ||||||
} | ||||||
} | ||||||
|
||||||
export function useQueryBuilderState({initialQuery}: {initialQuery: string}) { | ||||||
const initialState: QueryBuilderState = {query: initialQuery}; | ||||||
|
||||||
|
@@ -121,6 +198,10 @@ export function useQueryBuilderState({initialQuery}: {initialQuery: string}) { | |||||
return { | ||||||
query: replaceQueryToken(state.query, action.token, action.value), | ||||||
}; | ||||||
case 'TOGGLE_FILTER_VALUE': | ||||||
return mutliSelectTokenValue(state, action); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
case 'DELETE_LAST_MULTI_SELECT_FILTER_VALUE': | ||||||
return deleteLastMultiSelectTokenValue(state, action); | ||||||
default: | ||||||
return state; | ||||||
} | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last one I promise
(One usage of this function below too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I keep messing this up hah