forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvatarWithDisplayName.tsx
More file actions
371 lines (331 loc) · 15.5 KB
/
AvatarWithDisplayName.tsx
File metadata and controls
371 lines (331 loc) · 15.5 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import React, {useEffect, useRef} from 'react';
import {View} from 'react-native';
import type {ColorValue, GestureResponderEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useReportAttributes from '@hooks/useReportAttributes';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import {getPersonalDetailsForAccountIDs} from '@libs/OptionsListUtils';
import {getReportName} from '@libs/ReportNameUtils';
import type {DisplayNameWithTooltips} from '@libs/ReportUtils';
import {
canEditReportTitle,
getChatRoomSubtitle,
getDisplayNamesWithTooltips,
getParentNavigationSubtitle,
getReportStatusColorStyle,
getReportStatusTranslation,
isChatThread,
isExpenseReport,
isInvoiceReport,
isIOUReport,
isMoneyRequest,
isMoneyRequestReport,
isTrackExpenseReport,
navigateToDetailsPage,
} from '@libs/ReportUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Policy, Report} from '@src/types/onyx';
import {getButtonRole} from './Button/utils';
import DisplayNames from './DisplayNames';
import type DisplayNamesProps from './DisplayNames/types';
import Icon from './Icon';
import ParentNavigationSubtitle from './ParentNavigationSubtitle';
import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback';
import ReportActionAvatars from './ReportActionAvatars';
import type {TransactionListItemType} from './SelectionListWithSections/types';
import Text from './Text';
type AvatarWithDisplayNameProps = {
/** The report currently being looked at */
report: OnyxEntry<Report>;
/** The size of the avatar */
size?: ValueOf<typeof CONST.AVATAR_SIZE>;
/** Whether if it's an unauthenticated user */
isAnonymous?: boolean;
/** Whether we should enable detail page navigation */
shouldEnableDetailPageNavigation?: boolean;
/** Whether the avatar is pressable to open the actor details */
shouldEnableAvatarNavigation?: boolean;
/** Whether we should enable custom title logic designed for search lis */
shouldUseCustomSearchTitleName?: boolean;
/** Whether we should display the status of the report */
shouldDisplayStatus?: boolean;
/** Transactions inside report */
transactions?: TransactionListItemType[];
/** Whether to open the parent report link in the current tab if possible */
openParentReportInCurrentTab?: boolean;
/** Color of the secondary avatar border, usually should match the container background */
avatarBorderColor?: ColorValue;
/** The style of the custom display name text */
customDisplayNameStyle?: TextStyle;
/** The style of the parent navigation subtitle text */
parentNavigationSubtitleTextStyles?: StyleProp<TextStyle>;
/** The style of the parent navigation status container */
parentNavigationStatusContainerStyles?: StyleProp<ViewStyle>;
/** The policy associated with the report */
policy?: OnyxEntry<Policy>;
};
function getCustomDisplayName(
shouldUseCustomSearchTitleName: boolean,
report: OnyxEntry<Report>,
title: string,
displayNamesWithTooltips: DisplayNameWithTooltips,
transactions: TransactionListItemType[],
shouldUseFullTitle: boolean,
customSearchDisplayStyle: TextStyle[],
regularStyle: TextStyle[],
isAnonymous: boolean,
isMoneyRequestOrReport: boolean,
): React.ReactNode {
const reportName = report?.reportName ?? CONST.REPORT.DEFAULT_REPORT_NAME;
const isIOUOrInvoice = report?.type === CONST.REPORT.TYPE.IOU || report?.type === CONST.REPORT.TYPE.INVOICE;
const hasTransactions = transactions.length > 0;
function getDisplayProps(): DisplayNamesProps {
const baseProps = {
displayNamesWithTooltips,
tooltipEnabled: true,
numberOfLines: 1,
};
if (shouldUseCustomSearchTitleName) {
const styleProps = {
textStyles: customSearchDisplayStyle,
};
if (!hasTransactions) {
return {
fullTitle: reportName,
shouldUseFullTitle,
...baseProps,
...styleProps,
};
}
if (isIOUOrInvoice) {
return {
fullTitle: title,
shouldUseFullTitle: true,
...baseProps,
...styleProps,
};
}
return {
fullTitle: reportName,
shouldUseFullTitle,
...baseProps,
...styleProps,
};
}
return {
fullTitle: title,
textStyles: regularStyle,
shouldUseFullTitle: isMoneyRequestOrReport || isAnonymous,
...baseProps,
};
}
const {fullTitle, textStyles, displayNamesWithTooltips: displayNamesWithTooltipsProp, tooltipEnabled, numberOfLines, shouldUseFullTitle: shouldUseFullTitleProp} = getDisplayProps();
return (
<DisplayNames
fullTitle={fullTitle}
displayNamesWithTooltips={displayNamesWithTooltipsProp}
tooltipEnabled={tooltipEnabled}
numberOfLines={numberOfLines}
textStyles={textStyles}
shouldUseFullTitle={shouldUseFullTitleProp}
/>
);
}
function AvatarWithDisplayName({
report,
policy,
isAnonymous = false,
size = CONST.AVATAR_SIZE.DEFAULT,
shouldEnableDetailPageNavigation = false,
shouldEnableAvatarNavigation = true,
shouldUseCustomSearchTitleName = false,
transactions = [],
openParentReportInCurrentTab = false,
avatarBorderColor: avatarBorderColorProp,
shouldDisplayStatus = false,
customDisplayNameStyle = {},
parentNavigationSubtitleTextStyles,
parentNavigationStatusContainerStyles = {},
}: AvatarWithDisplayNameProps) {
const {localeCompare, formatPhoneNumber} = useLocalize();
const [parentReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`, {canEvict: false});
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST) ?? CONST.EMPTY_OBJECT;
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to display the edit button only on large screens
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const reportAttributes = useReportAttributes();
const isReportArchived = useReportIsArchived(report?.reportID);
const title = getReportName(report, reportAttributes);
const isParentReportArchived = useReportIsArchived(report?.parentReportID);
const subtitle = getChatRoomSubtitle(report, true, isReportArchived);
const parentNavigationSubtitleData = getParentNavigationSubtitle(report, isParentReportArchived, reportAttributes);
const isMoneyRequestOrReport = isMoneyRequestReport(report) || isMoneyRequest(report) || isTrackExpenseReport(report) || isInvoiceReport(report);
const ownerPersonalDetails = getPersonalDetailsForAccountIDs(report?.ownerAccountID ? [report.ownerAccountID] : [], personalDetails);
const displayNamesWithTooltips = getDisplayNamesWithTooltips(Object.values(ownerPersonalDetails), false, localeCompare, formatPhoneNumber);
const avatarBorderColor = avatarBorderColorProp ?? (isAnonymous ? theme.highlightBG : theme.componentBG);
const statusText = shouldDisplayStatus ? getReportStatusTranslation({stateNum: report?.stateNum, statusNum: report?.statusNum, translate}) : undefined;
const reportStatusColorStyle = shouldDisplayStatus ? getReportStatusColorStyle(theme, report?.stateNum, report?.statusNum) : {};
const icons = useMemoizedLazyExpensifyIcons(['Pencil']);
const shouldShowReportTitleEditButton = shouldEnableDetailPageNavigation && !isSmallScreenWidth && canEditReportTitle(report, policy);
const actorAccountID = useRef<number | null>(null);
useEffect(() => {
if (!report?.parentReportActionID) {
return;
}
const parentReportAction = parentReportActions?.[report?.parentReportActionID];
actorAccountID.current = parentReportAction?.actorAccountID ?? CONST.DEFAULT_NUMBER_ID;
}, [parentReportActions, report?.parentReportActionID]);
const goToDetailsPage = () => {
navigateToDetailsPage(report, Navigation.getActiveRoute());
};
const navigateToEditReportTitle = (event?: GestureResponderEvent | KeyboardEvent) => {
event?.stopPropagation?.();
Navigation.navigate(ROUTES.EDIT_REPORT_FIELD_REQUEST.getRoute(report?.reportID, report?.policyID, CONST.REPORT_FIELD_TITLE_FIELD_ID, Navigation.getReportRHPActiveRoute()));
};
const showActorDetails = () => {
// We should navigate to the details page if the report is a IOU/expense report
if (shouldEnableDetailPageNavigation) {
goToDetailsPage();
return;
}
if (isExpenseReport(report) && report?.ownerAccountID) {
Navigation.navigate(ROUTES.PROFILE.getRoute(report.ownerAccountID));
return;
}
if (isIOUReport(report) && report?.reportID) {
Navigation.navigate(ROUTES.REPORT_PARTICIPANTS.getRoute(report.reportID));
return;
}
if (isChatThread(report)) {
// In an ideal situation account ID won't be 0
if (actorAccountID.current && actorAccountID.current > 0) {
Navigation.navigate(ROUTES.PROFILE.getRoute(actorAccountID.current));
return;
}
}
if (report?.reportID) {
// Report detail route is added as fallback but based on the current implementation this route won't be executed
Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(report.reportID));
}
};
const shouldUseFullTitle = isMoneyRequestOrReport || isAnonymous;
const displayNameContent = getCustomDisplayName(
shouldUseCustomSearchTitleName,
report,
title,
displayNamesWithTooltips,
transactions,
shouldUseFullTitle,
[styles.headerText, styles.pre, customDisplayNameStyle],
[isAnonymous ? styles.headerAnonymousFooter : styles.headerText, styles.pre],
isAnonymous,
isMoneyRequestOrReport,
);
const multipleAvatars = (
<ReportActionAvatars
singleAvatarContainerStyle={[styles.actionAvatar, styles.mr3]}
subscriptAvatarBorderColor={avatarBorderColor}
size={size}
secondaryAvatarContainerStyle={StyleUtils.getBackgroundAndBorderStyle(avatarBorderColor)}
reportID={report?.reportID}
/>
);
const headerView = (
<View style={[styles.appContentHeaderTitle, styles.flex1]}>
{!!report && !!title && (
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter, styles.justifyContentBetween]}>
<View accessibilityLabel={title}>
{shouldEnableAvatarNavigation ? (
<PressableWithoutFeedback
sentryLabel={CONST.SENTRY_LABEL.AVATAR_WITH_DISPLAY_NAME.SHOW_ACTOR_DETAILS}
onPress={showActorDetails}
accessibilityLabel={title}
role={getButtonRole(true)}
>
{multipleAvatars}
</PressableWithoutFeedback>
) : (
multipleAvatars
)}
</View>
<View style={[styles.flex1, styles.flexColumn]}>
{shouldShowReportTitleEditButton ? (
<PressableWithoutFeedback
sentryLabel={CONST.SENTRY_LABEL.HEADER_VIEW.DETAILS_BUTTON}
onPress={navigateToEditReportTitle}
style={[styles.flexRow, styles.alignItemsCenter, styles.alignSelfStart, styles.mw100]}
accessibilityLabel={title}
role={CONST.ROLE.BUTTON}
>
<View style={[styles.flexShrink1]}>{displayNameContent}</View>
<Icon
src={icons.Pencil}
width={variables.iconSizeExtraSmall}
height={variables.iconSizeExtraSmall}
additionalStyles={styles.ml1}
fill={theme.icon}
/>
</PressableWithoutFeedback>
) : (
displayNameContent
)}
{Object.keys(parentNavigationSubtitleData).length > 0 && (
<ParentNavigationSubtitle
parentNavigationSubtitleData={parentNavigationSubtitleData}
reportID={report?.reportID}
parentReportID={report?.parentReportID}
parentReportActionID={report?.parentReportActionID}
pressableStyles={[styles.alignSelfStart, styles.mw100]}
openParentReportInCurrentTab={openParentReportInCurrentTab}
statusText={statusText}
textStyles={parentNavigationSubtitleTextStyles}
statusTextContainerStyles={parentNavigationStatusContainerStyles}
statusTextColor={reportStatusColorStyle?.textColor}
statusTextBackgroundColor={reportStatusColorStyle?.backgroundColor}
/>
)}
{!!subtitle && (
<Text
style={[styles.sidebarLinkText, styles.optionAlternateText, styles.textLabelSupporting, styles.pre]}
numberOfLines={1}
>
{subtitle}
</Text>
)}
</View>
</View>
)}
</View>
);
if (!shouldEnableDetailPageNavigation) {
return headerView;
}
return (
<PressableWithoutFeedback
sentryLabel={CONST.SENTRY_LABEL.AVATAR_WITH_DISPLAY_NAME.GO_TO_DETAILS_PAGE}
onPress={goToDetailsPage}
style={[styles.flexRow, styles.alignItemsCenter, styles.flex1]}
accessibilityLabel={title}
role={CONST.ROLE.BUTTON}
>
{headerView}
</PressableWithoutFeedback>
);
}
export default AvatarWithDisplayName;