Skip to content

Commit 317563a

Browse files
authored
Merge pull request Expensify#86961 from callstack-internal/perf/decompose-header-secondary-actions
[No QA] Extract useExportAgainModal hook from MoneyReportHeader
2 parents 92936bf + 1308cd5 commit 317563a

2 files changed

Lines changed: 56 additions & 45 deletions

File tree

src/components/MoneyReportHeader.tsx

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import useDefaultExpensePolicy from '@hooks/useDefaultExpensePolicy';
1818
import useDeleteTransactions from '@hooks/useDeleteTransactions';
1919
import useDuplicateTransactionsAndViolations from '@hooks/useDuplicateTransactionsAndViolations';
2020
import useEnvironment from '@hooks/useEnvironment';
21+
import useExportAgainModal from '@hooks/useExportAgainModal';
2122
import useGetIOUReportFromReportAction from '@hooks/useGetIOUReportFromReportAction';
2223
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
2324
import useLocalize from '@hooks/useLocalize';
@@ -128,7 +129,6 @@ import {
128129
isPerDiemRequest,
129130
isTransactionPendingDelete,
130131
} from '@libs/TransactionUtils';
131-
import type {ExportType} from '@pages/inbox/report/ReportDetailsExportPage';
132132
import {
133133
approveMoneyRequest,
134134
canApproveIOU,
@@ -377,8 +377,8 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
377377
const hasCustomUnitOutOfPolicyViolation = hasCustomUnitOutOfPolicyViolationTransactionUtils(transactionViolations);
378378
const isPerDiemRequestOnNonDefaultWorkspace = isPerDiemRequest(transaction) && defaultExpensePolicy?.id !== policy?.id;
379379

380-
const [exportModalStatus, setExportModalStatus] = useState<ExportType | null>(null);
381380
const {showConfirmModal} = useConfirmModal();
381+
const {triggerExportOrConfirm} = useExportAgainModal(moneyRequestReport?.reportID, moneyRequestReport?.policyID);
382382
const {showDecisionModal} = useDecisionModal();
383383

384384
const showOfflineModal = () => {
@@ -1027,18 +1027,6 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
10271027
bankAccountList,
10281028
]);
10291029

1030-
const confirmExport = useCallback(() => {
1031-
setExportModalStatus(null);
1032-
if (!moneyRequestReport?.reportID || !connectedIntegration) {
1033-
return;
1034-
}
1035-
if (exportModalStatus === CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION) {
1036-
exportToIntegration(moneyRequestReport?.reportID, connectedIntegration);
1037-
} else if (exportModalStatus === CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED) {
1038-
markAsManuallyExported([moneyRequestReport?.reportID ?? CONST.DEFAULT_NUMBER_ID], connectedIntegration);
1039-
}
1040-
}, [connectedIntegration, exportModalStatus, moneyRequestReport?.reportID]);
1041-
10421030
const getAmount = (actionType: ValueOf<typeof CONST.REPORT.REPORT_PREVIEW_ACTIONS>) => ({
10431031
formattedAmount: getTotalAmountForIOUReportPreviewButton(moneyRequestReport, policy, actionType, nonPendingDeleteTransactions),
10441032
});
@@ -1171,7 +1159,7 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
11711159
return;
11721160
}
11731161
if (isExported) {
1174-
setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION);
1162+
triggerExportOrConfirm(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION);
11751163
return;
11761164
}
11771165
exportToIntegration(moneyRequestReport?.reportID, connectedIntegration);
@@ -1191,7 +1179,7 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
11911179
return;
11921180
}
11931181
if (isExported) {
1194-
setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED);
1182+
triggerExportOrConfirm(CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED);
11951183
return;
11961184
}
11971185
markAsManuallyExported([moneyRequestReport?.reportID ?? CONST.DEFAULT_NUMBER_ID], connectedIntegration);
@@ -1223,6 +1211,7 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
12231211
isExported,
12241212
exportTemplates,
12251213
beginExportWithTemplate,
1214+
triggerExportOrConfirm,
12261215
showDecisionModal,
12271216
]);
12281217

