diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index ac28aeba4633..c5192dfdefb8 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -984,7 +984,19 @@ function getSuggestedSearchesVisibility( const isPayer = isPolicyPayer(policy, currentUserEmail); const isAdmin = policy.role === CONST.POLICY.ROLE.ADMIN; const isExporter = policy.exporter === currentUserEmail; - const isApprover = policy.approver === currentUserEmail; + let isSubmittedTo = false; + let isUserApprover = policy.approver === currentUserEmail; + for (const employee of Object.values(policy.employeeList ?? {})) { + if (employee?.submitsTo === currentUserEmail || employee?.forwardsTo === currentUserEmail) { + isSubmittedTo = true; + isUserApprover = true; + } else if (employee?.overLimitForwardsTo === currentUserEmail) { + isUserApprover = true; + } + if (isSubmittedTo && isUserApprover) { + break; + } + } const isApprovalEnabled = policy.approvalMode ? policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false; const hasExportError = (Object.keys(policy.connections ?? {}) as ConnectionName[]).some((connection) => { @@ -994,13 +1006,10 @@ function getSuggestedSearchesVisibility( const hasVBBA = !!policy.achAccount?.bankAccountID && policy.achAccount.state === CONST.BANK_ACCOUNT.STATE.OPEN; const hasReimburser = !!policy.achAccount?.reimburser; const isECardEnabled = !!policy.areExpensifyCardsEnabled; - const isSubmittedTo = Object.values(policy.employeeList ?? {}).some((employee) => { - return employee.submitsTo === currentUserEmail || employee.forwardsTo === currentUserEmail; - }); const isEligibleForSubmitSuggestion = isPaidPolicy; const isEligibleForPaySuggestion = isPaidPolicy && isPayer; - const isPolicyEligibleForApproveSuggestion = isPaidPolicy && isEligibleForApproveSuggestion(policy.approvalMode, isApprover, isSubmittedTo); + const isPolicyEligibleForApproveSuggestion = isPaidPolicy && isEligibleForApproveSuggestion(policy.approvalMode, isUserApprover, isSubmittedTo); const isEligibleForExportSuggestion = isExporter && !hasExportError; const isEligibleForStatementsSuggestion = isPaidPolicy && !!policy.areCompanyCardsEnabled && hasCardFeed; const isEligibleForUnapprovedCashSuggestion = isPaidPolicy && isAdmin && isApprovalEnabled && isPaymentEnabled; @@ -1009,10 +1018,10 @@ function getSuggestedSearchesVisibility( const isEligibleForReimbursementsSuggestion = isPaidPolicy && isAdmin && isPaymentEnabled && hasVBBA && hasReimburser; const isAuditor = policy.role === CONST.POLICY.ROLE.AUDITOR; const memberCount = Object.keys(policy.employeeList ?? {}).length; - const isEligibleForTopSpendersSuggestion = isPaidPolicy && (isAdmin || isAuditor || isApprover) && memberCount >= 2; + const isEligibleForTopSpendersSuggestion = isPaidPolicy && (isAdmin || isAuditor || isUserApprover) && memberCount >= 2; const isEligibleForTopCategoriesSuggestion = isPaidPolicy && policy.areCategoriesEnabled === true; const isEligibleForTopMerchantsSuggestion = isPaidPolicy; - const isEligibleForSpendOverTimeSuggestion = isPaidPolicy && (isAdmin || isAuditor || isApprover); + const isEligibleForSpendOverTimeSuggestion = isPaidPolicy && (isAdmin || isAuditor || isUserApprover); shouldShowSubmitSuggestion ||= isEligibleForSubmitSuggestion; shouldShowPaySuggestion ||= isEligibleForPaySuggestion; diff --git a/src/pages/workspace/travel/WorkspaceTravelInvoicingExportPage.tsx b/src/pages/workspace/travel/WorkspaceTravelInvoicingExportPage.tsx index 6104bd033968..a8e3352271fa 100644 --- a/src/pages/workspace/travel/WorkspaceTravelInvoicingExportPage.tsx +++ b/src/pages/workspace/travel/WorkspaceTravelInvoicingExportPage.tsx @@ -181,6 +181,9 @@ function WorkspaceTravelInvoicingExportPage({route}: WorkspaceTravelInvoicingExp setIsDownloading(false), ); } else { + // Intentional: this setState resets the loading indicator when generation completes but no file is available. + // It runs in a cleanup path of the effect, not as a cascading re-render trigger. + // eslint-disable-next-line react-hooks/set-state-in-effect setIsDownloading(false); } }, [prevIsGenerating, isGenerating, travelInvoiceStatement, policyID, getDateRange, translate, baseURL, currentUserPersonalDetails?.login, session?.encryptedAuthToken]); diff --git a/src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx b/src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx index 802bc90a5c2d..7deba2a1d5c8 100644 --- a/src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx +++ b/src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx @@ -87,6 +87,9 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) { const defaultApprover = getDefaultApprover(policy); + // useCallback is needed here because goBack is passed as a prop to child components; + // the rule flags it because the deps could be inlined, but removing useCallback would cause unnecessary re-renders. + // eslint-disable-next-line react-hooks/preserve-manual-memoization const goBack = useCallback(() => { if ((!feature && featureNameAlias !== CONST.UPGRADE_FEATURE_INTRO_MAPPING.policyPreventMemberChangingTitle.alias) || !policyID) { Navigation.dismissModal(); @@ -131,6 +134,9 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) { upgradeToCorporate(policy.id, feature?.name); }; + // useCallback is needed here because confirmUpgrade is passed as a prop to child components; + // the rule flags it because the deps could be inlined, but removing useCallback would cause unnecessary re-renders. + // eslint-disable-next-line react-hooks/preserve-manual-memoization const confirmUpgrade = useCallback(() => { if (!policyID) { return; diff --git a/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsEditPage.tsx b/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsEditPage.tsx index 78a0c2a4cce1..56253bbcb896 100644 --- a/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsEditPage.tsx +++ b/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsEditPage.tsx @@ -123,6 +123,9 @@ function WorkspaceWorkflowsApprovalsEditPage({policy, isLoadingReportData = true errors: null, originalApprovers: currentApprovalWorkflow.approvers, }); + // Intentional: synchronizes the initial workflow snapshot when the current workflow changes. + // This runs alongside setApprovalWorkflow above and is part of the same logical update. + // eslint-disable-next-line react-hooks/set-state-in-effect setInitialApprovalWorkflow(currentApprovalWorkflow); }, [currentApprovalWorkflow, defaultWorkflowMembers, initialApprovalWorkflow, usedApproverEmails]); diff --git a/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsExpensesFromPage.tsx b/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsExpensesFromPage.tsx index a2794b68f50a..b7a777b5fe0d 100644 --- a/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsExpensesFromPage.tsx +++ b/src/pages/workspace/workflows/approvals/WorkspaceWorkflowsApprovalsExpensesFromPage.tsx @@ -48,6 +48,9 @@ function WorkspaceWorkflowsApprovalsExpensesFromPage({policy, isLoadingReportDat return; } + // Intentional: derives the selected-members list from the approval workflow data. + // This effect synchronizes local component state with the Onyx-sourced workflow when it changes. + // eslint-disable-next-line react-hooks/set-state-in-effect setSelectedMembers( approvalWorkflow.members.map((member) => { const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policy?.employeeList); diff --git a/tests/unit/Search/SearchUIUtilsTest.ts b/tests/unit/Search/SearchUIUtilsTest.ts index 59b0bf309d88..5746da59b1f0 100644 --- a/tests/unit/Search/SearchUIUtilsTest.ts +++ b/tests/unit/Search/SearchUIUtilsTest.ts @@ -6116,6 +6116,66 @@ describe('SearchUIUtils', () => { expect(searchQuery).toContain(`view:${CONST.SEARCH.VIEW.PIE}`); }); + + test('Should show Top Spenders for workflow approver (submitsTo) in paid policy', () => { + const workflowApproverEmail = 'workflow-approver@policy.com'; + const policyKey = `policy_${policyID}`; + + const policies: OnyxCollection = { + [policyKey]: { + id: policyID, + type: CONST.POLICY.TYPE.TEAM, + approver: 'someone-else@policy.com', + employeeList: { + 'employee1@policy.com': {submitsTo: workflowApproverEmail, forwardsTo: ''}, + 'employee2@policy.com': {submitsTo: '', forwardsTo: ''}, + }, + } as unknown as OnyxTypes.Policy, + }; + + const response = SearchUIUtils.getSuggestedSearchesVisibility(workflowApproverEmail, {}, policies, undefined); + expect(response.visibility.topSpenders).toBe(true); + }); + + test('Should show Spend Over Time for workflow approver (forwardsTo) in paid policy', () => { + const workflowApproverEmail = 'workflow-approver@policy.com'; + const policyKey = `policy_${policyID}`; + + const policies: OnyxCollection = { + [policyKey]: { + id: policyID, + type: CONST.POLICY.TYPE.TEAM, + approver: 'someone-else@policy.com', + employeeList: { + 'employee1@policy.com': {submitsTo: '', forwardsTo: workflowApproverEmail}, + 'employee2@policy.com': {submitsTo: '', forwardsTo: ''}, + }, + } as unknown as OnyxTypes.Policy, + }; + + const response = SearchUIUtils.getSuggestedSearchesVisibility(workflowApproverEmail, {}, policies, undefined); + expect(response.visibility.spendOverTime).toBe(true); + }); + + test('Should hide Top Spenders for regular member who is not a workflow approver', () => { + const regularEmail = 'regular@policy.com'; + const policyKey = `policy_${policyID}`; + + const policies: OnyxCollection = { + [policyKey]: { + id: policyID, + type: CONST.POLICY.TYPE.TEAM, + approver: 'someone-else@policy.com', + employeeList: { + 'employee1@policy.com': {submitsTo: 'someone-else@policy.com', forwardsTo: ''}, + [regularEmail]: {submitsTo: '', forwardsTo: ''}, + }, + } as unknown as OnyxTypes.Policy, + }; + + const response = SearchUIUtils.getSuggestedSearchesVisibility(regularEmail, {}, policies, undefined); + expect(response.visibility.topSpenders).toBe(false); + }); }); describe('Test getSuggestedSearches sort defaults', () => {