diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index caa0a19d76ee..8d29eace17cb 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -10,7 +10,6 @@ import type {Coordinate} from '@components/MapView/MapViewTypes'; import utils from '@components/MapView/utils'; import type {UnreportedExpenseListItemType} from '@components/SelectionListWithSections/types'; import type {TransactionWithOptionalSearchFields} from '@components/TransactionItemRow'; -import {getPolicyTagsData} from '@libs/actions/Policy/Tag'; import type {MergeDuplicatesParams} from '@libs/API/parameters'; import {getCategoryDefaultTaxRate, isCategoryMissing} from '@libs/CategoryUtils'; import {convertToBackendAmount, getCurrencyDecimals, getCurrencySymbol} from '@libs/CurrencyUtils'; @@ -60,6 +59,7 @@ import type { OnyxInputOrEntry, Policy, PolicyCategories, + PolicyTagLists, RecentWaypoint, Report, ReviewDuplicates, @@ -139,6 +139,28 @@ Onyx.connectWithoutView({ callback: (value) => (allBetas = value), }); +let allPolicyTags: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_TAGS, + waitForCollectionCallback: true, + callback: (value) => { + if (!value) { + allPolicyTags = {}; + return; + } + allPolicyTags = value; + }, +}); + +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file (https://github.com/Expensify/App/issues/72719) + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + function hasDistanceCustomUnit(transaction: OnyxEntry | Partial): boolean { const type = transaction?.comment?.type; const customUnitName = transaction?.comment?.customUnit?.name; @@ -2453,6 +2475,8 @@ function compareDuplicateTransactionFields( keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]]; } } else if (fieldName === 'tag') { + // TODO: Replace getPolicyTagsData with useOnyx hook (https://github.com/Expensify/App/issues/72719) + // eslint-disable-next-line @typescript-eslint/no-deprecated const policyTags = report?.policyID ? getPolicyTagsData(report?.policyID) : {}; const isMultiLevelTags = isMultiLevelTagsPolicyUtils(policyTags); if (isMultiLevelTags) { diff --git a/src/libs/actions/IOU/Duplicate.ts b/src/libs/actions/IOU/Duplicate.ts index 5bf8bcfc1c4d..d5607072b6ac 100644 --- a/src/libs/actions/IOU/Duplicate.ts +++ b/src/libs/actions/IOU/Duplicate.ts @@ -19,7 +19,6 @@ import { getTransactionDetails, } from '@libs/ReportUtils'; import {getRequestType, getTransactionType} from '@libs/TransactionUtils'; -import {getPolicyTagsData} from '@userActions/Policy/Tag'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; @@ -34,11 +33,22 @@ import { getAllTransactionViolations, getCurrentUserEmail, getMoneyRequestParticipantsFromReport, + getPolicyTags, getUserAccountID, requestMoney, trackExpense, } from '.'; +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file https://github.com/Expensify/App/issues/80049 + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + const allPolicyTags = getPolicyTags(); + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + function getIOUActionForTransactions(transactionIDList: Array, iouReportID: string | undefined): Array> { const allReportActions = getAllReportActionsFromIOU(); return Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`] ?? {})?.filter( @@ -559,6 +569,8 @@ function duplicateExpenseTransaction({ params.policyParams = { policy: targetPolicy, + // TODO: remove `allPolicyTags` from this file https://github.com/Expensify/App/issues/80049 + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTagList: getPolicyTagsData(targetPolicy.id) ?? {}, policyCategories: targetPolicyCategories ?? {}, }; diff --git a/src/libs/actions/IOU/SendInvoice.ts b/src/libs/actions/IOU/SendInvoice.ts index bea275ab32b4..2b02631c8e8f 100644 --- a/src/libs/actions/IOU/SendInvoice.ts +++ b/src/libs/actions/IOU/SendInvoice.ts @@ -23,7 +23,7 @@ import { } from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import {buildOptimisticTransaction} from '@libs/TransactionUtils'; -import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from '@userActions/Policy/Tag'; +import {buildOptimisticPolicyRecentlyUsedTags} from '@userActions/Policy/Tag'; import {notifyNewAction} from '@userActions/Report'; import {removeDraftTransaction} from '@userActions/TransactionEdit'; import CONST from '@src/CONST'; @@ -34,7 +34,7 @@ import type {InvoiceReceiver, InvoiceReceiverType} from '@src/types/onyx/Report' import type {OnyxData} from '@src/types/onyx/Request'; import type {Receipt} from '@src/types/onyx/Transaction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import {getAllPersonalDetails, getReceiptError, getSearchOnyxUpdate, mergePolicyRecentlyUsedCategories, mergePolicyRecentlyUsedCurrencies} from '.'; +import {getAllPersonalDetails, getPolicyTags, getReceiptError, getSearchOnyxUpdate, mergePolicyRecentlyUsedCategories, mergePolicyRecentlyUsedCurrencies} from '.'; import type {BasePolicyParams} from '.'; type SendInvoiceInformation = { @@ -108,6 +108,16 @@ type BuildOnyxDataForInvoiceParams = { participant?: Participant; }; +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file https://github.com/Expensify/App/issues/80048 + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + const allPolicyTags = getPolicyTags(); + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + /** Builds the Onyx data for an invoice */ function buildOnyxDataForInvoice( invoiceParams: BuildOnyxDataForInvoiceParams, @@ -635,6 +645,8 @@ function getSendInvoiceInformation({ const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories); const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ + // TODO: remove `allPolicyTags` from this file https://github.com/Expensify/App/issues/80048 + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(optimisticInvoiceReport.policyID), policyRecentlyUsedTags, transactionTags: tag, diff --git a/src/libs/actions/IOU/Split.ts b/src/libs/actions/IOU/Split.ts index e9711a7b904e..502b9ca2e8f9 100644 --- a/src/libs/actions/IOU/Split.ts +++ b/src/libs/actions/IOU/Split.ts @@ -38,7 +38,7 @@ import { } from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import {buildOptimisticTransaction, getChildTransactions, isOnHold, isPerDiemRequest as isPerDiemRequestTransactionUtils} from '@libs/TransactionUtils'; -import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from '@userActions/Policy/Tag'; +import {buildOptimisticPolicyRecentlyUsedTags} from '@userActions/Policy/Tag'; import {notifyNewAction} from '@userActions/Report'; import {removeDraftSplitTransaction, removeDraftTransaction} from '@userActions/TransactionEdit'; import CONST from '@src/CONST'; @@ -65,6 +65,7 @@ import { getMoneyRequestInformation, getMoneyRequestParticipantsFromReport, getOrCreateOptimisticSplitChatReport, + getPolicyTags, getReceiptError, getReportPreviewAction, getUpdateMoneyRequestParams, @@ -304,6 +305,16 @@ function splitBillAndOpenReport({ notifyNewAction(splitData.chatReportID, currentUserAccountID); } +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file [https://github.com/Expensify/App/issues/80401] + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + const allPolicyTags = getPolicyTags(); + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + /** Used exclusively for starting a split expense request that contains a receipt, the split request will be completed once the receipt is scanned * or user enters details manually. * @@ -592,6 +603,8 @@ function startSplitBill({ } const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories); const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ + // TODO: remove `allPolicyTags` from this file [https://github.com/Expensify/App/issues/80401] + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(participant.policyID), policyRecentlyUsedTags, transactionTags: tag, @@ -996,7 +1009,8 @@ function updateSplitTransactions({ const originalTransactionID = transactionData?.originalTransactionID ?? CONST.IOU.OPTIMISTIC_TRANSACTION_ID; const originalTransaction = allTransactionsList?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${originalTransactionID}`]; const originalTransactionDetails = getTransactionDetails(originalTransaction); - + // TODO: remove `allPolicyTags` from this file [https://github.com/Expensify/App/issues/80401] + // eslint-disable-next-line @typescript-eslint/no-deprecated const policyTags = getPolicyTagsData(expenseReport?.policyID); const participants = getMoneyRequestParticipantsFromReport(expenseReport, currentUserPersonalDetails.accountID); const splitExpenses = transactionData?.splitExpenses ?? []; diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 503be422fc32..cb99ac270607 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -224,7 +224,7 @@ import ViolationsUtils from '@libs/Violations/ViolationsUtils'; import {clearByKey as clearPdfByOnyxKey} from '@userActions/CachedPDFPaths'; import {buildAddMembersToWorkspaceOnyxData, buildUpdateWorkspaceMembersRoleOnyxData} from '@userActions/Policy/Member'; import {buildPolicyData, generatePolicyID} from '@userActions/Policy/Policy'; -import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from '@userActions/Policy/Tag'; +import {buildOptimisticPolicyRecentlyUsedTags} from '@userActions/Policy/Tag'; import type {GuidedSetupData} from '@userActions/Report'; import {buildInviteToRoomOnyxData, completeOnboarding, getCurrentUserAccountID, notifyNewAction, optimisticReportLastData} from '@userActions/Report'; import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions'; @@ -873,6 +873,19 @@ Onyx.connect({ }, }); +let allPolicyTags: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_TAGS, + waitForCollectionCallback: true, + callback: (value) => { + if (!value) { + allPolicyTags = {}; + return; + } + allPolicyTags = value; + }, +}); + let allReports: OnyxCollection; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, @@ -971,6 +984,19 @@ function getUserAccountID(): number { return userAccountID; } +function getPolicyTags(): OnyxCollection { + return allPolicyTags; +} + +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file (https://github.com/Expensify/App/issues/72721) + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + /** * @private * After finishing the action in RHP from the Inbox tab, besides dismissing the modal, we should open the report. @@ -3209,6 +3235,8 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories); const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(iouReport.policyID), policyRecentlyUsedTags, transactionTags: tag, @@ -3562,6 +3590,8 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI optimisticTransaction.hasEReceipt = true; const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories); const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(iouReport.policyID), policyRecentlyUsedTags, transactionTags: tag, @@ -4399,6 +4429,8 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U const hasModifiedTag = 'tag' in transactionChanges; if (hasModifiedTag) { const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(iouReport?.policyID), policyRecentlyUsedTags, transactionTags: transactionChanges.tag, @@ -7303,6 +7335,8 @@ function createSplitsAndOnyxData({ // Add tag to optimistic policy recently used tags when a participant is a workspace const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? buildOptimisticPolicyRecentlyUsedTags({ + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated policyTags: getPolicyTagsData(participant.policyID), policyRecentlyUsedTags, transactionTags: tag, @@ -11005,6 +11039,8 @@ function detachReceipt(transactionID: string | undefined, transactionPolicy: Ony ]; if (transactionPolicy && isPaidGroupPolicy(transactionPolicy) && newTransaction) { + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated const policyTagList = getPolicyTagsData(transactionPolicy.id); const currentTransactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] ?? []; const violationsOnyxData = ViolationsUtils.getViolationsOnyxData( @@ -11130,6 +11166,8 @@ function replaceReceipt({transactionID, file, source, transactionPolicy, transac ]; if (transactionPolicy && isPaidGroupPolicy(transactionPolicy) && newTransaction) { + // TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook + // eslint-disable-next-line @typescript-eslint/no-deprecated const policyTagList = getPolicyTagsData(transactionPolicy.id); const currentTransactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] ?? []; const violationsOnyxData = ViolationsUtils.getViolationsOnyxData( @@ -13136,6 +13174,7 @@ export { getUserAccountID, getReceiptError, getSearchOnyxUpdate, + getPolicyTags, setMoneyRequestTimeRate, setMoneyRequestTimeCount, buildMinimalTransactionForFormula, diff --git a/src/libs/actions/Policy/Tag.ts b/src/libs/actions/Policy/Tag.ts index ae474c02ed99..7996407c475a 100644 --- a/src/libs/actions/Policy/Tag.ts +++ b/src/libs/actions/Policy/Tag.ts @@ -1,5 +1,5 @@ import lodashCloneDeep from 'lodash/cloneDeep'; -import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {LocalizedTranslate} from '@components/LocaleContextProvider'; import type PolicyData from '@hooks/usePolicyData/types'; @@ -44,20 +44,6 @@ function isTaskIncomplete(taskReport: OnyxEntry): boolean { return !!taskReport && (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED); } -let allPolicyTags: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY_TAGS, - waitForCollectionCallback: true, - callback: (value) => { - if (!value) { - allPolicyTags = {}; - return; - } - - allPolicyTags = value; - }, -}); - function openPolicyTagsPage(policyID: string) { if (!policyID) { Log.warn('openPolicyTagsPage invalid params', {policyID}); @@ -1289,10 +1275,6 @@ function downloadMultiLevelTagsCSV(policyID: string, onDownloadFailed: () => voi fileDownload(translate, ApiUtils.getCommandURL({command}), fileName, '', false, formData, CONST.NETWORK.METHOD.POST, onDownloadFailed); } -function getPolicyTagsData(policyID: string | undefined) { - return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; -} - export { buildOptimisticPolicyRecentlyUsedTags, setPolicyRequiresTag, @@ -1312,7 +1294,6 @@ export { setPolicyTagApprover, importPolicyTags, downloadTagsCSV, - getPolicyTagsData, downloadMultiLevelTagsCSV, cleanPolicyTags, setImportedSpreadsheetIsImportingMultiLevelTags, diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 4bf505b1b3c4..30d7a0152020 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -54,7 +54,7 @@ import type {OriginalMessageIOU, OriginalMessageModifiedExpense} from '@src/type import type {OnyxData} from '@src/types/onyx/Request'; import type {Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import type TransactionState from '@src/types/utils/TransactionStateType'; -import {getPolicyTagsData} from './Policy/Tag'; +import {getPolicyTags} from './IOU/index'; let allTransactionDrafts: OnyxCollection = {}; Onyx.connect({ @@ -95,6 +95,16 @@ Onyx.connect({ callback: (val) => (allTransactionViolations = val ?? []), }); +/** + * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. + * TODO: remove `getPolicyTagsData` from this file (https://github.com/Expensify/App/issues/72720) + * All usages of this function should be replaced with useOnyx hook in React components. + */ +function getPolicyTagsData(policyID: string | undefined) { + const allPolicyTags = getPolicyTags(); + return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {}; +} + // Helper to safely check for a string 'name' property function isViolationWithName(violation: unknown): violation is {name: string} { return !!(violation && typeof violation === 'object' && typeof (violation as {name?: unknown}).name === 'string'); @@ -888,6 +898,8 @@ function changeTransactionsReport({ let transactionsMoved = false; let shouldFixViolations = false; + // TODO: Replace getPolicyTagsData with useOnyx hook (https://github.com/Expensify/App/issues/72720) + // eslint-disable-next-line @typescript-eslint/no-deprecated const policyTagList = getPolicyTagsData(policy?.id); const policyHasDependentTags = hasDependentTags(policy, policyTagList);