Skip to content

Add workspace autocomplete list and highlighting #60424

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions src/components/Search/SearchAutocompleteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import UserListItem from '@components/SelectionList/UserListItem';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useFastSearchFromOptions from '@hooks/useFastSearchFromOptions';
import useLocalize from '@hooks/useLocalize';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -23,7 +24,7 @@ import memoize from '@libs/memoize';
import {combineOrderingOfReportsAndPersonalDetails, getSearchOptions, getValidPersonalDetailOptions} from '@libs/OptionsListUtils';
import type {Options, SearchOption} from '@libs/OptionsListUtils';
import Performance from '@libs/Performance';
import {getAllTaxRates, getCleanedTagName} from '@libs/PolicyUtils';
import {getAllTaxRates, getCleanedTagName, shouldShowPolicy} from '@libs/PolicyUtils';
import type {OptionData} from '@libs/ReportUtils';
import {
getAutocompleteCategories,
Expand Down Expand Up @@ -145,11 +146,12 @@ function SearchAutocompleteList(

const {activeWorkspaceID} = useActiveWorkspace();
const policy = usePolicy(activeWorkspaceID);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [recentSearches] = useOnyx(ONYXKEYS.RECENT_SEARCHES);
const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: true});
const [recentSearches] = useOnyx(ONYXKEYS.RECENT_SEARCHES, {canBeMissing: true});
const personalDetails = usePersonalDetails();
const [reports = {}] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [reports = {}] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true});
const taxRates = getAllTaxRates();
const {canUseLeftHandBar} = usePermissions();

