forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyRequestReportView.tsx
More file actions
344 lines (304 loc) · 16.8 KB
/
MoneyRequestReportView.tsx
File metadata and controls
344 lines (304 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import {PortalHost} from '@gorhom/portal';
import React, {useCallback, useEffect, useMemo} from 'react';
// We use Animated for all functionality related to wide RHP to make it easier
// to interact with react-navigation components (e.g., CardContainer, interpolator), which also use Animated.
// eslint-disable-next-line no-restricted-imports
import {Animated, InteractionManager, ScrollView, View} from 'react-native';
import type {LayoutChangeEvent} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import MoneyReportHeader from '@components/MoneyReportHeader';
import MoneyRequestHeader from '@components/MoneyRequestHeader';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import MoneyRequestReceiptView from '@components/ReportActionItem/MoneyRequestReceiptView';
import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView';
import ReportHeaderSkeletonView from '@components/ReportHeaderSkeletonView';
import useNetwork from '@hooks/useNetwork';
import useNewTransactions from '@hooks/useNewTransactions';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useParentReportAction from '@hooks/useParentReportAction';
import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {removeFailedReport} from '@libs/actions/Report';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import Log from '@libs/Log';
import {getAllNonDeletedTransactions, shouldDisplayReportTableView, shouldWaitForTransactions as shouldWaitForTransactionsUtil} from '@libs/MoneyRequestReportUtils';
import navigationRef from '@libs/Navigation/navigationRef';
import {getFilteredReportActionsForReportView, getOneTransactionThreadReportID, isMoneyRequestAction, isSentMoneyReportAction} from '@libs/ReportActionsUtils';
import {canEditReportAction, getReportOfflinePendingActionAndErrors, isReportTransactionThread} from '@libs/ReportUtils';
import {buildCannedSearchQuery} from '@libs/SearchQueryUtils';
import {cancelSpan} from '@libs/telemetry/activeSpans';
import markOpenReportEnd from '@libs/telemetry/markOpenReportEnd';
import type {SkeletonSpanReasonAttributes} from '@libs/telemetry/useSkeletonSpan';
import Navigation from '@navigation/Navigation';
import ReportActionsView from '@pages/inbox/report/ReportActionsView';
import ReportFooter from '@pages/inbox/report/ReportFooter';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type {ThemeStyles} from '@src/styles';
import type * as OnyxTypes from '@src/types/onyx';
import MoneyRequestReportActionsList from './MoneyRequestReportActionsList';
const loadingAppReasonAttributes: SkeletonSpanReasonAttributes = {context: 'MoneyRequestReportView.isLoadingApp'};
type MoneyRequestReportViewProps = {
/** The report */
report: OnyxEntry<OnyxTypes.Report>;
/** Metadata for report */
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>;
/** Current policy */
policy: OnyxEntry<OnyxTypes.Policy>;
/** Whether Report footer (that includes Composer) should be displayed */
shouldDisplayReportFooter: boolean;
/** The `backTo` route that should be used when clicking back button */
backToRoute: Route | undefined;
/** Callback executed on layout */
onLayout?: (event: LayoutChangeEvent) => void;
};
function goBackFromSearchMoneyRequest() {
const rootState = navigationRef.getRootState();
const lastRoute = rootState.routes.at(-1);
if (!lastRoute) {
Log.hmmm('[goBackFromSearchMoneyRequest()] No last route found in root state.');
return;
}
if (lastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
Navigation.goBack();
return;
}
if (lastRoute?.name !== NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR) {
Log.hmmm('[goBackFromSearchMoneyRequest()] goBackFromSearchMoneyRequest was called from a different navigator than SearchFullscreenNavigator.');
return;
}
if (rootState.routes.length > 1) {
Navigation.goBack();
return;
}
Navigation.goBack(ROUTES.SEARCH_ROOT.getRoute({query: buildCannedSearchQuery()}));
}
function InitialLoadingSkeleton({styles, onLayout, reasonAttributes}: {styles: ThemeStyles; onLayout?: (event: LayoutChangeEvent) => void; reasonAttributes: SkeletonSpanReasonAttributes}) {
return (
<View
style={[styles.flex1]}
onLayout={onLayout}
>
<View style={[styles.appContentHeader, styles.borderBottom]}>
<ReportHeaderSkeletonView
onBackButtonPress={() => {}}
reasonAttributes={reasonAttributes}
/>
</View>
<ReportActionsSkeletonView />
</View>
);
}
function MoneyRequestReportView({report, policy, reportMetadata, shouldDisplayReportFooter, backToRoute, onLayout}: MoneyRequestReportViewProps) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const reportID = report?.reportID;
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const {reportPendingAction, reportErrors} = getReportOfflinePendingActionAndErrors(report);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(report?.chatReportID)}`);
const {reportActions: unfilteredReportActions, hasNewerActions, hasOlderActions} = usePaginatedReportActions(reportID);
const reportActions = useMemo(() => {
return getFilteredReportActionsForReportView(unfilteredReportActions);
}, [unfilteredReportActions]);
const reportTransactions = useReportTransactionsCollection(reportID);
const hasPendingDeletionTransaction = Object.values(reportTransactions ?? {}).some((transaction) => transaction?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
const transactions = useMemo(() => getAllNonDeletedTransactions(reportTransactions, reportActions, isOffline, true), [reportTransactions, reportActions, isOffline]);
const visibleTransactions = useMemo(() => {
if (isOffline) {
return transactions;
}
// When there are no pending delete transactions, which is most of the time, we can return the same transactions keeping the same reference avoiding extra work
const hasPendingDelete = transactions.some((transaction) => transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
if (!hasPendingDelete) {
return transactions;
}
return transactions.filter((transaction) => transaction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
}, [transactions, isOffline]);
const reportTransactionIDs = visibleTransactions.map((transaction) => transaction.transactionID);
const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActions ?? [], isOffline, reportTransactionIDs);
const isSentMoneyReport = useMemo(() => reportActions.some((action) => isSentMoneyReportAction(action)), [reportActions]);
const newTransactions = useNewTransactions(reportMetadata?.hasOnceLoadedReportActions, transactions);
const parentReportAction = useParentReportAction(report);
const lastReportAction = [...reportActions, parentReportAction].find((action) => canEditReportAction(action) && !isMoneyRequestAction(action));
const isLoadingInitialReportActions = reportMetadata?.isLoadingInitialReportActions;
const dismissReportCreationError = useCallback(() => {
goBackFromSearchMoneyRequest();
// eslint-disable-next-line @typescript-eslint/no-deprecated
InteractionManager.runAfterInteractions(() => removeFailedReport(reportID));
}, [reportID]);
// Special case handling a report that is a transaction thread
// If true we will use standard `ReportActionsView` to display report data and a special header, anything else is handled via `MoneyRequestReportActionsList`
const isTransactionThreadView = isReportTransactionThread(report);
// Prevent the empty state flash by ensuring transaction data is fully loaded before deciding which view to render
// We need to wait for both the selector to finish AND ensure we're not in a loading state where transactions could still populate
const shouldWaitForTransactions = shouldWaitForTransactionsUtil(report, transactions, reportMetadata, isOffline);
const shouldShowOpenReportLoadingSkeleton = !!(isLoadingInitialReportActions && reportActions.length === 0 && !isOffline) || shouldWaitForTransactions;
const isEmptyTransactionReport = visibleTransactions && visibleTransactions.length === 0 && transactionThreadReportID === undefined;
const shouldDisplayMoneyRequestActionsList = !!isEmptyTransactionReport || shouldDisplayReportTableView(report, visibleTransactions ?? []);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);
const shouldShowWideRHPReceipt = visibleTransactions.length === 1 && !isSmallScreenWidth && !!transactionThreadReport;
const reportHeaderView = useMemo(
() =>
isTransactionThreadView ? (
<MoneyRequestHeader
report={report}
policy={policy}
parentReportAction={parentReportAction}
onBackButtonPress={() => {
if (!backToRoute) {
goBackFromSearchMoneyRequest();
return;
}
Navigation.goBack(backToRoute);
}}
/>
) : (
<MoneyReportHeader
report={report}
policy={policy}
reportActions={reportActions}
transactionThreadReportID={transactionThreadReportID}
isLoadingInitialReportActions={isLoadingInitialReportActions}
shouldDisplayBackButton
onBackButtonPress={() => {
if (!backToRoute) {
goBackFromSearchMoneyRequest();
return;
}
Navigation.goBack(backToRoute);
}}
/>
),
[backToRoute, isLoadingInitialReportActions, isTransactionThreadView, parentReportAction, policy, report, reportActions, transactionThreadReportID],
);
// We need to cancel telemetry span when user leaves the screen before full report data is loaded
useEffect(() => {
return () => {
cancelSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`);
};
}, [reportID]);
useEffect(() => {
if (!shouldShowOpenReportLoadingSkeleton || !report) {
return;
}
markOpenReportEnd(report, {warm: false});
}, [report, shouldShowOpenReportLoadingSkeleton]);
if (shouldShowOpenReportLoadingSkeleton) {
const skeletonReasonAttributes: SkeletonSpanReasonAttributes = {
context: 'MoneyRequestReportView.InitialLoadingSkeleton',
isLoadingInitialReportActions: !!isLoadingInitialReportActions,
shouldWaitForTransactions,
};
return (
<InitialLoadingSkeleton
styles={styles}
reasonAttributes={skeletonReasonAttributes}
/>
);
}
if (reportActions.length === 0) {
return <ReportActionsSkeletonView shouldAnimate={false} />;
}
if (!report) {
return;
}
if (isLoadingApp) {
return (
<View style={styles.flex1}>
<ReportHeaderSkeletonView reasonAttributes={loadingAppReasonAttributes} />
<ReportActionsSkeletonView />
{shouldDisplayReportFooter ? (
<ReportFooter
report={report}
lastReportAction={lastReportAction}
// If the report is from the 'Send Money' flow, we add the comment to the `iou` report because for these we don't combine reportActions even if there is a single transaction (they always have a single transaction)
transactionThreadReportID={isSentMoneyReport ? undefined : transactionThreadReportID}
/>
) : null}
</View>
);
}
return (
<View style={styles.flex1}>
<OfflineWithFeedback
pendingAction={reportPendingAction ?? report?.pendingFields?.reimbursed}
errors={reportErrors}
needsOffscreenAlphaCompositing
shouldShowErrorMessages={false}
>
{reportHeaderView}
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={reportPendingAction}
errors={reportErrors}
onClose={dismissReportCreationError}
needsOffscreenAlphaCompositing
style={styles.flex1}
contentContainerStyle={styles.flex1}
errorRowStyles={[styles.ph5, styles.mv2]}
>
<View style={[styles.flex1, styles.flexRow]}>
{shouldShowWideRHPReceipt && (
<Animated.View style={styles.wideRHPMoneyRequestReceiptViewContainer}>
<ScrollView contentContainerStyle={styles.wideRHPMoneyRequestReceiptViewScrollViewContainer}>
<MoneyRequestReceiptView
report={transactionThreadReport}
fillSpace
isDisplayedInWideRHP
/>
</ScrollView>
</Animated.View>
)}
<View style={[styles.overflowHidden, styles.justifyContentEnd, styles.flex1]}>
{shouldDisplayMoneyRequestActionsList ? (
<MoneyRequestReportActionsList
report={report}
policy={policy}
transactions={visibleTransactions}
hasPendingDeletionTransaction={hasPendingDeletionTransaction}
newTransactions={newTransactions}
reportActions={reportActions}
hasOlderActions={hasOlderActions}
onLayout={onLayout}
hasNewerActions={hasNewerActions}
showReportActionsLoadingState={isLoadingInitialReportActions && !reportMetadata?.hasOnceLoadedReportActions}
reportPendingAction={reportPendingAction}
/>
) : (
<ReportActionsView
report={report}
reportActions={reportActions}
isLoadingInitialReportActions={reportMetadata?.isLoadingInitialReportActions}
hasNewerActions={hasNewerActions}
hasOlderActions={hasOlderActions}
parentReportAction={parentReportAction}
onLayout={onLayout}
transactionThreadReportID={transactionThreadReportID}
/>
)}
{shouldDisplayReportFooter ? (
<>
<ReportFooter
report={report}
lastReportAction={lastReportAction}
reportTransactions={transactions}
// If the report is from the 'Send Money' flow, we add the comment to the `iou` report because for these we don't combine reportActions even if there is a single transaction (they always have a single transaction)
transactionThreadReportID={isSentMoneyReport ? undefined : transactionThreadReportID}
/>
<PortalHost name="suggestions" />
</>
) : null}
</View>
</View>
</OfflineWithFeedback>
</View>
);
}
export default MoneyRequestReportView;