@@ -1250,7 +1239,7 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
12501239
setIsHoldMenuVisible(true);
12511240
}
12521241
}}
1253-
onExportModalOpen={() => setExportModalStatus(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION)}
1242+
onExportModalOpen={() => triggerExportOrConfirm(CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION)}
12541243
/>
12551244
);
12561245

@@ -2002,34 +1991,6 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
20021991
chatReport?.reportID,
20031992
]);
20041993

2005-
const showExportAgainModal = useCallback(() => {
2006-
if (!connectedIntegration) {
2007-
return;
2008-
}
2009-
showConfirmModal({
2010-
title: translate('workspace.exportAgainModal.title'),
2011-
prompt: translate('workspace.exportAgainModal.description', {
2012-
connectionName: connectedIntegration ?? connectedIntegrationFallback,
2013-
reportName: moneyRequestReport?.reportName ?? '',
2014-
}),
2015-
confirmText: translate('workspace.exportAgainModal.confirmText'),
2016-
cancelText: translate('workspace.exportAgainModal.cancelText'),
2017-
}).then((result) => {
2018-
if (result.action !== ModalActions.CONFIRM) {
2019-
setExportModalStatus(null);
2020-
return;
2021-
}
2022-
confirmExport();
2023-
});
2024-
}, [showConfirmModal, translate, connectedIntegration, connectedIntegrationFallback, moneyRequestReport?.reportName, confirmExport]);
2025-
2026-
useEffect(() => {
2027-
if (!exportModalStatus) {
2028-
return;
2029-
}
2030-
showExportAgainModal();
2031-
}, [exportModalStatus, showExportAgainModal]);
2032-
20331994
const allExpensesSelected = selectedTransactionIDs.length > 0 && selectedTransactionIDs.length === nonPendingDeleteTransactions.length;
20341995

20351996
const selectedTransactionsOptions = useMemo(() => {

src/hooks/useExportAgainModal.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {ModalActions} from '@components/Modal/Global/ModalContext';
2+
import {exportToIntegration, markAsManuallyExported} from '@libs/actions/Report';
3+
import {getConnectedIntegration, getValidConnectedIntegration} from '@libs/PolicyUtils';
4+
import type {ExportType} from '@pages/inbox/report/ReportDetailsExportPage';
5+
import CONST from '@src/CONST';
6+
import ONYXKEYS from '@src/ONYXKEYS';
7+
import useConfirmModal from './useConfirmModal';
8+
import useLocalize from './useLocalize';
9+
import useOnyx from './useOnyx';
10+
import usePolicy from './usePolicy';
11+
12+
function useExportAgainModal(reportID: string | undefined, policyID: string | undefined) {
13+
const {translate} = useLocalize();
14+
const {showConfirmModal} = useConfirmModal();
15+
const policy = usePolicy(policyID);
16+
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
17+
18+
const connectedIntegration = getValidConnectedIntegration(policy);
19+
const connectedIntegrationFallback = getConnectedIntegration(policy);
20+
const reportName = report?.reportName ?? '';
21+
22+
const triggerExportOrConfirm = (exportType: ExportType) => {
23+
if (!connectedIntegration) {
24+
return;
25+
}
26+
27+
showConfirmModal({
28+
title: translate('workspace.exportAgainModal.title'),
29+
prompt: translate('workspace.exportAgainModal.description', {
30+
connectionName: connectedIntegration ?? connectedIntegrationFallback,
31+
reportName,
32+
}),
33+
confirmText: translate('workspace.exportAgainModal.confirmText'),
34+
cancelText: translate('workspace.exportAgainModal.cancelText'),
35+
}).then((result) => {
36+
if (result.action !== ModalActions.CONFIRM || !reportID || !connectedIntegration) {
37+
return;
38+
}
39+
if (exportType === CONST.REPORT.EXPORT_OPTIONS.EXPORT_TO_INTEGRATION) {
40+
exportToIntegration(reportID, connectedIntegration);
41+
} else if (exportType === CONST.REPORT.EXPORT_OPTIONS.MARK_AS_EXPORTED) {
42+
markAsManuallyExported([reportID], connectedIntegration);
43+
}
44+
});
45+
};
46+
47+
return {triggerExportOrConfirm};
48+
}
49+
50+
export default useExportAgainModal;

0 commit comments

Comments
 (0)