-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Highlight autocomplete value on a match #56243
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
Changes from all commits
0169b8e
29f98af
0cf5ebb
85d5435
944fbdd
7eb5861
74cade5
68b3262
d5acdee
068f04c
62b1617
d0f297d
56a5c8d
568d42f
ee8297b
efb018a
bf5abe0
f56ae47
3e3221f
11b4639
829f608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,27 @@ | ||
| import type {ForwardedRef, ReactNode, RefObject} from 'react'; | ||
| import React, {forwardRef, useLayoutEffect, useState} from 'react'; | ||
| import React, {forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useState} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import type {StyleProp, TextInputProps, ViewStyle} from 'react-native'; | ||
| import {useOnyx} from 'react-native-onyx'; | ||
| import {useSharedValue} from 'react-native-reanimated'; | ||
| import FormHelpMessage from '@components/FormHelpMessage'; | ||
| import type {SelectionListHandle} from '@components/SelectionList/types'; | ||
| import TextInput from '@components/TextInput'; | ||
| import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types'; | ||
| import useActiveWorkspace from '@hooks/useActiveWorkspace'; | ||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import {parseFSAttributes} from '@libs/Fullstory'; | ||
| import {parseForLiveMarkdown} from '@libs/SearchAutocompleteUtils'; | ||
| import runOnLiveMarkdownRuntime from '@libs/runOnLiveMarkdownRuntime'; | ||
| import {getAutocompleteCategories, getAutocompleteTags, parseForLiveMarkdown} from '@libs/SearchAutocompleteUtils'; | ||
| import handleKeyPress from '@libs/SearchInputOnKeyPress'; | ||
| import shouldDelayFocus from '@libs/shouldDelayFocus'; | ||
| import variables from '@styles/variables'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {SubstitutionMap} from './SearchRouter/getQueryWithSubstitutions'; | ||
|
|
||
| type SearchAutocompleteInputProps = { | ||
| /** Value of TextInput */ | ||
|
|
@@ -61,6 +65,9 @@ type SearchAutocompleteInputProps = { | |
|
|
||
| /** Whether the search reports API call is running */ | ||
| isSearchingForReports?: boolean; | ||
|
|
||
| /** Map of autocomplete suggestions. Required for highlighting to work properly */ | ||
| substitutionMap: SubstitutionMap; | ||
| } & Pick<TextInputProps, 'caretHidden' | 'autoFocus' | 'selection'>; | ||
|
|
||
| function SearchAutocompleteInput( | ||
|
|
@@ -82,20 +89,80 @@ function SearchAutocompleteInput( | |
| rightComponent, | ||
| isSearchingForReports, | ||
| selection, | ||
| substitutionMap, | ||
| }: SearchAutocompleteInputProps, | ||
| ref: ForwardedRef<BaseTextInputRef>, | ||
| ) { | ||
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
| const [isFocused, setIsFocused] = useState<boolean>(false); | ||
| const {isOffline} = useNetwork(); | ||
| const {activeWorkspaceID} = useActiveWorkspace(); | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
|
|
||
| const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST); | ||
| const currencyAutocompleteList = Object.keys(currencyList ?? {}); | ||
| const currencySharedValue = useSharedValue(currencyAutocompleteList); | ||
|
|
||
| const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES); | ||
| const categoryAutocompleteList = useMemo(() => { | ||
| return getAutocompleteCategories(allPolicyCategories, activeWorkspaceID); | ||
| }, [activeWorkspaceID, allPolicyCategories]); | ||
| const categorySharedValue = useSharedValue(categoryAutocompleteList); | ||
|
|
||
| const [allPoliciesTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS); | ||
| const tagAutocompleteList = useMemo(() => { | ||
| return getAutocompleteTags(allPoliciesTags, activeWorkspaceID); | ||
| }, [activeWorkspaceID, allPoliciesTags]); | ||
| const tagSharedValue = useSharedValue(tagAutocompleteList); | ||
|
|
||
| const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST); | ||
| const emailList = Object.keys(loginList ?? {}); | ||
| const emailListSharedValue = useSharedValue(emailList); | ||
|
|
||
| const offlineMessage: string = isOffline && shouldShowOfflineMessage ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : ''; | ||
|
|
||
| useEffect(() => { | ||
| runOnLiveMarkdownRuntime(() => { | ||
| 'worklet'; | ||
|
|
||
| emailListSharedValue.set(emailList); | ||
| })(); | ||
| }, [emailList, emailListSharedValue]); | ||
|
|
||
| useEffect(() => { | ||
| runOnLiveMarkdownRuntime(() => { | ||
| 'worklet'; | ||
|
|
||
| currencySharedValue.set(currencyAutocompleteList); | ||
| })(); | ||
| }, [currencyAutocompleteList, currencySharedValue]); | ||
|
|
||
| useEffect(() => { | ||
| runOnLiveMarkdownRuntime(() => { | ||
| 'worklet'; | ||
|
|
||
| categorySharedValue.set(categoryAutocompleteList); | ||
| })(); | ||
| }, [categorySharedValue, categoryAutocompleteList]); | ||
|
|
||
| useEffect(() => { | ||
| runOnLiveMarkdownRuntime(() => { | ||
| 'worklet'; | ||
|
|
||
| tagSharedValue.set(tagAutocompleteList); | ||
| }); | ||
|
Contributor
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. @289Adam289 You made a mistake here. You might want to fix this.
Contributor
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. @shubham1206agra what do you think about me fixing this in my PR for short-mentions, its a quick small change, and my PR is also related to running parser code in worklet?
Contributor
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. Ok |
||
| }, [tagSharedValue, tagAutocompleteList]); | ||
|
|
||
| const parser = useCallback( | ||
| (input: string) => { | ||
| 'worklet'; | ||
|
|
||
| return parseForLiveMarkdown(input, currentUserPersonalDetails.displayName ?? '', substitutionMap, emailListSharedValue, currencySharedValue, categorySharedValue, tagSharedValue); | ||
| }, | ||
| [currentUserPersonalDetails.displayName, substitutionMap, currencySharedValue, categorySharedValue, tagSharedValue, emailListSharedValue], | ||
| ); | ||
|
|
||
| const inputWidth = isFullWidth ? styles.w100 : {width: variables.popoverWidth}; | ||
|
|
||
| // Parse Fullstory attributes on initial render | ||
|
|
@@ -145,11 +212,7 @@ function SearchAutocompleteInput( | |
| onKeyPress={handleKeyPress(onSubmit)} | ||
| type="markdown" | ||
| multiline={false} | ||
| parser={(input: string) => { | ||
| 'worklet'; | ||
|
|
||
| return parseForLiveMarkdown(input, emailList, currentUserPersonalDetails.displayName ?? ''); | ||
| }} | ||
| parser={parser} | ||
| selection={selection} | ||
| /> | ||
| </View> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import {useIsFocused} from '@react-navigation/native'; | ||
| import isEqual from 'lodash/isEqual'; | ||
| import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import {useOnyx} from 'react-native-onyx'; | ||
|
|
@@ -123,7 +124,9 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps | |
| setAutocompleteQueryValue(updatedUserQuery); | ||
|
|
||
| const updatedSubstitutionsMap = getUpdatedSubstitutionsMap(userQuery, autocompleteSubstitutions); | ||
| setAutocompleteSubstitutions(updatedSubstitutionsMap); | ||
| if (!isEqual(autocompleteSubstitutions, updatedSubstitutionsMap)) { | ||
| setAutocompleteSubstitutions(updatedSubstitutionsMap); | ||
| } | ||
|
Comment on lines
+127
to
+129
Contributor
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. Looks like this condition logic led to the following issue: which we fixed by adjusting the check such that we only if (!isEqual(autocompleteSubstitutions, updatedSubstitutionsMap) && !isEmpty(updatedSubstitutionsMap)) {
setAutocompleteSubstitutions(updatedSubstitutionsMap);
} |
||
|
|
||
| if (updatedUserQuery) { | ||
| listRef.current?.updateAndScrollToFocusedIndex(0); | ||
|
|
@@ -290,6 +293,7 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps | |
| autocompleteListRef={listRef} | ||
| ref={textInputRef} | ||
| selection={selection} | ||
| substitutionMap={autocompleteSubstitutions} | ||
| /> | ||
| <View style={[styles.mh85vh, !isAutocompleteListVisible && styles.dNone]}> | ||
| <SearchAutocompleteList | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.