Skip to content
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
42 changes: 34 additions & 8 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import useOnyx from '@hooks/useOnyx';
Expand Down Expand Up @@ -73,23 +74,25 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
const privateIsArchivedMap = usePrivateIsArchivedMap();
const sortedActions = useSortedActions();
const hasInitialData = useMemo(() => Object.keys(personalDetails ?? {}).length > 0, [personalDetails]);
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const getReprocessedReportOption = useCallback(
(report: OnyxEntry<Report> | null, reportID: string, policyID?: string) => {
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`];
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID ?? report?.policyID}`];

return processReport(report, personalDetails, privateIsArchived, policy, reportAttributes?.reports).reportOption;
return processReport(report, personalDetails, privateIsArchived, policy, reportAttributes?.reports, undefined, undefined, isTrackIntentUser).reportOption;
},
[allPolicies, personalDetails, privateIsArchivedMap, reportAttributes?.reports],
[allPolicies, personalDetails, privateIsArchivedMap, reportAttributes?.reports, isTrackIntentUser],
);

const loadOptions = useCallback(() => {
const optionLists = createOptionList(personalDetails, privateIsArchivedMap, reports, allPolicies, reportAttributes?.reports);
const optionLists = createOptionList(personalDetails, privateIsArchivedMap, reports, allPolicies, reportAttributes?.reports, undefined, undefined, isTrackIntentUser);
setOptions({
reports: optionLists.reports,
personalDetails: optionLists.personalDetails,
});
}, [personalDetails, privateIsArchivedMap, reports, allPolicies, reportAttributes?.reports]);
}, [personalDetails, privateIsArchivedMap, reports, allPolicies, reportAttributes?.reports, isTrackIntentUser]);

