-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportActionItemSingle.tsx
330 lines (304 loc) · 14.8 KB
/
ReportActionItemSingle.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
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
import React, {useCallback, useMemo} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import Avatar from '@components/Avatar';
import {FallbackAvatar} from '@components/Icon/Expensicons';
import MultipleAvatars from '@components/MultipleAvatars';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import {usePersonalDetails} from '@components/OnyxProvider';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import SubscriptAvatar from '@components/SubscriptAvatar';
import Text from '@components/Text';
import Tooltip from '@components/Tooltip';
import UserDetailsTooltip from '@components/UserDetailsTooltip';
import useLocalize from '@hooks/useLocalize';
import usePolicy from '@hooks/usePolicy';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import ControlSelection from '@libs/ControlSelection';
import DateUtils from '@libs/DateUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getPersonalDetailByEmail} from '@libs/PersonalDetailsUtils';
import {getReportActionMessage} from '@libs/ReportActionsUtils';
import {
getDefaultWorkspaceAvatar,
getDisplayNameForParticipant,
getIcons,
getPolicyName,
getReportActionActorAccountID,
getWorkspaceIcon,
isIndividualInvoiceRoom,
isInvoiceReport as isInvoiceReportUtils,
isInvoiceRoom,
isOptimisticPersonalDetail,
isPolicyExpenseChat,
isTripRoom as isTripRoomReportUtils,
} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Report, ReportAction} from '@src/types/onyx';
import type {Icon} from '@src/types/onyx/OnyxCommon';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import ReportActionItemDate from './ReportActionItemDate';
import ReportActionItemFragment from './ReportActionItemFragment';
type ReportActionItemSingleProps = Partial<ChildrenProps> & {
/** All the data of the action */
action: OnyxEntry<ReportAction>;
/** Styles for the outermost View */
wrapperStyle?: StyleProp<ViewStyle>;
/** Report for this action */
report: OnyxEntry<Report>;
/** IOU Report for this action, if any */
iouReport?: OnyxEntry<Report>;
/** Show header for action */
showHeader?: boolean;
/** Determines if the avatar is displayed as a subscript (positioned lower than normal) */
shouldShowSubscriptAvatar?: boolean;
/** If the message has been flagged for moderation */
hasBeenFlagged?: boolean;
/** If the action is being hovered */
isHovered?: boolean;
};
const showUserDetails = (accountID: number | undefined) => {
Navigation.navigate(ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute()));
};
const showWorkspaceDetails = (reportID: string | undefined) => {
if (!reportID) {
return;
}
Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(reportID, Navigation.getReportRHPActiveRoute()));
};
function ReportActionItemSingle({
action,
children,
wrapperStyle,
showHeader = true,
shouldShowSubscriptAvatar = false,
hasBeenFlagged = false,
report,
iouReport,
isHovered = false,
}: ReportActionItemSingleProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const personalDetails = usePersonalDetails();
const policy = usePolicy(report?.policyID);
const delegatePersonalDetails = action?.delegateAccountID ? personalDetails?.[action?.delegateAccountID] : undefined;
const ownerAccountID = iouReport?.ownerAccountID ?? action?.childOwnerAccountID;
const isReportPreviewAction = action?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW;
const actorAccountID = getReportActionActorAccountID(action, iouReport, report);
const [invoiceReceiverPolicy] = useOnyx(
`${ONYXKEYS.COLLECTION.POLICY}${report?.invoiceReceiver && 'policyID' in report.invoiceReceiver ? report.invoiceReceiver.policyID : CONST.DEFAULT_NUMBER_ID}`,
);
let displayName = getDisplayNameForParticipant({accountID: actorAccountID});
const {avatar, login, pendingFields, status, fallbackIcon} = personalDetails?.[actorAccountID ?? CONST.DEFAULT_NUMBER_ID] ?? {};
const accountOwnerDetails = getPersonalDetailByEmail(login ?? '');
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
let actorHint = (login || (displayName ?? '')).replace(CONST.REGEX.MERGED_ACCOUNT_PREFIX, '');
const isTripRoom = isTripRoomReportUtils(report);
const displayAllActors = isReportPreviewAction && !isTripRoom && !isPolicyExpenseChat(report);
const isInvoiceReport = isInvoiceReportUtils(iouReport ?? null);
const isWorkspaceActor = isInvoiceReport || (isPolicyExpenseChat(report) && (!actorAccountID || displayAllActors));
let avatarSource = avatar;
let avatarId: number | string | undefined = actorAccountID;
if (isWorkspaceActor) {
displayName = getPolicyName({report, policy});
actorHint = displayName;
avatarSource = getWorkspaceIcon(report, policy).source;
avatarId = report?.policyID;
} else if (delegatePersonalDetails) {
displayName = delegatePersonalDetails?.displayName ?? '';
avatarSource = delegatePersonalDetails?.avatar;
avatarId = delegatePersonalDetails?.accountID;
} else if (isReportPreviewAction && isTripRoom) {
displayName = report?.reportName ?? '';
avatarSource = personalDetails?.[ownerAccountID ?? CONST.DEFAULT_NUMBER_ID]?.avatar;
avatarId = ownerAccountID;
}
// If this is a report preview, display names and avatars of both people involved
let secondaryAvatar: Icon;
const primaryDisplayName = displayName;
if (displayAllActors) {
if (isInvoiceRoom(report) && !isIndividualInvoiceRoom(report)) {
const secondaryPolicyAvatar = invoiceReceiverPolicy?.avatarURL ?? getDefaultWorkspaceAvatar(invoiceReceiverPolicy?.name);
secondaryAvatar = {
source: secondaryPolicyAvatar,
type: CONST.ICON_TYPE_WORKSPACE,
name: invoiceReceiverPolicy?.name,
id: invoiceReceiverPolicy?.id,
};
} else {
// The ownerAccountID and actorAccountID can be the same if a user submits an expense back from the IOU's original creator, in that case we need to use managerID to avoid displaying the same user twice
const secondaryAccountId = ownerAccountID === actorAccountID || isInvoiceReport ? actorAccountID : ownerAccountID;
const secondaryUserAvatar = personalDetails?.[secondaryAccountId ?? -1]?.avatar ?? FallbackAvatar;
const secondaryDisplayName = getDisplayNameForParticipant({accountID: secondaryAccountId});
secondaryAvatar = {
source: secondaryUserAvatar,
type: CONST.ICON_TYPE_AVATAR,
name: secondaryDisplayName ?? '',
id: secondaryAccountId,
};
}
} else if (!isWorkspaceActor) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const avatarIconIndex = report?.isOwnPolicyExpenseChat || isPolicyExpenseChat(report) ? 0 : 1;
const reportIcons = getIcons(report, {});
secondaryAvatar = reportIcons.at(avatarIconIndex) ?? {name: '', source: '', type: CONST.ICON_TYPE_AVATAR};
} else if (isInvoiceReportUtils(iouReport)) {
const secondaryAccountId = iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID;
const secondaryUserAvatar = personalDetails?.[secondaryAccountId ?? -1]?.avatar ?? FallbackAvatar;
const secondaryDisplayName = getDisplayNameForParticipant({accountID: secondaryAccountId});
secondaryAvatar = {
source: secondaryUserAvatar,
type: CONST.ICON_TYPE_AVATAR,
name: secondaryDisplayName,
id: secondaryAccountId,
};
} else {
secondaryAvatar = {name: '', source: '', type: 'avatar'};
}
const icon = {
source: avatarSource ?? FallbackAvatar,
type: isWorkspaceActor ? CONST.ICON_TYPE_WORKSPACE : CONST.ICON_TYPE_AVATAR,
name: primaryDisplayName ?? '',
id: avatarId,
};
// Since the display name for a report action message is delivered with the report history as an array of fragments
// we'll need to take the displayName from personal details and have it be in the same format for now. Eventually,
// we should stop referring to the report history items entirely for this information.
const personArray = displayName
? [
{
type: 'TEXT',
text: displayName,
},
]
: action?.person;
const reportID = report?.reportID;
const iouReportID = iouReport?.reportID;
const showActorDetails = useCallback(() => {
if (isWorkspaceActor) {
showWorkspaceDetails(reportID);
} else {
// Show participants page IOU report preview
if (iouReportID && displayAllActors) {
Navigation.navigate(ROUTES.REPORT_PARTICIPANTS.getRoute(iouReportID, Navigation.getReportRHPActiveRoute()));
return;
}
showUserDetails(Number(icon.id));
}
}, [isWorkspaceActor, reportID, iouReportID, displayAllActors, icon.id]);
const shouldDisableDetailPage = useMemo(
() =>
CONST.RESTRICTED_ACCOUNT_IDS.includes(actorAccountID ?? CONST.DEFAULT_NUMBER_ID) ||
(!isWorkspaceActor && isOptimisticPersonalDetail(action?.delegateAccountID ? Number(action.delegateAccountID) : actorAccountID ?? CONST.DEFAULT_NUMBER_ID)),
[action, isWorkspaceActor, actorAccountID],
);
const getAvatar = () => {
if (shouldShowSubscriptAvatar) {
return (
<SubscriptAvatar
mainAvatar={icon}
secondaryAvatar={secondaryAvatar}
noMargin
/>
);
}
if (displayAllActors) {
return (
<MultipleAvatars
icons={[icon, secondaryAvatar]}
isInReportAction
shouldShowTooltip
secondAvatarStyle={[StyleUtils.getBackgroundAndBorderStyle(theme.appBG), isHovered ? StyleUtils.getBackgroundAndBorderStyle(theme.hoverComponentBG) : undefined]}
/>
);
}
return (
<UserDetailsTooltip
accountID={Number(delegatePersonalDetails && !isWorkspaceActor ? actorAccountID : icon.id ?? CONST.DEFAULT_NUMBER_ID)}
delegateAccountID={action?.delegateAccountID}
icon={icon}
>
<View>
<Avatar
containerStyles={[styles.actionAvatar]}
source={icon.source}
type={icon.type}
name={icon.name}
avatarID={icon.id}
fallbackIcon={fallbackIcon}
/>
</View>
</UserDetailsTooltip>
);
};
const hasEmojiStatus = !displayAllActors && status?.emojiCode;
const formattedDate = DateUtils.getStatusUntilDate(status?.clearAfter ?? '');
const statusText = status?.text ?? '';
const statusTooltipText = formattedDate ? `${statusText ? `${statusText} ` : ''}(${formattedDate})` : statusText;
return (
<View style={[styles.chatItem, wrapperStyle]}>
<PressableWithoutFeedback
style={[styles.alignSelfStart, styles.mr3]}
onPressIn={ControlSelection.block}
onPressOut={ControlSelection.unblock}
onPress={showActorDetails}
disabled={shouldDisableDetailPage}
accessibilityLabel={actorHint}
role={CONST.ROLE.BUTTON}
>
<OfflineWithFeedback pendingAction={pendingFields?.avatar ?? undefined}>{getAvatar()}</OfflineWithFeedback>
</PressableWithoutFeedback>
<View style={[styles.chatItemRight]}>
{showHeader ? (
<View style={[styles.chatItemMessageHeader]}>
<PressableWithoutFeedback
style={[styles.flexShrink1, styles.mr1]}
onPressIn={ControlSelection.block}
onPressOut={ControlSelection.unblock}
onPress={showActorDetails}
disabled={shouldDisableDetailPage}
accessibilityLabel={actorHint}
role={CONST.ROLE.BUTTON}
>
{personArray?.map((fragment, index) => (
<ReportActionItemFragment
// eslint-disable-next-line react/no-array-index-key
key={`person-${action?.reportActionID}-${index}`}
accountID={Number(delegatePersonalDetails && !isWorkspaceActor ? actorAccountID : icon.id ?? CONST.DEFAULT_NUMBER_ID)}
fragment={{...fragment, type: fragment.type ?? '', text: fragment.text ?? ''}}
delegateAccountID={action?.delegateAccountID}
isSingleLine
actorIcon={icon}
moderationDecision={getReportActionMessage(action)?.moderationDecision?.decision}
/>
))}
</PressableWithoutFeedback>
{!!hasEmojiStatus && (
<Tooltip text={statusTooltipText}>
<Text
style={styles.userReportStatusEmoji}
numberOfLines={1}
>{`${status?.emojiCode}`}</Text>
</Tooltip>
)}
<ReportActionItemDate created={action?.created ?? ''} />
</View>
) : null}
{!!action?.delegateAccountID && (
<Text style={[styles.chatDelegateMessage]}>{translate('delegate.onBehalfOfMessage', {delegator: accountOwnerDetails?.displayName ?? ''})}</Text>
)}
<View style={hasBeenFlagged ? styles.blockquote : {}}>{children}</View>
</View>
</View>
);
}
ReportActionItemSingle.displayName = 'ReportActionItemSingle';
export default ReportActionItemSingle;