forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportActionItemSingle.tsx
282 lines (257 loc) · 12.5 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
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 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 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 {getReportActionMessage} from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
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: 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: string) => {
Navigation.navigate(ROUTES.PROFILE.getRoute(accountID));
};
const showWorkspaceDetails = (reportID: string) => {
Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(reportID));
};
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.EMPTY_OBJECT;
const actorAccountID = ReportUtils.getReportActionActorAccountID(action, iouReport);
let displayName = ReportUtils.getDisplayNameForParticipant(actorAccountID);
const {avatar, login, pendingFields, status, fallbackIcon} = personalDetails[actorAccountID ?? -1] ?? {};
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
let actorHint = (login || (displayName ?? '')).replace(CONST.REGEX.MERGED_ACCOUNT_PREFIX, '');
const displayAllActors = useMemo(() => action?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW, [action?.actionName]);
const isInvoiceReport = ReportUtils.isInvoiceReport(iouReport ?? {});
const isWorkspaceActor = isInvoiceReport || (ReportUtils.isPolicyExpenseChat(report) && (!actorAccountID || displayAllActors));
const ownerAccountID = iouReport?.ownerAccountID ?? action?.childOwnerAccountID;
let avatarSource = avatar;
let avatarId: number | string | undefined = actorAccountID;
if (isWorkspaceActor) {
displayName = ReportUtils.getPolicyName(report);
actorHint = displayName;
avatarSource = ReportUtils.getWorkspaceAvatar(report);
avatarId = report.policyID;
} else if (action?.delegateAccountID && personalDetails[action?.delegateAccountID]) {
// We replace the actor's email, name, and avatar with the Copilot manually for now. And only if we have their
// details. This will be improved upon when the Copilot feature is implemented.
const delegateDetails = personalDetails[action.delegateAccountID];
const delegateDisplayName = delegateDetails?.displayName;
actorHint = `${delegateDisplayName} (${translate('reportAction.asCopilot')} ${displayName})`;
displayName = actorHint;
avatarSource = delegateDetails?.avatar;
avatarId = action.delegateAccountID;
}
// If this is a report preview, display names and avatars of both people involved
let secondaryAvatar: Icon;
const primaryDisplayName = displayName;
if (displayAllActors) {
// 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 = ReportUtils.getDisplayNameForParticipant(secondaryAccountId);
if (!isInvoiceReport) {
displayName = `${primaryDisplayName} & ${secondaryDisplayName}`;
}
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 || ReportUtils.isPolicyExpenseChat(report) ? 0 : 1;
const reportIcons = ReportUtils.getIcons(report, {});
secondaryAvatar = reportIcons[avatarIconIndex];
} 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));
return;
}
showUserDetails(action?.delegateAccountID ? String(action.delegateAccountID) : String(actorAccountID));
}
}, [isWorkspaceActor, reportID, actorAccountID, action?.delegateAccountID, iouReportID, displayAllActors]);
const shouldDisableDetailPage = useMemo(
() =>
CONST.RESTRICTED_ACCOUNT_IDS.includes(actorAccountID ?? -1) ||
(!isWorkspaceActor && ReportUtils.isOptimisticPersonalDetail(action?.delegateAccountID ? Number(action.delegateAccountID) : actorAccountID ?? -1)),
[action, isWorkspaceActor, actorAccountID],
);
const getAvatar = () => {
if (displayAllActors) {
return (
<MultipleAvatars
icons={[icon, secondaryAvatar]}
isInReportAction
shouldShowTooltip
secondAvatarStyle={[StyleUtils.getBackgroundAndBorderStyle(theme.appBG), isHovered ? StyleUtils.getBackgroundAndBorderStyle(theme.hoverComponentBG) : undefined]}
/>
);
}
if (shouldShowSubscriptAvatar) {
return (
<SubscriptAvatar
mainAvatar={icon}
secondaryAvatar={secondaryAvatar}
noMargin
/>
);
}
return (
<UserDetailsTooltip
accountID={Number(actorAccountID ?? -1)}
delegateAccountID={Number(action?.delegateAccountID ?? -1)}
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={actorAccountID ?? -1}
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}
<View style={hasBeenFlagged ? styles.blockquote : {}}>{children}</View>
</View>
</View>
);
}
ReportActionItemSingle.displayName = 'ReportActionItemSingle';
export default ReportActionItemSingle;