-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make autocomplete work with entity ids #51633
Open
Kicu
wants to merge
5
commits into
Expensify:main
Choose a base branch
from
software-mansion-labs:kicu/autocomplete-query-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+658
−235
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b93c8c8
Add function for generating new query with autocomplete values
Kicu 12987cd
Add autocomplete working for from/to and chat rooms
Kicu db340d7
Add autocomplete working for taxRates and taxes from policy
Kicu 217d562
Add autocomplete for cardIDs and correctly compute backend query
Kicu e3e559b
Fix autocomplete for contextual search item
Kicu 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
src/components/Search/SearchRouter/getQueryWithSubstitutions.ts
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type {SearchAutocompleteQueryRange} from '@components/Search/types'; | ||
import * as parser from '@libs/SearchParser/autocompleteParser'; | ||
|
||
type SubstitutionEntry = {value: string}; | ||
type SubstitutionMap = Record<string, SubstitutionEntry>; | ||
|
||
const getSubstitutionMapKey = (filterName: string, value: string) => `${filterName}:${value}`; | ||
|
||
/** | ||
* Given a plaintext query and a SubstitutionMap object, this function will return a transformed query where: | ||
* - any autocomplete mention in the original query will be substituted with an id taken from `substitutions` object | ||
* - anything that does not match will stay as is | ||
* | ||
* Ex: | ||
* query: `A from:@johndoe A` | ||
* substitutions: { | ||
* from:@johndoe: 9876 | ||
* } | ||
* return: `A from:9876 A` | ||
*/ | ||
function getQueryWithSubstitutions(changedQuery: string, substitutions: SubstitutionMap) { | ||
const parsed = parser.parse(changedQuery) as {ranges: SearchAutocompleteQueryRange[]}; | ||
|
||
const searchAutocompleteQueryRanges = parsed.ranges; | ||
|
||
if (searchAutocompleteQueryRanges.length === 0) { | ||
return changedQuery; | ||
} | ||
|
||
let resultQuery = changedQuery; | ||
let lengthDiff = 0; | ||
|
||
for (const range of searchAutocompleteQueryRanges) { | ||
const itemKey = getSubstitutionMapKey(range.key, range.value); | ||
const substitutionEntry = substitutions[itemKey]; | ||
|
||
if (substitutionEntry) { | ||
const substitutionStart = range.start + lengthDiff; | ||
const substitutionEnd = range.start + range.length; | ||
|
||
// generate new query but substituting "user-typed" value with the entity id/email from substitutions | ||
resultQuery = resultQuery.slice(0, substitutionStart) + substitutionEntry.value + changedQuery.slice(substitutionEnd); | ||
lengthDiff = lengthDiff + substitutionEntry.value.length - range.length; | ||
} | ||
} | ||
|
||
return resultQuery; | ||
} | ||
|
||
export {getQueryWithSubstitutions, getSubstitutionMapKey}; | ||
export type {SubstitutionMap}; |
43 changes: 43 additions & 0 deletions
43
src/components/Search/SearchRouter/getUpdatedSubstitutionsMap.ts
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type {SearchAutocompleteQueryRange} from '@components/Search/types'; | ||
import * as parser from '@libs/SearchParser/autocompleteParser'; | ||
import type {SubstitutionMap} from './getQueryWithSubstitutions'; | ||
|
||
const getSubstitutionsKey = (filterName: string, value: string) => `${filterName}:${value}`; | ||
|
||
/** | ||
* Given a plaintext query and a SubstitutionMap object, | ||
* this function will remove any substitution keys that do not appear in the query and return an updated object | ||
* | ||
* Ex: | ||
* query: `Test from:John1` | ||
* substitutions: { | ||
* from:SomeOtherJohn: 12345 | ||
* } | ||
* return: {} | ||
*/ | ||
function getUpdatedSubstitutionsMap(query: string, substitutions: SubstitutionMap): SubstitutionMap { | ||
const parsedQuery = parser.parse(query) as {ranges: SearchAutocompleteQueryRange[]}; | ||
|
||
const searchAutocompleteQueryRanges = parsedQuery.ranges; | ||
|
||
if (searchAutocompleteQueryRanges.length === 0) { | ||
return {}; | ||
} | ||
|
||
const autocompleteQueryKeys = searchAutocompleteQueryRanges.map((range) => getSubstitutionsKey(range.key, range.value)); | ||
|
||
// Build a new substitutions map consisting of only the keys from old map, that appear in query | ||
const updatedSubstitutionMap = autocompleteQueryKeys.reduce((map, key) => { | ||
if (substitutions[key]) { | ||
// eslint-disable-next-line no-param-reassign | ||
map[key] = substitutions[key]; | ||
} | ||
|
||
return map; | ||
}, {} as SubstitutionMap); | ||
|
||
return updatedSubstitutionMap; | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export {getUpdatedSubstitutionsMap}; |
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
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.
Do we need the value key here? AFAIK we always have the map as
{[filterKey:typedValue]: {value: [id]}}
. Could we simplify it to{[filterKey:typedValue]: [id]}
?