-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
OptionRowLHNData.tsx
95 lines (86 loc) · 3.18 KB
/
OptionRowLHNData.tsx
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
import {deepEqual} from 'fast-equals';
import React, {useMemo, useRef} from 'react';
import useCurrentReportID from '@hooks/useCurrentReportID';
import * as ReportUtils from '@libs/ReportUtils';
import SidebarUtils from '@libs/SidebarUtils';
import CONST from '@src/CONST';
import type {OptionData} from '@src/libs/ReportUtils';
import OptionRowLHN from './OptionRowLHN';
import type {OptionRowLHNDataProps} from './types';
/*
* This component gets the data from onyx for the actual
* OptionRowLHN component.
* The OptionRowLHN component is memoized, so it will only
* re-render if the data really changed.
*/
function OptionRowLHNData({
isFocused = false,
fullReport,
reportActions,
personalDetails = {},
preferredLocale = CONST.LOCALES.DEFAULT,
policy,
receiptTransactions,
parentReportAction,
transaction,
lastReportActionTransaction,
transactionViolations,
canUseViolations,
...propsToForward
}: OptionRowLHNDataProps) {
const reportID = propsToForward.reportID;
const currentReportIDValue = useCurrentReportID();
const isReportFocused = isFocused && currentReportIDValue?.currentReportID === reportID;
const optionItemRef = useRef<OptionData>();
const shouldDisplayViolations = canUseViolations && ReportUtils.shouldDisplayTransactionThreadViolations(fullReport, transactionViolations, parentReportAction);
const optionItem = useMemo(() => {
// Note: ideally we'd have this as a dependent selector in onyx!
const item = SidebarUtils.getOptionData({
report: fullReport,
reportActions,
personalDetails,
preferredLocale: preferredLocale ?? CONST.LOCALES.DEFAULT,
policy,
parentReportAction,
hasViolations: !!shouldDisplayViolations,
transactionViolations,
});
if (deepEqual(item, optionItemRef.current)) {
return optionItemRef.current;
}
optionItemRef.current = item;
return item;
// Listen parentReportAction to update title of thread report when parentReportAction changed
// Listen to transaction to update title of transaction report when transaction changed
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [
fullReport,
lastReportActionTransaction,
reportActions,
personalDetails,
preferredLocale,
policy,
parentReportAction,
transaction,
transactionViolations,
canUseViolations,
receiptTransactions,
]);
return (
<OptionRowLHN
// eslint-disable-next-line react/jsx-props-no-spreading
{...propsToForward}
isFocused={isReportFocused}
optionItem={optionItem}
/>
);
}
OptionRowLHNData.displayName = 'OptionRowLHNData';
/**
* This component is rendered in a list.
* On scroll we want to avoid that a item re-renders
* just because the list has to re-render when adding more items.
* Thats also why the React.memo is used on the outer component here, as we just
* use it to prevent re-renders from parent re-renders.
*/
export default React.memo(OptionRowLHNData);