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
23 changes: 16 additions & 7 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
6 changes: 6 additions & 0 deletions src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnyxTypes.Policy> = {
[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<OnyxTypes.Policy> = {
[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<OnyxTypes.Policy> = {
[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', () => {
Expand Down
Loading