-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceMembersPage.tsx
575 lines (516 loc) · 24.4 KB
/
WorkspaceMembersPage.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import type {StackScreenProps} from '@react-navigation/stack';
import lodashIsEqual from 'lodash/isEqual';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {TextInput} from 'react-native';
import {InteractionManager, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import Badge from '@components/Badge';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import Button from '@components/Button';
import ButtonWithDropdownMenu from '@components/ButtonWithDropdownMenu';
import type {DropdownOption, WorkspaceMemberBulkActionType} from '@components/ButtonWithDropdownMenu/types';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
import MessagesRow from '@components/MessagesRow';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import TableListItem from '@components/SelectionList/TableListItem';
import type {ListItem} from '@components/SelectionList/types';
import Text from '@components/Text';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePrevious from '@hooks/usePrevious';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import type {CentralPaneNavigatorParamList} from '@libs/Navigation/types';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as UserUtils from '@libs/UserUtils';
import * as Policy from '@userActions/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PersonalDetailsList, PolicyMember, PolicyMembers, Session} from '@src/types/onyx';
import type {Errors, PendingAction} from '@src/types/onyx/OnyxCommon';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {WithPolicyAndFullscreenLoadingProps} from './withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading';
type WorkspaceMembersPageOnyxProps = {
/** Personal details of all users */
personalDetails: OnyxEntry<PersonalDetailsList>;
/** Session info for the currently logged in user. */
session: OnyxEntry<Session>;
};
type WorkspaceMembersPageProps = WithPolicyAndFullscreenLoadingProps &
WithCurrentUserPersonalDetailsProps &
WorkspaceMembersPageOnyxProps &
StackScreenProps<CentralPaneNavigatorParamList, typeof SCREENS.WORKSPACE.MEMBERS>;
/**
* Inverts an object, equivalent of _.invert
*/
function invertObject(object: Record<string, string>): Record<string, string> {
const invertedEntries = Object.entries(object).map(([key, value]) => [value, key]);
const inverted: Record<string, string> = Object.fromEntries(invertedEntries);
return inverted;
}
type MemberOption = Omit<ListItem, 'accountID'> & {accountID: number};
function WorkspaceMembersPage({policyMembers, personalDetails, route, policy, session, currentUserPersonalDetails, isLoadingReportData = true}: WorkspaceMembersPageProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [selectedEmployees, setSelectedEmployees] = useState<number[]>([]);
const [removeMembersConfirmModalVisible, setRemoveMembersConfirmModalVisible] = useState(false);
const [errors, setErrors] = useState({});
const {isOffline} = useNetwork();
const prevIsOffline = usePrevious(isOffline);
const accountIDs = useMemo(() => Object.keys(policyMembers ?? {}).map((accountID) => Number(accountID)), [policyMembers]);
const prevAccountIDs = usePrevious(accountIDs);
const textInputRef = useRef<TextInput>(null);
const isOfflineAndNoMemberDataAvailable = isEmptyObject(policyMembers) && isOffline;
const prevPersonalDetails = usePrevious(personalDetails);
const {translate, formatPhoneNumber, preferredLocale} = useLocalize();
const {isSmallScreenWidth} = useWindowDimensions();
const dropdownButtonRef = useRef(null);
const isPolicyAdmin = PolicyUtils.isPolicyAdmin(policy);
/**
* Get filtered personalDetails list with current policyMembers
*/
const filterPersonalDetails = (members: OnyxEntry<PolicyMembers>, details: OnyxEntry<PersonalDetailsList>): PersonalDetailsList =>
Object.keys(members ?? {}).reduce((result, key) => {
if (details?.[key]) {
return {
...result,
[key]: details[key],
};
}
return result;
}, {});
/**
* Get members for the current workspace
*/
const getWorkspaceMembers = useCallback(() => {
Policy.openWorkspaceMembersPage(route.params.policyID, Object.keys(PolicyUtils.getMemberAccountIDsForWorkspace(policyMembers, personalDetails)));
}, [route.params.policyID, policyMembers, personalDetails]);
/**
* Check if the current selection includes members that cannot be removed
*/
const validateSelection = useCallback(() => {
const newErrors: Errors = {};
selectedEmployees.forEach((member) => {
if (member !== policy?.ownerAccountID && member !== session?.accountID) {
return;
}
newErrors[member] = translate('workspace.people.error.cannotRemove');
});
setErrors(newErrors);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedEmployees, policy?.owner, session?.accountID]);
useEffect(() => {
getWorkspaceMembers();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
validateSelection();
}, [preferredLocale, validateSelection]);
useEffect(() => {
if (removeMembersConfirmModalVisible && !lodashIsEqual(accountIDs, prevAccountIDs)) {
setRemoveMembersConfirmModalVisible(false);
}
setSelectedEmployees((prevSelected) => {
// Filter all personal details in order to use the elements needed for the current workspace
const currentPersonalDetails = filterPersonalDetails(policyMembers, personalDetails);
// We need to filter the previous selected employees by the new personal details, since unknown/new user id's change when transitioning from offline to online
const prevSelectedElements = prevSelected.map((id) => {
const prevItem = prevPersonalDetails?.id;
const res = Object.values(currentPersonalDetails).find((item) => prevItem?.login === item?.login);
return res?.accountID ?? id;
});
// This is an equivalent of the lodash intersection function. The reduce method below is used to filter the items that exist in both arrays.
return [prevSelectedElements, Object.values(PolicyUtils.getMemberAccountIDsForWorkspace(policyMembers, personalDetails))].reduce((prev, members) =>
prev.filter((item) => members.includes(item)),
);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [policyMembers]);
useEffect(() => {
const isReconnecting = prevIsOffline && !isOffline;
if (!isReconnecting) {
return;
}
getWorkspaceMembers();
}, [isOffline, prevIsOffline, getWorkspaceMembers]);
/**
* Open the modal to invite a user
*/
const inviteUser = () => {
Navigation.navigate(ROUTES.WORKSPACE_INVITE.getRoute(route.params.policyID));
};
/**
* Remove selected users from the workspace
* Please see https://github.com/Expensify/App/blob/main/README.md#Security for more details
*/
const removeUsers = () => {
if (!isEmptyObject(errors)) {
return;
}
// Remove the admin from the list
const accountIDsToRemove = session?.accountID ? selectedEmployees.filter((id) => id !== session.accountID) : selectedEmployees;
Policy.removeMembers(accountIDsToRemove, route.params.policyID);
setSelectedEmployees([]);
setRemoveMembersConfirmModalVisible(false);
};
/**
* Show the modal to confirm removal of the selected members
*/
const askForConfirmationToRemove = () => {
if (!isEmptyObject(errors)) {
return;
}
setRemoveMembersConfirmModalVisible(true);
};
/**
* Add or remove all users passed from the selectedEmployees list
*/
const toggleAllUsers = (memberList: MemberOption[]) => {
const enabledAccounts = memberList.filter((member) => !member.isDisabled);
const everyoneSelected = enabledAccounts.every((member) => selectedEmployees.includes(member.accountID));
if (everyoneSelected) {
setSelectedEmployees([]);
} else {
const everyAccountId = enabledAccounts.map((member) => member.accountID);
setSelectedEmployees(everyAccountId);
}
validateSelection();
};
/**
* Add user from the selectedEmployees list
*/
const addUser = useCallback(
(accountID: number) => {
setSelectedEmployees((prevSelected) => [...prevSelected, accountID]);
validateSelection();
},
[validateSelection],
);
/**
* Remove user from the selectedEmployees list
*/
const removeUser = useCallback(
(accountID: number) => {
setSelectedEmployees((prevSelected) => prevSelected.filter((id) => id !== accountID));
validateSelection();
},
[validateSelection],
);
/**
* Toggle user from the selectedEmployees list
*/
const toggleUser = useCallback(
(accountID: number, pendingAction?: PendingAction) => {
if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return;
}
// Add or remove the user if the checkbox is enabled
if (selectedEmployees.includes(accountID)) {
removeUser(accountID);
} else {
addUser(accountID);
}
},
[selectedEmployees, addUser, removeUser],
);
/** Opens the member details page */
const openMemberDetails = useCallback(
(item: MemberOption) => {
if (!isPolicyAdmin) {
Navigation.navigate(ROUTES.PROFILE.getRoute(item.accountID));
return;
}
if (!PolicyUtils.isPaidGroupPolicy(policy)) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_MEMBER_DETAILS.getRoute(route.params.policyID, item.accountID, Navigation.getActiveRoute()));
},
[isPolicyAdmin, policy, route.params.policyID],
);
/**
* Dismisses the errors on one item
*/
const dismissError = useCallback(
(item: MemberOption) => {
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
Policy.clearDeleteMemberError(route.params.policyID, item.accountID);
} else {
Policy.clearAddMemberError(route.params.policyID, item.accountID);
}
},
[route.params.policyID],
);
/**
* Check if the policy member is deleted from the workspace
*/
const isDeletedPolicyMember = (policyMember: PolicyMember): boolean =>
!isOffline && policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && isEmptyObject(policyMember.errors);
const policyOwner = policy?.owner;
const currentUserLogin = currentUserPersonalDetails.login;
const policyID = route.params.policyID;
const invitedPrimaryToSecondaryLogins = invertObject(policy?.primaryLoginsInvited ?? {});
const getUsers = (): MemberOption[] => {
let result: MemberOption[] = [];
Object.entries(policyMembers ?? {}).forEach(([accountIDKey, policyMember]) => {
const accountID = Number(accountIDKey);
if (isDeletedPolicyMember(policyMember)) {
return;
}
const details = personalDetails?.[accountID];
if (!details) {
Log.hmmm(`[WorkspaceMembersPage] no personal details found for policy member with accountID: ${accountID}`);
return;
}
// If this policy is owned by Expensify then show all support (expensify.com or team.expensify.com) emails
// We don't want to show guides as policy members unless the user is a guide. Some customers get confused when they
// see random people added to their policy, but guides having access to the policies help set them up.
if (PolicyUtils.isExpensifyTeam(details?.login ?? details?.displayName)) {
if (policyOwner && currentUserLogin && !PolicyUtils.isExpensifyTeam(policyOwner) && !PolicyUtils.isExpensifyTeam(currentUserLogin)) {
return;
}
}
const isSelected = selectedEmployees.includes(accountID);
const isOwner = policy?.owner === details.login;
const isAdmin = policyMember.role === CONST.POLICY.ROLE.ADMIN;
let roleBadge = null;
if (isOwner || isAdmin) {
roleBadge = (
<Badge
text={isOwner ? translate('common.owner') : translate('common.admin')}
textStyles={styles.textStrong}
badgeStyles={[styles.justifyContentCenter, StyleUtils.getMinimumWidth(60), styles.badgeBordered, isSelected && styles.activeItemBadge]}
/>
);
}
result.push({
keyForList: accountIDKey,
accountID,
isSelected,
isDisabled:
isPolicyAdmin &&
(accountID === session?.accountID ||
accountID === policy?.ownerAccountID ||
policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE ||
!isEmptyObject(policyMember.errors)),
text: formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details)),
alternateText: formatPhoneNumber(details?.login ?? ''),
rightElement: roleBadge,
icons: [
{
source: UserUtils.getAvatar(details.avatar, accountID),
name: formatPhoneNumber(details?.login ?? ''),
type: CONST.ICON_TYPE_AVATAR,
id: accountID,
},
],
errors: policyMember.errors,
pendingAction: policyMember.pendingAction,
// Note which secondary login was used to invite this primary login
invitedSecondaryLogin: details?.login ? invitedPrimaryToSecondaryLogins[details.login] ?? '' : '',
});
});
result = result.sort((a, b) => a.text.toLowerCase().localeCompare(b.text.toLowerCase()));
return result;
};
const data = getUsers();
const getHeaderMessage = () => {
if (isOfflineAndNoMemberDataAvailable) {
return translate('workspace.common.mustBeOnlineToViewMembers');
}
return !data.length ? translate('workspace.common.memberNotFound') : '';
};
const getHeaderContent = () => (
<>
<Text style={[styles.pl5, styles.mb4, styles.mt3, styles.textSupporting]}>{translate('workspace.people.membersListTitle')}</Text>
{!isEmptyObject(invitedPrimaryToSecondaryLogins) && (
<MessagesRow
type="success"
// eslint-disable-next-line @typescript-eslint/naming-convention
messages={{0: 'workspace.people.addedWithPrimary'}}
containerStyles={[styles.pb5, styles.ph5]}
onClose={() => Policy.dismissAddedWithPrimaryLoginMessages(policyID)}
/>
)}
</>
);
const getCustomListHeader = () => {
const header = (
<View style={[styles.flex1, styles.flexRow, styles.justifyContentBetween]}>
<View>
<Text style={[styles.searchInputStyle, isPolicyAdmin ? styles.ml3 : styles.ml0]}>{translate('common.member')}</Text>
</View>
<View style={[StyleUtils.getMinimumWidth(60)]}>
<Text style={[styles.searchInputStyle, styles.textAlignCenter]}>{translate('common.role')}</Text>
</View>
</View>
);
if (isPolicyAdmin) {
return header;
}
return <View style={[styles.peopleRow, styles.userSelectNone, styles.ph9, styles.pv3, styles.pb5]}>{header}</View>;
};
const changeUserRole = (role: typeof CONST.POLICY.ROLE.ADMIN | typeof CONST.POLICY.ROLE.USER) => {
if (!isEmptyObject(errors)) {
return;
}
const accountIDsToUpdate = selectedEmployees.filter((id) => policyMembers?.[id].role !== role);
Policy.updateWorkspaceMembersRole(route.params.policyID, accountIDsToUpdate, role);
setSelectedEmployees([]);
};
const getBulkActionsButtonOptions = () => {
const options: Array<DropdownOption<WorkspaceMemberBulkActionType>> = [
{
text: translate('workspace.people.removeMembersTitle'),
value: CONST.POLICY.MEMBERS_BULK_ACTION_TYPES.REMOVE,
icon: Expensicons.RemoveMembers,
onSelected: askForConfirmationToRemove,
},
];
if (PolicyUtils.isPaidGroupPolicy(policy)) {
if (selectedEmployees.find((employee) => policyMembers?.[employee]?.role === CONST.POLICY.ROLE.ADMIN)) {
options.push({
text: translate('workspace.people.makeMember'),
value: CONST.POLICY.MEMBERS_BULK_ACTION_TYPES.MAKE_MEMBER,
icon: Expensicons.User,
onSelected: () => changeUserRole(CONST.POLICY.ROLE.USER),
});
}
if (selectedEmployees.find((employee) => policyMembers?.[employee]?.role === CONST.POLICY.ROLE.USER)) {
options.push({
text: translate('workspace.people.makeAdmin'),
value: CONST.POLICY.MEMBERS_BULK_ACTION_TYPES.MAKE_ADMIN,
icon: Expensicons.MakeAdmin,
onSelected: () => changeUserRole(CONST.POLICY.ROLE.ADMIN),
});
}
}
return options;
};
const getHeaderButtons = () => {
if (!isPolicyAdmin) {
return null;
}
return (
<View style={styles.w100}>
{selectedEmployees.length > 0 ? (
<ButtonWithDropdownMenu<WorkspaceMemberBulkActionType>
shouldAlwaysShowDropdownMenu
pressOnEnter
customText={translate('workspace.people.selected', {selectedNumber: selectedEmployees.length})}
buttonSize={CONST.DROPDOWN_BUTTON_SIZE.MEDIUM}
onPress={() => null}
options={getBulkActionsButtonOptions()}
buttonRef={dropdownButtonRef}
style={[isSmallScreenWidth && styles.flexGrow1]}
/>
) : (
<Button
medium
success
onPress={inviteUser}
text={translate('workspace.invite.member')}
icon={Expensicons.Plus}
iconStyles={StyleUtils.getTransformScaleStyle(0.6)}
innerStyles={[isSmallScreenWidth && styles.alignItemsCenter]}
style={[isSmallScreenWidth && styles.flexGrow1]}
/>
)}
</View>
);
};
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
testID={WorkspaceMembersPage.displayName}
shouldShowOfflineIndicatorInWideScreen
>
<FullPageNotFoundView
shouldShow={(isEmptyObject(policy) && !isLoadingReportData) || PolicyUtils.isPendingDeletePolicy(policy)}
subtitleKey={isEmptyObject(policy) ? undefined : 'workspace.common.notAuthorized'}
onBackButtonPress={PolicyUtils.goBackFromInvalidPolicy}
onLinkPress={PolicyUtils.goBackFromInvalidPolicy}
>
<HeaderWithBackButton
title={translate('workspace.common.members')}
icon={Illustrations.ReceiptWrangler}
onBackButtonPress={() => {
Navigation.goBack();
}}
shouldShowBackButton={isSmallScreenWidth}
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_MEMBERS}
>
{!isSmallScreenWidth && getHeaderButtons()}
</HeaderWithBackButton>
{isSmallScreenWidth && <View style={[styles.pl5, styles.pr5]}>{getHeaderButtons()}</View>}
<ConfirmModal
danger
title={translate('workspace.people.removeMembersTitle')}
isVisible={removeMembersConfirmModalVisible}
onConfirm={removeUsers}
onCancel={() => setRemoveMembersConfirmModalVisible(false)}
prompt={translate('workspace.people.removeMembersPrompt')}
confirmText={translate('common.remove')}
cancelText={translate('common.cancel')}
onModalHide={() => {
InteractionManager.runAfterInteractions(() => {
if (!textInputRef.current) {
return;
}
textInputRef.current.focus();
});
}}
/>
<View style={[styles.w100, styles.flex1]}>
<SelectionList
canSelectMultiple={isPolicyAdmin}
sections={[{data, indexOffset: 0, isDisabled: false}]}
ListItem={TableListItem}
disableKeyboardShortcuts={removeMembersConfirmModalVisible}
headerMessage={getHeaderMessage()}
headerContent={getHeaderContent()}
onSelectRow={openMemberDetails}
onCheckboxPress={(item) => toggleUser(item.accountID)}
onSelectAll={() => toggleAllUsers(data)}
onDismissError={dismissError}
showLoadingPlaceholder={!isOfflineAndNoMemberDataAvailable && (!OptionsListUtils.isPersonalDetailsReady(personalDetails) || isEmptyObject(policyMembers))}
showScrollIndicator
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
ref={textInputRef}
customListHeader={getCustomListHeader()}
listHeaderWrapperStyle={[styles.ph9, styles.pv3, styles.pb5]}
/>
</View>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
WorkspaceMembersPage.displayName = 'WorkspaceMembersPage';
export default withCurrentUserPersonalDetails(
withPolicyAndFullscreenLoading(
withOnyx<WorkspaceMembersPageProps, WorkspaceMembersPageOnyxProps>({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
})(WorkspaceMembersPage),
),
);