Skip to content
Merged
21 changes: 13 additions & 8 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
const [reports, {sourceValue: changedReports}] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const prevReports = usePrevious(reports);
const [, {sourceValue: changedReportActions}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const personalDetails = usePersonalDetails();
const prevPersonalDetails = usePrevious(personalDetails);
const privateIsArchivedMap = usePrivateIsArchivedMap();
Expand All @@ -70,12 +71,12 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
const hasInitialData = useMemo(() => Object.keys(personalDetails ?? {}).length > 0, [personalDetails]);

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

/**
* This effect is responsible for generating the options list when their data is not yet initialized
Expand Down Expand Up @@ -132,7 +133,8 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
const report = changedReportsEntries[reportKey];
const reportID = reportKey.replace(ONYXKEYS.COLLECTION.REPORT, '');
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`];
const {reportOption} = processReport(report, personalDetails, privateIsArchived, currentUserAccountID, reportAttributes?.reports);
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`];
const {reportOption} = processReport(report, personalDetails, privateIsArchived, currentUserAccountID, policy, reportAttributes?.reports);

if (reportOption) {
updatedReportsMap.set(reportID, reportOption);
Expand All @@ -146,7 +148,7 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
reports: Array.from(updatedReportsMap.values()),
};
});
}, [changedReportsEntries, personalDetails, currentUserAccountID, reports, reportAttributes?.reports, privateIsArchivedMap]);
}, [changedReportsEntries, personalDetails, currentUserAccountID, reports, allPolicies, reportAttributes?.reports, privateIsArchivedMap]);

useEffect(() => {
if (!changedReportActions || !areOptionsInitialized.current) {
Expand All @@ -168,7 +170,8 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT_ACTIONS, '');
const reportItem = updatedReportsMap.get(reportID)?.item;
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`];
const {reportOption} = processReport(reportItem, personalDetails, privateIsArchived, currentUserAccountID, reportAttributes?.reports);
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportItem?.policyID}`];
const {reportOption} = processReport(reportItem, personalDetails, privateIsArchived, currentUserAccountID, policy, reportAttributes?.reports);

if (reportOption) {
updatedReportsMap.set(reportID, reportOption);
Expand All @@ -180,7 +183,7 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
reports: Array.from(updatedReportsMap.values()),
};
});
}, [changedReportActions, personalDetails, currentUserAccountID, reports, reportAttributes?.reports, privateIsArchivedMap]);
}, [changedReportActions, personalDetails, currentUserAccountID, reports, allPolicies, reportAttributes?.reports, privateIsArchivedMap]);

/**
* This effect is used to update the options list when personal details change.
Expand All @@ -203,6 +206,7 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
currentUserAccountID,
privateIsArchivedMap,
reports,
allPolicies,
reportAttributes?.reports,
);
setOptions((prevOptions) => ({
Expand Down Expand Up @@ -238,7 +242,8 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
}

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

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

setOptions((prevOptions) => {
const newOptions = {...prevOptions};
Expand Down
6 changes: 5 additions & 1 deletion src/components/Search/SearchFiltersChatsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
const selectedOptions: OptionData[] = selectedReportIDs.map((id) => {
const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${id}`];
const reportData = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`];
const report = getSelectedOptionData(createOptionFromReport({...reportData, reportID: id}, personalDetails, currentUserAccountID, privateIsArchived, reportAttributesDerived));
const reportPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const report = getSelectedOptionData(
createOptionFromReport({...reportData, reportID: id}, personalDetails, currentUserAccountID, privateIsArchived, reportPolicy, reportAttributesDerived),
);
const isReportArchived = !!privateIsArchived;
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];
Expand Down Expand Up @@ -110,6 +113,7 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
chatOptions.personalDetails,
privateIsArchivedMap,
currentUserAccountID,
allPolicies,
personalDetails,
false,
undefined,
Expand Down
3 changes: 3 additions & 0 deletions src/components/Search/SearchFiltersParticipantsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
const currentUserAccountID = currentUserPersonalDetails.accountID;
const currentUserEmail = currentUserPersonalDetails.email ?? '';
const [recentAttendees] = useOnyx(ONYXKEYS.NVP_RECENT_ATTENDEES);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);

// Transform raw recentAttendees into Option[] format for use with getValidOptions (only for attendee filter)
const recentAttendeeLists = useMemo(
Expand Down Expand Up @@ -109,6 +110,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
chatOptions.personalDetails,
privateIsArchivedMap,
currentUserAccountID,
allPolicies,
personalDetails,
true,
undefined,
Expand Down Expand Up @@ -178,6 +180,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
privateIsArchivedMap,
currentUserAccountID,
personalDetails,
allPolicies,
reportAttributesDerived,
translate,
formatPhoneNumber,
Expand Down
4 changes: 3 additions & 1 deletion src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
}

const privateIsArchived = privateIsArchivedMap[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${contextualReportID}`];
const option = createOptionFromReport(report, personalDetails, currentUserAccountID, privateIsArchived, undefined, {showPersonalDetails: true});
const reportPolicy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`];
Comment thread
dukenv0307 marked this conversation as resolved.
const option = createOptionFromReport(report, personalDetails, currentUserAccountID, privateIsArchived, reportPolicy, undefined, {showPersonalDetails: true});
reportForContextualSearch = option;
}

Expand Down Expand Up @@ -175,6 +176,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
personalDetails,
currentUserAccountID,
privateIsArchivedMap,
policies,
],
);

Expand Down
17 changes: 15 additions & 2 deletions src/hooks/useFilteredOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function useFilteredOptions(config: UseFilteredOptionsConfig = {}): UseFilteredO

const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [allPersonalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const reportAttributesDerived = useReportAttributes();

const privateIsArchivedMap = usePrivateIsArchivedMap();
Expand All @@ -84,14 +85,26 @@ function useFilteredOptions(config: UseFilteredOptionsConfig = {}): UseFilteredO
const options: OptionList | null = useMemo(
() =>
enabled && allReports && allPersonalDetails
? createFilteredOptionList(allPersonalDetails, allReports, currentUserPersonalDetails.accountID, reportAttributesDerived, privateIsArchivedMap, {
? createFilteredOptionList(allPersonalDetails, allReports, currentUserPersonalDetails.accountID, reportAttributesDerived, privateIsArchivedMap, allPolicies, {
maxRecentReports: reportsLimit,
includeP2P,
searchTerm,
betas,
})
: null,
[enabled, allReports, allPersonalDetails, currentUserPersonalDetails.accountID, reportAttributesDerived, privateIsArchivedMap, reportsLimit, includeP2P, searchTerm, betas],
[
enabled,
allReports,
allPersonalDetails,
currentUserPersonalDetails.accountID,
reportAttributesDerived,
privateIsArchivedMap,
allPolicies,
reportsLimit,
includeP2P,
searchTerm,
betas,
],
);

const hasMore = options ? reportsLimit < totalReports : false;
Expand Down
Loading
Loading