-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportActionItemMessage.tsx
160 lines (138 loc) · 6.92 KB
/
ReportActionItemMessage.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
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
import type {ReactElement} from 'react';
import React from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ReportAction} from '@src/types/onyx';
import TextCommentFragment from './comment/TextCommentFragment';
import ReportActionItemFragment from './ReportActionItemFragment';
type ReportActionItemMessageProps = {
/** The report action */
action: ReportAction;
/** Should the comment have the appearance of being grouped with the previous comment? */
displayAsGroup: boolean;
/** Additional styles to add after local styles. */
style?: StyleProp<ViewStyle & TextStyle>;
/** Whether or not the message is hidden by moderation */
isHidden?: boolean;
/** The ID of the report */
reportID: string;
};
function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHidden = false}: ReportActionItemMessageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${ReportActionsUtils.getLinkedTransactionID(action) ?? -1}`);
const fragments = ReportActionsUtils.getReportActionMessageFragments(action);
const isIOUReport = ReportActionsUtils.isMoneyRequestAction(action);
if (ReportActionsUtils.isMemberChangeAction(action)) {
const fragment = ReportActionsUtils.getMemberChangeMessageFragment(action);
return (
<View style={[styles.chatItemMessage, style]}>
<TextCommentFragment
fragment={fragment}
displayAsGroup={displayAsGroup}
style={style}
source=""
styleAsDeleted={false}
/>
</View>
);
}
if (action.actionName === CONST.REPORT.ACTIONS.TYPE.ROOM_CHANGE_LOG.UPDATE_ROOM_DESCRIPTION) {
const fragment = ReportActionsUtils.getUpdateRoomDescriptionFragment(action);
return (
<View style={[styles.chatItemMessage, style]}>
<TextCommentFragment
fragment={fragment}
displayAsGroup={displayAsGroup}
style={style}
source=""
styleAsDeleted={false}
/>
</View>
);
}
let iouMessage: string | undefined;
if (isIOUReport) {
const originalMessage = action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU ? ReportActionsUtils.getOriginalMessage(action) : null;
const iouReportID = originalMessage?.IOUReportID;
if (iouReportID) {
iouMessage = ReportUtils.getIOUReportActionDisplayMessage(action, transaction);
}
}
const isApprovedOrSubmittedReportAction = ReportActionsUtils.isApprovedOrSubmittedReportAction(action);
const isHoldReportAction = [CONST.REPORT.ACTIONS.TYPE.HOLD, CONST.REPORT.ACTIONS.TYPE.UNHOLD].some((type) => type === action.actionName);
/**
* Get the ReportActionItemFragments
* @param shouldWrapInText determines whether the fragments are wrapped in a Text component
* @returns report action item fragments
*/
const renderReportActionItemFragments = (shouldWrapInText: boolean): ReactElement | ReactElement[] => {
const reportActionItemFragments = fragments.map((fragment, index) => (
<ReportActionItemFragment
/* eslint-disable-next-line react/no-array-index-key */
key={`actionFragment-${action.reportActionID}-${index}`}
fragment={fragment}
iouMessage={iouMessage}
isThreadParentMessage={ReportActionsUtils.isThreadParentMessage(action, reportID)}
pendingAction={action.pendingAction}
actionName={action.actionName}
source={ReportActionsUtils.isAddCommentAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.source : ''}
accountID={action.actorAccountID ?? -1}
style={style}
displayAsGroup={displayAsGroup}
isApprovedOrSubmittedReportAction={isApprovedOrSubmittedReportAction}
isHoldReportAction={isHoldReportAction}
// Since system messages from Old Dot begin with the person who performed the action,
// the first fragment will contain the person's display name and their email. We'll use this
// to decide if the fragment should be from left to right for RTL display names e.g. Arabic for proper
// formatting.
isFragmentContainingDisplayName={index === 0}
moderationDecision={ReportActionsUtils.getReportActionMessage(action)?.moderationDecision?.decision}
/>
));
// Approving or submitting reports in oldDot results in system messages made up of multiple fragments of `TEXT` type
// which we need to wrap in `<Text>` to prevent them rendering on separate lines.
return shouldWrapInText ? <Text style={styles.ltr}>{reportActionItemFragments}</Text> : reportActionItemFragments;
};
const openWorkspaceInvoicesPage = () => {
const policyID = report?.policyID;
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_INVOICES.getRoute(policyID));
};
const shouldShowAddBankAccountButton = action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU && ReportUtils.hasMissingInvoiceBankAccount(reportID) && !ReportUtils.isSettled(reportID);
return (
<View style={[styles.chatItemMessage, style]}>
{!isHidden ? (
<>
{renderReportActionItemFragments(isApprovedOrSubmittedReportAction)}
{shouldShowAddBankAccountButton && (
<Button
style={[styles.mt2, styles.alignSelfStart]}
success
text={translate('workspace.invoices.paymentMethods.addBankAccount')}
onPress={openWorkspaceInvoicesPage}
/>
)}
</>
) : (
<Text style={[styles.textLabelSupporting, styles.lh20]}>{translate('moderation.flaggedContent')}</Text>
)}
</View>
);
}
ReportActionItemMessage.displayName = 'ReportActionItemMessage';
export default ReportActionItemMessage;