const {options, areOptionsInitialized} = useOptionsList();
const searchOptions = useMemo(() => {
Expand All @@ -167,8 +169,8 @@ function SearchAutocompleteList(
const expenseTypes = Object.values(CONST.SEARCH.TRANSACTION_TYPE);
const booleanTypes = Object.values(CONST.SEARCH.BOOLEAN);

const [userCardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST);
const [userCardList] = useOnyx(ONYXKEYS.CARD_LIST, {canBeMissing: true});
const [workspaceCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST, {canBeMissing: true});
const allCards = useMemo(() => mergeCardListWithWorkspaceFeeds(workspaceCardFeeds ?? CONST.EMPTY_OBJECT, userCardList), [userCardList, workspaceCardFeeds]);
const cardAutocompleteList = Object.values(allCards);
const cardFeedNamesWithType = useMemo(() => {
Expand Down Expand Up @@ -210,20 +212,31 @@ function SearchAutocompleteList(

const taxAutocompleteList = useMemo(() => getAutocompleteTaxList(taxRates, policy), [policy, taxRates]);

const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES);
const [allRecentCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES);
const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES, {canBeMissing: false});
const [allRecentCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, {canBeMissing: true});
const categoryAutocompleteList = useMemo(() => {
return getAutocompleteCategories(allPolicyCategories, activeWorkspaceID);
}, [activeWorkspaceID, allPolicyCategories]);
const recentCategoriesAutocompleteList = useMemo(() => {
return getAutocompleteRecentCategories(allRecentCategories, activeWorkspaceID);
}, [activeWorkspaceID, allRecentCategories]);

const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST);
const [policies = {}] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email, canBeMissing: false});

const workspaceList = useMemo(
() =>
Object.values(policies)
.filter((singlePolicy) => !!singlePolicy && shouldShowPolicy(singlePolicy, false, currentUserLogin) && !singlePolicy?.isJoinRequestPending)
.map((singlePolicy) => ({id: singlePolicy?.id, name: singlePolicy?.name ?? ''})),
[policies, currentUserLogin],
);

const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const currencyAutocompleteList = Object.keys(currencyList ?? {}).filter((currency) => !currencyList?.[currency]?.retired);
const [recentCurrencyAutocompleteList] = useOnyx(ONYXKEYS.RECENTLY_USED_CURRENCIES);
const [allPoliciesTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS);
const [allRecentTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS);
const [recentCurrencyAutocompleteList] = useOnyx(ONYXKEYS.RECENTLY_USED_CURRENCIES, {canBeMissing: true});
const [allPoliciesTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, {canBeMissing: false});
const [allRecentTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS, {canBeMissing: true});
const tagAutocompleteList = useMemo(() => {
return getAutocompleteTags(allPoliciesTags, activeWorkspaceID);
}, [activeWorkspaceID, allPoliciesTags]);
Expand Down Expand Up @@ -403,6 +416,22 @@ function SearchAutocompleteList(
text: value,
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID: {
if (!canUseLeftHandBar) {
return [];
}
const filteredPolicies = workspaceList
.filter((workspace) => workspace.name.toLowerCase().includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(workspace.name.toLowerCase()))
.sort()
.slice(0, 10);

return filteredPolicies.map((workspace) => ({
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.POLICY_ID,
text: workspace.name,
autocompleteID: workspace.id,
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID,
}));
}
default: {
return [];
}
Expand All @@ -419,14 +448,16 @@ function SearchAutocompleteList(
getParticipantsAutocompleteList,
searchOptions.recentReports,
typeAutocompleteList,
groupByAutocompleteList,
statusAutocompleteList,
expenseTypes,
feedAutoCompleteList,
workspaceCardFeeds,
cardAutocompleteList,
allCards,
groupByAutocompleteList,
booleanTypes,
workspaceList,
canUseLeftHandBar,
]);

const sortedRecentSearches = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
buildUserReadableQueryString,
buildUserReadableQueryStringWithPolicyID,
getQueryWithUpdatedValues,
getQueryWithUpdatedValuesWithoutPolicy,
isDefaultExpensesQuery,
isDefaultExpensesQueryWithPolicyIDCheck,
sanitizeSearchValue,
Expand Down Expand Up @@ -68,11 +69,11 @@ function SearchPageHeaderInput({queryJSON, searchRouterListVisible, hideSearchRo
const theme = useTheme();
const {shouldUseNarrowLayout: displayNarrowHeader} = useResponsiveLayout();
const personalDetails = usePersonalDetails();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true});
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
const taxRates = useMemo(() => getAllTaxRates(), []);
const [userCardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST);
const [userCardList] = useOnyx(ONYXKEYS.CARD_LIST, {canBeMissing: false});
const [workspaceCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST, {canBeMissing: true});
const allCards = useMemo(() => mergeCardListWithWorkspaceFeeds(workspaceCardFeeds ?? CONST.EMPTY_OBJECT, userCardList), [userCardList, workspaceCardFeeds]);
const cardFeedNamesWithType = useMemo(() => {
return getCardFeedNamesWithType({workspaceCardFeeds, translate});
Expand Down Expand Up @@ -121,9 +122,9 @@ function SearchPageHeaderInput({queryJSON, searchRouterListVisible, hideSearchRo
}, [isDefaultQuery, queryText]);

useEffect(() => {
const substitutionsMap = buildSubstitutionsMap(originalInputQuery, personalDetails, reports, taxRates, allCards, cardFeedNamesWithType);
const substitutionsMap = buildSubstitutionsMap(originalInputQuery, personalDetails, reports, taxRates, allCards, cardFeedNamesWithType, policies, canUseLeftHandBar);
setAutocompleteSubstitutions(substitutionsMap);
}, [cardFeedNamesWithType, allCards, originalInputQuery, personalDetails, reports, taxRates]);
}, [cardFeedNamesWithType, allCards, originalInputQuery, personalDetails, reports, taxRates, policies, canUseLeftHandBar]);

useEffect(() => {
if (searchRouterListVisible) {
Expand Down Expand Up @@ -167,7 +168,7 @@ function SearchPageHeaderInput({queryJSON, searchRouterListVisible, hideSearchRo
const submitSearch = useCallback(
(queryString: SearchQueryString) => {
const queryWithSubstitutions = getQueryWithSubstitutions(queryString, autocompleteSubstitutions);
const updatedQuery = getQueryWithUpdatedValues(queryWithSubstitutions, queryJSON.policyID);
const updatedQuery = canUseLeftHandBar ? getQueryWithUpdatedValuesWithoutPolicy(queryWithSubstitutions) : getQueryWithUpdatedValues(queryWithSubstitutions, queryJSON.policyID);

if (!updatedQuery) {
return;
Expand All @@ -182,7 +183,7 @@ function SearchPageHeaderInput({queryJSON, searchRouterListVisible, hideSearchRo
setAutocompleteQueryValue('');
}
},
[autocompleteSubstitutions, hideSearchRouterList, originalInputQuery, queryJSON.policyID],
[autocompleteSubstitutions, hideSearchRouterList, originalInputQuery, queryJSON.policyID, canUseLeftHandBar],
);

const onListItemPress = useCallback(
Expand Down
12 changes: 7 additions & 5 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useDebouncedState from '@hooks/useDebouncedState';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useLocalize from '@hooks/useLocalize';
import usePermissions from '@hooks/usePermissions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {scrollToRight} from '@libs/InputUtils';
import type {SearchOption} from '@libs/OptionsListUtils';
import type {OptionData} from '@libs/ReportUtils';
import {getAutocompleteQueryWithComma, getQueryWithoutAutocompletedPart} from '@libs/SearchAutocompleteUtils';
import {getQueryWithUpdatedValues, sanitizeSearchValue} from '@libs/SearchQueryUtils';
import {getQueryWithUpdatedValues, getQueryWithUpdatedValuesWithoutPolicy, sanitizeSearchValue} from '@libs/SearchQueryUtils';
import StringUtils from '@libs/StringUtils';
import Navigation from '@navigation/Navigation';
import type {ReportsSplitNavigatorParamList} from '@navigation/types';
Expand Down Expand Up @@ -79,9 +80,10 @@ type SearchRouterProps = {
function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDisplayed}: SearchRouterProps, ref: React.Ref<View>) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [, recentSearchesMetadata] = useOnyx(ONYXKEYS.RECENT_SEARCHES);
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false});
const [, recentSearchesMetadata] = useOnyx(ONYXKEYS.RECENT_SEARCHES, {canBeMissing: true});
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false, canBeMissing: true});
const {activeWorkspaceID} = useActiveWorkspace();
const {canUseLeftHandBar} = usePermissions();

const {shouldUseNarrowLayout} = useResponsiveLayout();
const listRef = useRef<SelectionListHandle>(null);
Expand Down Expand Up @@ -215,7 +217,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
const submitSearch = useCallback(
(queryString: SearchQueryString) => {
const queryWithSubstitutions = getQueryWithSubstitutions(queryString, autocompleteSubstitutions);
const updatedQuery = getQueryWithUpdatedValues(queryWithSubstitutions, activeWorkspaceID);
const updatedQuery = canUseLeftHandBar ? getQueryWithUpdatedValuesWithoutPolicy(queryWithSubstitutions) : getQueryWithUpdatedValues(queryWithSubstitutions, activeWorkspaceID);
if (!updatedQuery) {
return;
}
Expand All @@ -226,7 +228,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
setTextInputValue('');
setAutocompleteQueryValue('');
},
[autocompleteSubstitutions, onRouterClose, setTextInputValue, activeWorkspaceID],
[autocompleteSubstitutions, onRouterClose, setTextInputValue, activeWorkspaceID, canUseLeftHandBar],
);

const setTextAndUpdateSelection = useCallback(
Expand Down
13 changes: 9 additions & 4 deletions src/components/Search/SearchRouter/buildSubstitutionsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type {OnyxCollection} from 'react-native-onyx';
import type {SearchAutocompleteQueryRange, SearchFilterKey} from '@components/Search/types';
import type {CardFeedNamesWithType} from '@libs/CardFeedUtils';
import {parse} from '@libs/SearchParser/autocompleteParser';
import {getFilterDisplayValue} from '@libs/SearchQueryUtils';
import {getFilterDisplayValue, getFilterDisplayValueWithPolicyID} from '@libs/SearchQueryUtils';
import CONST from '@src/CONST';
import type {CardList, PersonalDetailsList, Report} from '@src/types/onyx';
import type {CardList, PersonalDetailsList, Policy, Report} from '@src/types/onyx';
import type {SubstitutionMap} from './getQueryWithSubstitutions';

const getSubstitutionsKey = (filterKey: SearchFilterKey, value: string) => `${filterKey}:${value}`;
Expand Down Expand Up @@ -32,6 +32,8 @@ function buildSubstitutionsMap(
allTaxRates: Record<string, string[]>,
cardList: CardList,
cardFeedNamesWithType: CardFeedNamesWithType,
policies: OnyxCollection<Policy>,
canUseLeftHandBar?: boolean,
): SubstitutionMap {
const parsedQuery = parse(query) as {ranges: SearchAutocompleteQueryRange[]};

Expand Down Expand Up @@ -63,9 +65,12 @@ function buildSubstitutionsMap(
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID
) {
const displayValue = getFilterDisplayValue(filterKey, filterValue, personalDetails, reports, cardList, cardFeedNamesWithType);
const displayValue = canUseLeftHandBar
? getFilterDisplayValueWithPolicyID(filterKey, filterValue, personalDetails, reports, cardList, cardFeedNamesWithType, policies)
: getFilterDisplayValue(filterKey, filterValue, personalDetails, reports, cardList, cardFeedNamesWithType);

// If displayValue === filterValue, then it means there is nothing to substitute, so we don't add any key to map
if (displayValue !== filterValue) {
Expand Down
1 change: 1 addition & 0 deletions src/libs/SearchAutocompleteUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function filterOutRangesWithCorrectValue(
case CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE:
case CONST.SEARCH.SYNTAX_FILTER_KEYS.FEED:
case CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID:
case CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID:
return substitutionMap[`${range.key}:${range.value}`] !== undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No bugs here 👍 I noticed a small improvement that we could do.

The way this key is built: ${range.key}:${range.value} should be the exact same way that we build it inside autocompleteMap.
There is a local getSubstitutionsKey() function in buildSubstitutionsMap (and duplicated in 1 other file as well).
We could extract it, and reuse here - just to be super clean 😎


case CONST.SEARCH.SYNTAX_FILTER_KEYS.TO:
Expand Down
3 changes: 3 additions & 0 deletions src/libs/SearchParser/autocompleteParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ function peg$parse(input, options) {
s1 = peg$parsereimbursable();
if (s1 === peg$FAILED) {
s1 = peg$parsebillable();
if (s1 === peg$FAILED) {
s1 = peg$parsepolicyID();
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libs/SearchParser/autocompleteParser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ autocompleteKey "key"
/ groupBy
/ reimbursable
/ billable
/ policyID
)

filterKey
Expand Down
Loading
Loading