From 7ff0657f70ca5124530b2a08edfc01fa3f773d9f Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 31 Jul 2025 10:59:12 +0530 Subject: [PATCH 1/2] Refactored Onyx.connect in --- src/libs/actions/IOU.ts | 15 +++-- src/libs/actions/Policy/PerDiem.ts | 64 ++----------------- .../step/IOURequestStepConfirmation.tsx | 6 +- 3 files changed, 21 insertions(+), 64 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 193a4c4aba4b..62c2f1bdea58 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -228,7 +228,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import {clearByKey as clearPdfByOnyxKey} from './CachedPDFPaths'; import {buildOptimisticPolicyRecentlyUsedCategories, getPolicyCategoriesData} from './Policy/Category'; import {buildAddMembersToWorkspaceOnyxData, buildUpdateWorkspaceMembersRoleOnyxData} from './Policy/Member'; -import {buildOptimisticPolicyRecentlyUsedDestinations} from './Policy/PerDiem'; import {buildOptimisticRecentlyUsedCurrencies, buildPolicyData, generatePolicyID} from './Policy/Policy'; import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from './Policy/Tag'; import type {GuidedSetupData} from './Report'; @@ -409,6 +408,10 @@ type BasePolicyParams = { policyCategories?: OnyxEntry; }; +type RecentlyUsedParams = { + destinations?: OnyxEntry; +}; + type RequestMoneyParticipantParams = { payeeEmail: string | undefined; payeeAccountID: number; @@ -419,6 +422,7 @@ type PerDiemExpenseInformation = { report: OnyxEntry; participantParams: RequestMoneyParticipantParams; policyParams?: BasePolicyParams; + recentlyUsedParams?: RecentlyUsedParams; transactionParams: PerDiemExpenseTransactionParams; }; @@ -427,6 +431,7 @@ type PerDiemExpenseInformationParams = { transactionParams: PerDiemExpenseTransactionParams; participantParams: RequestMoneyParticipantParams; policyParams?: BasePolicyParams; + recentlyUsedParams?: RecentlyUsedParams; moneyRequestReportID?: string; }; @@ -3563,9 +3568,10 @@ function computeDefaultPerDiemExpenseComment(customUnit: TransactionCustomUnit, * it creates optimistic versions of them and uses those instead */ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseInformationParams): MoneyRequestInformation { - const {parentChatReport, transactionParams, participantParams, policyParams = {}, moneyRequestReportID = ''} = perDiemExpenseInformation; + const {parentChatReport, transactionParams, participantParams, policyParams = {}, recentlyUsedParams = {}, moneyRequestReportID = ''} = perDiemExpenseInformation; const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams; const {policy, policyCategories, policyTagList} = policyParams; + const {destinations: recentlyUsedDestinations} = recentlyUsedParams; const {comment = '', currency, created, category, tag, customUnit, billable, attendees} = transactionParams; const amount = computePerDiemExpenseAmount(customUnit); @@ -3655,7 +3661,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI const optimisticPolicyRecentlyUsedCategories = buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag); const optimisticPolicyRecentlyUsedCurrencies = buildOptimisticRecentlyUsedCurrencies(currency); - const optimisticPolicyRecentlyUsedDestinations = buildOptimisticPolicyRecentlyUsedDestinations(iouReport.policyID, customUnit.customUnitRateID); + const optimisticPolicyRecentlyUsedDestinations = customUnit.customUnitRateID ? [...new Set([customUnit.customUnitRateID, ...(recentlyUsedDestinations ?? [])])] : []; // STEP 4: Build optimistic reportActions. We need: // 1. CREATED action for the chatReport @@ -5627,7 +5633,7 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { * Submit per diem expense to another user */ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInformation) { - const {report, participantParams, policyParams = {}, transactionParams} = submitPerDiemExpenseInformation; + const {report, participantParams, policyParams = {}, recentlyUsedParams = {}, transactionParams} = submitPerDiemExpenseInformation; const {payeeAccountID} = participantParams; const {currency, comment = '', category, tag, created, customUnit, attendees} = transactionParams; @@ -5663,6 +5669,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf parentChatReport: currentChatReport, participantParams, policyParams, + recentlyUsedParams, transactionParams, moneyRequestReportID, }); diff --git a/src/libs/actions/Policy/PerDiem.ts b/src/libs/actions/Policy/PerDiem.ts index c6994c24cbb5..33bc2bef5a02 100644 --- a/src/libs/actions/Policy/PerDiem.ts +++ b/src/libs/actions/Policy/PerDiem.ts @@ -1,58 +1,22 @@ import lodashDeepClone from 'lodash/cloneDeep'; -import lodashUnion from 'lodash/union'; -import type {NullishDeep, OnyxCollection} from 'react-native-onyx'; +import type {NullishDeep} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; -import * as ApiUtils from '@libs/ApiUtils'; +import {getCommandURL} from '@libs/ApiUtils'; import fileDownload from '@libs/fileDownload'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import {translateLocal} from '@libs/Localize'; import enhanceParameters from '@libs/Network/enhanceParameters'; -import * as NumberUtils from '@libs/NumberUtils'; +import {generateHexadecimalValue} from '@libs/NumberUtils'; import {goBackWhenEnableFeature} from '@libs/PolicyUtils'; -import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy, RecentlyUsedCategories, Report} from '@src/types/onyx'; import type {ErrorFields, PendingAction} from '@src/types/onyx/OnyxCommon'; import type {CustomUnit, Rate} from '@src/types/onyx/Policy'; import type {OnyxData} from '@src/types/onyx/Request'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -const allPolicies: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY, - callback: (val, key) => { - if (!key) { - return; - } - if (val === null || val === undefined) { - // If we are deleting a policy, we have to check every report linked to that policy - // and unset the draft indicator (pencil icon) alongside removing any draft comments. Clearing these values will keep the newly archived chats from being displayed in the LHN. - // More info: https://github.com/Expensify/App/issues/14260 - const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); - const policyReports = ReportUtils.getAllPolicyReports(policyID); - const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; - policyReports.forEach((policyReport) => { - if (!policyReport) { - return; - } - const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; - }); - Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); - Onyx.multiSet(cleanUpSetQueries); - delete allPolicies[key]; - return; - } - - allPolicies[key] = val; - }, -}); - type SubRateData = { pendingAction?: PendingAction; destination: string; @@ -67,7 +31,7 @@ type SubRateData = { * Returns a client generated 13 character hexadecimal value for a custom unit ID */ function generateCustomUnitID(): string { - return NumberUtils.generateHexadecimalValue(13); + return generateHexadecimalValue(13); } function enablePerDiem(policyID: string, enabled: boolean, customUnitID?: string, shouldGoBack?: boolean) { @@ -193,7 +157,7 @@ function downloadPerDiemCSV(policyID: string, onDownloadFailed: () => void) { formData.append(key, String(value)); }); - fileDownload(ApiUtils.getCommandURL({command: WRITE_COMMANDS.EXPORT_PER_DIEM_CSV}), fileName, '', false, formData, CONST.NETWORK.METHOD.POST, onDownloadFailed); + fileDownload(getCommandURL({command: WRITE_COMMANDS.EXPORT_PER_DIEM_CSV}), fileName, '', false, formData, CONST.NETWORK.METHOD.POST, onDownloadFailed); } function clearPolicyPerDiemRatesErrorFields(policyID: string, customUnitID: string, updatedErrorFields: ErrorFields) { @@ -400,23 +364,6 @@ function editPerDiemRateCurrency(policyID: string, rateID: string, customUnit: C API.write(WRITE_COMMANDS.UPDATE_WORKSPACE_CUSTOM_UNIT, parameters, onyxData); } -let allRecentlyUsedDestinations: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS, - waitForCollectionCallback: true, - callback: (val) => (allRecentlyUsedDestinations = val), -}); - -function buildOptimisticPolicyRecentlyUsedDestinations(policyID: string | undefined, destination: string | undefined) { - if (!policyID || !destination) { - return []; - } - - const policyRecentlyUsedDestinations = allRecentlyUsedDestinations?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS}${policyID}`] ?? []; - - return lodashUnion([destination], policyRecentlyUsedDestinations); -} - export { generateCustomUnitID, enablePerDiem, @@ -429,5 +376,4 @@ export { editPerDiemRateSubrate, editPerDiemRateAmount, editPerDiemRateCurrency, - buildOptimisticPolicyRecentlyUsedDestinations, }; diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index c4f926728e6a..bd0579e57a40 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -130,6 +130,7 @@ function IOURequestStepConfirmation({ const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${realPolicyID}`, {canBeMissing: true}); const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION, {canBeMissing: true}); const [reportAttributesDerived] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, {canBeMissing: true, selector: (val) => val?.reports}); + const [recentlyUsedDestinations] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS}${realPolicyID}`, {canBeMissing: true}); /* * We want to use a report from the transaction if it exists @@ -543,6 +544,9 @@ function IOURequestStepConfirmation({ policyTagList: policyTags, policyCategories, }, + recentlyUsedParams: { + destinations: recentlyUsedDestinations, + }, transactionParams: { currency: transaction.currency, created: transaction.created, @@ -555,7 +559,7 @@ function IOURequestStepConfirmation({ }, }); }, - [report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories], + [report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories, recentlyUsedDestinations], ); const trackExpense = useCallback( From 678af1af6043e357b69eee680e70e45d3c7e8b75 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 31 Jul 2025 13:05:25 +0530 Subject: [PATCH 2/2] Fix warning limit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d5cace600b9..8035a31b683d 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand", "perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure", "typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc", - "lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=324 --cache --cache-location=node_modules/.cache/eslint", + "lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=322 --cache --cache-location=node_modules/.cache/eslint", "lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh", "lint-watch": "npx eslint-watch --watch --changed", "shellcheck": "./scripts/shellCheck.sh",