/**
* This effect is responsible for generating the options list when their data is not yet initialized
Expand Down Expand Up @@ -272,6 +275,9 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
reports,
allPolicies,
reportAttributes?.reports,
undefined,
undefined,
isTrackIntentUser,
);
setOptions((prevOptions) => ({
...prevOptions,
Expand Down Expand Up @@ -307,9 +313,20 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {

const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`];
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`];
const newReportOption = createOptionFromReport(report, personalDetails, privateIsArchived, policy, sortedActions, reportAttributes?.reports, {
showPersonalDetails: true,
});
const newReportOption = createOptionFromReport(
report,
personalDetails,
privateIsArchived,
policy,
sortedActions,
reportAttributes?.reports,
{
showPersonalDetails: true,
},
undefined,
undefined,
isTrackIntentUser,
);
const replaceIndex = options.reports.findIndex((option) => option.reportID === report.reportID);
newReportOptions.push({
newReportOption,
Expand All @@ -319,7 +336,16 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
}

// since personal details are not a collection, we need to recreate the whole list from scratch
const newPersonalDetailsOptions = createOptionList(personalDetails, privateIsArchivedMap, reports, allPolicies, reportAttributes?.reports).personalDetails;
const newPersonalDetailsOptions = createOptionList(
personalDetails,
privateIsArchivedMap,
reports,
allPolicies,
reportAttributes?.reports,
undefined,
undefined,
isTrackIntentUser,
).personalDetails;

setOptions((prevOptions) => {
const newOptions = {...prevOptions};
Expand Down
20 changes: 18 additions & 2 deletions src/components/Search/FilterComponents/InSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import React, {useEffect} from 'react';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import type {SearchFilterCommonProps} from '@components/Search/types';
Expand Down Expand Up @@ -68,16 +69,30 @@ function InSelector({value = [], selectionListTextInputStyle, selectionListStyle
const privateIsArchivedMap = usePrivateIsArchivedMap();
const [policyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, {selector: passthroughPolicyTagListSelector});
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const selectedOptions: OptionData[] = value.map((id) => {
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${id}`];
const reportData = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`];
const reportPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const report = getSelectedOptionData(createOptionFromReport({...reportData, reportID: id}, personalDetails, privateIsArchived, reportPolicy, sortedActions, reportAttributesDerived));
const report = getSelectedOptionData(
createOptionFromReport(
{...reportData, reportID: id},
personalDetails,
privateIsArchived,
reportPolicy,
sortedActions,
reportAttributesDerived,
undefined,
undefined,
undefined,
isTrackIntentUser,
),
);
const isReportArchived = !!privateIsArchived;
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];
const alternateText = getAlternateText(report, {}, {isReportArchived, policy, reportAttributesDerived, policyTags: reportPolicyTags, conciergeReportID});
const alternateText = getAlternateText(report, {}, {isReportArchived, policy, reportAttributesDerived, policyTags: reportPolicyTags, conciergeReportID, isTrackIntentUser});
return {...report, alternateText};
});

Expand All @@ -97,6 +112,7 @@ function InSelector({value = [], selectionListTextInputStyle, selectionListStyle
policyCollection: allPolicies,
sortedActions,
conciergeReportID,
isTrackIntentUser,
}).options;

const chatOptions = filterAndOrderOptions(defaultOptions, cleanSearchTerm, countryCode, loginList, currentUserEmail, currentUserAccountID, personalDetails, {
Expand Down
4 changes: 4 additions & 0 deletions src/components/Search/SearchAutocompleteList.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import type {ForwardedRef, RefObject} from 'react';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import type {OnyxCollection} from 'react-native-onyx';
Expand Down Expand Up @@ -176,6 +177,7 @@ function SearchAutocompleteList({
const currentUserAccountID = currentUserPersonalDetails.accountID;
const expensifyIcons = useMemoizedLazyExpensifyIcons(['History', 'MagnifyingGlass']);
const taxRates = useMemo(() => getAllTaxRates(policies), [policies]);
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const {options: listOptions, isLoading: isLoadingOptions} = useFilteredOptions({enabled: true, isSearching: !!autocompleteQueryValue.trim(), betas: betas ?? []});

Expand Down Expand Up @@ -225,6 +227,7 @@ function SearchAutocompleteList({
personalDetails,
sortedActions,
conciergeReportID,
isTrackIntentUser,
}).options;
}, [
listOptions,
Expand All @@ -240,6 +243,7 @@ function SearchAutocompleteList({
personalDetails,
sortedActions,
conciergeReportID,
isTrackIntentUser,
]);

const [isInitialRender, setIsInitialRender] = useState(true);
Expand Down
24 changes: 22 additions & 2 deletions src/components/Search/SearchFiltersChatsSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import passthroughPolicyTagListSelector from '@selectors/PolicyTagList';
import React, {useEffect, useState} from 'react';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
Expand Down Expand Up @@ -73,15 +74,33 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
const privateIsArchivedMap = usePrivateIsArchivedMap();
const [policyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, {selector: passthroughPolicyTagListSelector});
const sortedActions = useSortedActions();
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const selectedOptions: OptionData[] = selectedReportIDs.map((id) => {
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${id}`];
const reportData = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`];
const reportPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const report = getSelectedOptionData(createOptionFromReport({...reportData, reportID: id}, personalDetails, privateIsArchived, reportPolicy, sortedActions, reportAttributesDerived));
const report = getSelectedOptionData(
createOptionFromReport(
{...reportData, reportID: id},
personalDetails,
privateIsArchived,
reportPolicy,
sortedActions,
reportAttributesDerived,
undefined,
undefined,
undefined,
isTrackIntentUser,
),
);
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];
const alternateText = getAlternateText(report, {}, {isReportArchived: privateIsArchived, policy, reportAttributesDerived, policyTags: reportPolicyTags, conciergeReportID});
const alternateText = getAlternateText(
report,
{},
{isReportArchived: privateIsArchived, policy, reportAttributesDerived, policyTags: reportPolicyTags, conciergeReportID, isTrackIntentUser},
);
return {...report, alternateText};
});

Expand All @@ -101,6 +120,7 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
policyCollection: allPolicies,
sortedActions,
conciergeReportID,
isTrackIntentUser,
}).options;

const chatOptions = filterAndOrderOptions(defaultOptions, cleanSearchTerm, countryCode, loginList, currentUserEmail, currentUserAccountID, personalDetails, {
Expand Down
21 changes: 17 additions & 4 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {hasSeenTourSelector} from '@selectors/Onboarding';
import {hasSeenTourSelector, isTrackIntentUserSelector} from '@selectors/Onboarding';
import {deepEqual} from 'fast-equals';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import type {TextInputProps} from 'react-native';
Expand Down Expand Up @@ -73,6 +73,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});
const personalDetails = usePersonalDetails();
const sortedActions = useSortedActions();
const {shouldUseNarrowLayout} = useResponsiveLayout();
Expand Down Expand Up @@ -141,9 +142,20 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
return undefined;
}

const option = createOptionFromReport(contextualReport, personalDetails, contextualReportNVP, contextualReportPolicy, sortedActions, undefined, {
showPersonalDetails: true,
});
const option = createOptionFromReport(
contextualReport,
personalDetails,
contextualReportNVP,
contextualReportPolicy,
sortedActions,
undefined,
{
showPersonalDetails: true,
},
undefined,
undefined,
isTrackIntentUser,
);
reportForContextualSearch = option;
}

Expand Down Expand Up @@ -208,6 +220,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
sortedActions,
contextualReportNVP,
contextualReportPolicy,
isTrackIntentUser,
],
);

Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useAutocompleteSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import passthroughPolicyTagListSelector from '@selectors/PolicyTagList';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {LocaleContextProps} from '@components/LocaleContextProvider';
Expand Down Expand Up @@ -113,6 +114,7 @@ function useAutocompleteSuggestions({
const sortedActions = useSortedActions();
const {currencyList} = useCurrencyListState();
const {exportedToFilterOptions} = useExportedToFilterOptions();
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const parsedQuery = parseForAutocomplete(autocompleteQueryValue);
const {autocomplete, ranges = []} = parsedQuery ?? {};
Expand Down Expand Up @@ -244,6 +246,7 @@ function useAutocompleteSuggestions({
sortedActions,
conciergeReportID,
excludeFromSuggestionsOnly: memberExclusions,
isTrackIntentUser,
}).options.personalDetails.filter((participant) => participant.text && !alreadyAutocompletedKeys.has(participant.text.toLowerCase()));

return participants.map((participant) => ({
Expand Down Expand Up @@ -281,6 +284,7 @@ function useAutocompleteSuggestions({
personalDetails,
sortedActions,
conciergeReportID,
isTrackIntentUser,
}).options.recentReports.filter((chat) => {
if (!chat.text) {
return false;
Expand Down
26 changes: 19 additions & 7 deletions src/hooks/useFilteredOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import {useMemo, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {createFilteredOptionList} from '@libs/OptionsListUtils';
Expand Down Expand Up @@ -74,6 +75,7 @@ function useFilteredOptions(config: UseFilteredOptionsConfig = {}): UseFilteredO
const [allPersonalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const reportAttributesDerived = useReportAttributes();
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const privateIsArchivedMap = usePrivateIsArchivedMap();

Expand All @@ -83,14 +85,24 @@ function useFilteredOptions(config: UseFilteredOptionsConfig = {}): UseFilteredO
const options: OptionList | null = useMemo(
() =>
enabled && allReports && allPersonalDetails
? createFilteredOptionList(allPersonalDetails, allReports, reportAttributesDerived, privateIsArchivedMap, allPolicies, {
maxRecentReports: reportsLimit,
includeP2P,
isSearching,
betas,
})
? createFilteredOptionList(
allPersonalDetails,
allReports,
reportAttributesDerived,
privateIsArchivedMap,
allPolicies,
{
maxRecentReports: reportsLimit,
includeP2P,
isSearching,
betas,
},
undefined,
undefined,
isTrackIntentUser,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❌ CLEAN-REACT-PATTERNS-0 (docs)

This file compiles successfully with React Compiler, which means the useMemo wrapping this createFilteredOptionList call (starting at line 85) is redundant. The compiler will automatically memoize the derived value.

Moreover, isTrackIntentUser is now used inside the useMemo callback but is not included in the dependency array on line 105. This means the memoized value will be stale when isTrackIntentUser changes. Removing the manual useMemo would fix both the redundancy and the stale closure bug, since the compiler tracks dependencies automatically.

Remove the useMemo wrapper and compute options as a plain expression:

const options: OptionList | null =
    enabled && allReports && allPersonalDetails
        ? createFilteredOptionList(
              allPersonalDetails,
              allReports,
              reportAttributesDerived,
              privateIsArchivedMap,
              allPolicies,
              {
                  maxRecentReports: reportsLimit,
                  includeP2P,
                  isSearching,
                  betas,
              },
              undefined,
              undefined,
              isTrackIntentUser,
          )
        : null;

Reviewed at: ec63e2a | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

)
: null,
[enabled, allReports, allPersonalDetails, reportAttributesDerived, privateIsArchivedMap, allPolicies, reportsLimit, includeP2P, isSearching, betas],
[enabled, allReports, allPersonalDetails, reportAttributesDerived, privateIsArchivedMap, allPolicies, reportsLimit, includeP2P, isSearching, betas, isTrackIntentUser],
);

// When isSearching is set to true, the createFilteredOptionList returns all reports
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useSearchSelector/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isTrackIntentUserSelector} from '@selectors/Onboarding';
import passthroughPolicyTagListSelector from '@selectors/PolicyTagList';
import {useState} from 'react';
import type {PermissionStatus} from 'react-native-permissions';
Expand Down Expand Up @@ -211,6 +212,7 @@ function useSearchSelectorBase({
const personalDetails = usePersonalDetails();
const [allPolicyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, {selector: passthroughPolicyTagListSelector});
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});

const computedSearchTerm = getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode);
const trimmedSearchInput = debouncedSearchTerm.trim();
Expand Down Expand Up @@ -240,6 +242,7 @@ function useSearchSelectorBase({
personalDetails,
sortedActions,
conciergeReportID,
isTrackIntentUser,
});
case CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_GENERAL:
return getValidOptions(optionsWithContacts, allPolicies, draftComments, loginList, currentUserAccountID, currentUserEmail, conciergeReportID, {
Expand All @@ -262,6 +265,7 @@ function useSearchSelectorBase({
reportAttributesDerived: reportAttributesDerived?.reports,
allPolicyTags,
sortedActions,
isTrackIntentUser,
...getValidOptionsConfig,
});
case CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_SHARE_DESTINATION:
Expand All @@ -287,6 +291,7 @@ function useSearchSelectorBase({
reportAttributesDerived: reportAttributesDerived?.reports,
allPolicyTags,
sortedActions,
isTrackIntentUser,
});
case CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_ATTENDEES:
return getValidOptions(optionsWithContacts, allPolicies, draftComments, loginList, currentUserAccountID, currentUserEmail, conciergeReportID, {
Expand All @@ -309,6 +314,7 @@ function useSearchSelectorBase({
reportAttributesDerived: reportAttributesDerived?.reports,
allPolicyTags,
sortedActions,
isTrackIntentUser,
...getValidOptionsConfig,
});
default:
Expand Down
Loading
Loading