-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.tsx
206 lines (183 loc) · 8.88 KB
/
index.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
import React, {useCallback, useMemo} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import type {ListItem, SectionListDataType} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import Text from '@components/Text';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import {sortWorkspacesBySelected} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {getWorkspacesBrickRoads, getWorkspacesUnreadStatuses} from '@libs/WorkspacesSettingsUtils';
import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
import WorkspaceCardCreateAWorkspace from '@pages/workspace/card/WorkspaceCardCreateAWorkspace';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import WorkspacesSectionHeader from './WorkspacesSectionHeader';
type WorkspaceListItem = {
text: string;
policyID?: string;
isPolicyAdmin?: boolean;
brickRoadIndicator?: BrickRoad;
} & ListItem;
const WorkspaceCardCreateAWorkspaceInstance = <WorkspaceCardCreateAWorkspace />;
function WorkspaceSwitcherPage() {
const styles = useThemeStyles();
const theme = useTheme();
const {isOffline} = useNetwork();
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const {translate} = useLocalize();
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);
const [policies, fetchStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(reports, policies, reportActions), [reports, policies, reportActions]);
const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(reports), [reports]);
const getIndicatorTypeForPolicy = useCallback(
(policyId?: string) => {
if (policyId && policyId !== activeWorkspaceID) {
return brickRoadsForPolicies[policyId];
}
if (Object.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
if (Object.values(brickRoadsForPolicies).includes(CONST.BRICK_ROAD_INDICATOR_STATUS.INFO)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
return undefined;
},
[activeWorkspaceID, brickRoadsForPolicies],
);
const hasUnreadData = useCallback(
// TO DO: Implement checking if policy has some unread data
(policyId?: string) => {
if (policyId) {
return unreadStatusesForPolicies[policyId];
}
return Object.values(unreadStatusesForPolicies).some((status) => status);
},
[unreadStatusesForPolicies],
);
const selectPolicy = useCallback(
(option?: WorkspaceListItem) => {
if (!option) {
return;
}
const {policyID} = option;
setActiveWorkspaceID(policyID);
Navigation.goBack();
if (policyID !== activeWorkspaceID) {
Navigation.navigateWithSwitchPolicyID({policyID});
}
},
[activeWorkspaceID, setActiveWorkspaceID],
);
const usersWorkspaces = useMemo<WorkspaceListItem[]>(() => {
if (!policies || isEmptyObject(policies)) {
return [];
}
return Object.values(policies)
.filter((policy) => PolicyUtils.shouldShowPolicy(policy, !!isOffline) && !policy?.isJoinRequestPending)
.map((policy) => ({
text: policy?.name ?? '',
policyID: policy?.id ?? '-1',
brickRoadIndicator: getIndicatorTypeForPolicy(policy?.id),
icons: [
{
source: policy?.avatarURL ? policy.avatarURL : ReportUtils.getDefaultWorkspaceAvatar(policy?.name),
fallbackIcon: Expensicons.FallbackWorkspaceAvatar,
name: policy?.name,
type: CONST.ICON_TYPE_WORKSPACE,
id: policy?.id,
},
],
isBold: hasUnreadData(policy?.id),
keyForList: policy?.id,
isPolicyAdmin: PolicyUtils.isPolicyAdmin(policy),
isSelected: activeWorkspaceID === policy?.id,
}));
}, [policies, isOffline, getIndicatorTypeForPolicy, hasUnreadData, activeWorkspaceID]);
const filteredAndSortedUserWorkspaces = useMemo<WorkspaceListItem[]>(
() =>
usersWorkspaces
.filter((policy) => policy.text?.toLowerCase().includes(debouncedSearchTerm?.toLowerCase() ?? ''))
.sort((policy1, policy2) => sortWorkspacesBySelected({policyID: policy1.policyID, name: policy1.text}, {policyID: policy2.policyID, name: policy2.text}, activeWorkspaceID)),
[debouncedSearchTerm, usersWorkspaces, activeWorkspaceID],
);
const sections = useMemo(() => {
const options: Array<SectionListDataType<WorkspaceListItem>> = [
{
data: filteredAndSortedUserWorkspaces,
shouldShow: true,
indexOffset: 1,
},
];
return options;
}, [filteredAndSortedUserWorkspaces]);
const headerMessage = filteredAndSortedUserWorkspaces.length === 0 && usersWorkspaces.length ? translate('common.noResultsFound') : '';
const shouldShowCreateWorkspace = usersWorkspaces.length === 0;
const defaultPolicy = {
text: CONST.WORKSPACE_SWITCHER.NAME,
icons: [{source: Expensicons.ExpensifyAppIcon, name: CONST.WORKSPACE_SWITCHER.NAME, type: CONST.ICON_TYPE_AVATAR}],
brickRoadIndicator: getIndicatorTypeForPolicy(undefined),
keyForList: CONST.WORKSPACE_SWITCHER.NAME,
isSelected: activeWorkspaceID === undefined,
};
return (
<ScreenWrapper
testID={WorkspaceSwitcherPage.displayName}
includeSafeAreaPaddingBottom={false}
>
{({didScreenTransitionEnd}) => (
<>
<HeaderWithBackButton
title={translate('workspace.switcher.headerTitle')}
onBackButtonPress={Navigation.goBack}
/>
<View style={[styles.ph5, styles.flexRow, styles.justifyContentBetween, styles.alignItemsCenter, styles.mb1]}>
<Text
style={styles.label}
color={theme.textSupporting}
>
{translate('workspace.switcher.everythingSection')}
</Text>
</View>
<UserListItem
item={defaultPolicy}
isFocused={activeWorkspaceID === undefined}
showTooltip={false}
onSelectRow={() => selectPolicy(defaultPolicy)}
pressableStyle={styles.flexRow}
shouldSyncFocus={false}
/>
<WorkspacesSectionHeader />
<SelectionList<WorkspaceListItem>
ListItem={UserListItem}
sections={sections}
onSelectRow={selectPolicy}
textInputLabel={usersWorkspaces.length >= CONST.WORKSPACE_SWITCHER.MINIMUM_WORKSPACES_TO_SHOW_SEARCH ? translate('common.search') : undefined}
textInputValue={searchTerm}
onChangeText={setSearchTerm}
headerMessage={headerMessage}
listFooterContent={shouldShowCreateWorkspace ? WorkspaceCardCreateAWorkspaceInstance : null}
initiallyFocusedOptionKey={activeWorkspaceID ?? CONST.WORKSPACE_SWITCHER.NAME}
showLoadingPlaceholder={fetchStatus.status === 'loading' || !didScreenTransitionEnd}
/>
</>
)}
</ScreenWrapper>
);
}
WorkspaceSwitcherPage.displayName = 'WorkspaceSwitcherPage';
export default WorkspaceSwitcherPage;