-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceNewRoomPage.tsx
362 lines (334 loc) · 16.3 KB
/
WorkspaceNewRoomPage.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
import {useIsFocused} from '@react-navigation/core';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import BlockingView from '@components/BlockingViews/BlockingView';
import Button from '@components/Button';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormOnyxValues} from '@components/Form/types';
import * as Illustrations from '@components/Icon/Illustrations';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
import OfflineIndicator from '@components/OfflineIndicator';
import RoomNameInput from '@components/RoomNameInput';
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
import ValuePicker from '@components/ValuePicker';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as ErrorUtils from '@libs/ErrorUtils';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import variables from '@styles/variables';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {NewRoomForm} from '@src/types/form/NewRoomForm';
import INPUT_IDS from '@src/types/form/NewRoomForm';
import type {Policy, Report as ReportType, Session} from '@src/types/onyx';
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type WorkspaceNewRoomPageOnyxProps = {
/** The list of policies the user has access to. */
policies: OnyxCollection<Policy>;
/** All reports shared with the user */
reports: OnyxCollection<ReportType>;
/** Form state for NEW_ROOM_FORM */
formState: OnyxEntry<NewRoomForm>;
/** Session details for the user */
session: OnyxEntry<Session>;
/** policyID for main workspace */
activePolicyID: OnyxEntry<Required<string>>;
};
type WorkspaceNewRoomPageProps = WorkspaceNewRoomPageOnyxProps;
function WorkspaceNewRoomPage({policies, reports, formState, session, activePolicyID}: WorkspaceNewRoomPageProps) {
const styles = useThemeStyles();
const isFocused = useIsFocused();
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {isSmallScreenWidth} = useWindowDimensions();
const [visibility, setVisibility] = useState<ValueOf<typeof CONST.REPORT.VISIBILITY>>(CONST.REPORT.VISIBILITY.RESTRICTED);
const [writeCapability, setWriteCapability] = useState<ValueOf<typeof CONST.REPORT.WRITE_CAPABILITIES>>(CONST.REPORT.WRITE_CAPABILITIES.ALL);
const wasLoading = usePrevious<boolean>(!!formState?.isLoading);
const visibilityDescription = useMemo(() => translate(`newRoomPage.${visibility}Description`), [translate, visibility]);
const {isLoading = false, errorFields = {}} = formState ?? {};
const {activeWorkspaceID} = useActiveWorkspace();
const activeWorkspaceOrDefaultID = activeWorkspaceID ?? activePolicyID;
const workspaceOptions = useMemo(
() =>
PolicyUtils.getActivePolicies(policies)
?.filter((policy) => policy.type !== CONST.POLICY.TYPE.PERSONAL)
.map((policy) => ({
label: policy.name,
value: policy.id,
}))
.sort((a, b) => localeCompare(a.label, b.label)) ?? [],
[policies],
);
const [policyID, setPolicyID] = useState<string>(() => {
if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((option) => option.value === activeWorkspaceOrDefaultID)) {
return activeWorkspaceOrDefaultID;
}
return '';
});
const isPolicyAdmin = useMemo(() => {
if (!policyID) {
return false;
}
return ReportUtils.isPolicyAdmin(policyID, policies);
}, [policyID, policies]);
const [newRoomReportID, setNewRoomReportID] = useState<string>();
/**
* @param values - form input values passed by the Form component
*/
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_ROOM_FORM>) => {
const participants = [session?.accountID ?? 0];
const parsedDescription = ReportUtils.getParsedComment(values.reportDescription ?? '');
const policyReport = ReportUtils.buildOptimisticChatReport(
participants,
values.roomName,
CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
policyID,
CONST.REPORT.OWNER_ACCOUNT_ID_FAKE,
false,
'',
visibility,
writeCapability || CONST.REPORT.WRITE_CAPABILITIES.ALL,
CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
'',
'',
parsedDescription,
);
setNewRoomReportID(policyReport.reportID);
Report.addPolicyReport(policyReport);
};
useEffect(() => {
Report.clearNewRoomFormError();
}, []);
useEffect(() => {
if (policyID) {
if (!workspaceOptions.some((opt) => opt.value === policyID)) {
setPolicyID('');
}
return;
}
if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((opt) => opt.value === activeWorkspaceOrDefaultID)) {
setPolicyID(activeWorkspaceOrDefaultID);
} else {
setPolicyID('');
}
}, [activeWorkspaceOrDefaultID, policyID, workspaceOptions]);
useEffect(() => {
if (!(((wasLoading && !isLoading) || (isOffline && isLoading)) && isEmptyObject(errorFields))) {
return;
}
Navigation.dismissModal(newRoomReportID);
// eslint-disable-next-line react-hooks/exhaustive-deps -- we just want this to update on changing the form State
}, [isLoading, errorFields]);
useEffect(() => {
if (isPolicyAdmin) {
return;
}
setWriteCapability(CONST.REPORT.WRITE_CAPABILITIES.ALL);
}, [isPolicyAdmin]);
/**
* @param values - form input values passed by the Form component
* @returns an object containing validation errors, if any were found during validation
*/
const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_ROOM_FORM>): OnyxCommon.Errors => {
const errors: {policyID?: string; roomName?: string} = {};
if (!values.roomName || values.roomName === CONST.POLICY.ROOM_PREFIX) {
// We error if the user doesn't enter a room name or left blank
ErrorUtils.addErrorMessage(errors, 'roomName', 'newRoomPage.pleaseEnterRoomName');
} else if (values.roomName !== CONST.POLICY.ROOM_PREFIX && !ValidationUtils.isValidRoomName(values.roomName)) {
// We error if the room name has invalid characters
ErrorUtils.addErrorMessage(errors, 'roomName', 'newRoomPage.roomNameInvalidError');
} else if (ValidationUtils.isReservedRoomName(values.roomName)) {
// Certain names are reserved for default rooms and should not be used for policy rooms.
ErrorUtils.addErrorMessage(errors, 'roomName', ['newRoomPage.roomNameReservedError', {reservedName: values.roomName}]);
} else if (ValidationUtils.isExistingRoomName(values.roomName, reports, values.policyID ?? '')) {
// Certain names are reserved for default rooms and should not be used for policy rooms.
ErrorUtils.addErrorMessage(errors, 'roomName', 'newRoomPage.roomAlreadyExistsError');
} else if (values.roomName.length > CONST.TITLE_CHARACTER_LIMIT) {
ErrorUtils.addErrorMessage(errors, 'roomName', ['common.error.characterLimitExceedCounter', {length: values.roomName.length, limit: CONST.TITLE_CHARACTER_LIMIT}]);
}
const descriptionLength = ReportUtils.getCommentLength(values.reportDescription);
if (descriptionLength > CONST.REPORT_DESCRIPTION.MAX_LENGTH) {
ErrorUtils.addErrorMessage(errors, 'reportDescription', [
'common.error.characterLimitExceedCounter',
{length: descriptionLength, limit: CONST.REPORT_DESCRIPTION.MAX_LENGTH},
]);
}
if (!values.policyID) {
errors.policyID = 'newRoomPage.pleaseSelectWorkspace';
}
return errors;
},
[reports],
);
const writeCapabilityOptions = useMemo(
() =>
Object.values(CONST.REPORT.WRITE_CAPABILITIES).map((value) => ({
value,
label: translate(`writeCapabilityPage.writeCapability.${value}`),
})),
[translate],
);
const visibilityOptions = useMemo(
() =>
Object.values(CONST.REPORT.VISIBILITY)
.filter((visibilityOption) => visibilityOption !== CONST.REPORT.VISIBILITY.PUBLIC_ANNOUNCE)
.map((visibilityOption) => ({
label: translate(`newRoomPage.visibilityOptions.${visibilityOption}`),
value: visibilityOption,
description: translate(`newRoomPage.${visibilityOption}Description`),
})),
[translate],
);
const {inputCallbackRef} = useAutoFocusInput();
const renderEmptyWorkspaceView = () => (
<>
<BlockingView
icon={Illustrations.TeleScope}
iconWidth={variables.emptyWorkspaceIconWidth}
iconHeight={variables.emptyWorkspaceIconHeight}
title={translate('workspace.emptyWorkspace.notFound')}
subtitle={translate('workspace.emptyWorkspace.description')}
shouldShowLink={false}
/>
<Button
success
large
text={translate('footer.learnMore')}
onPress={() => Navigation.navigate(ROUTES.SETTINGS_WORKSPACES)}
style={[styles.mh5, styles.mb5]}
/>
{isSmallScreenWidth && <OfflineIndicator />}
</>
);
return (
<ScreenWrapper
shouldEnableKeyboardAvoidingView={false}
includeSafeAreaPaddingBottom={isOffline}
shouldShowOfflineIndicator={false}
includePaddingTop={false}
shouldEnablePickerAvoiding={false}
testID={WorkspaceNewRoomPage.displayName}
>
{({insets}) =>
workspaceOptions.length === 0 ? (
renderEmptyWorkspaceView()
) : (
<KeyboardAvoidingView
style={styles.h100}
behavior="padding"
// Offset is needed as KeyboardAvoidingView in nested inside of TabNavigator instead of wrapping whole screen.
// This is because when wrapping whole screen the screen was freezing when changing Tabs.
keyboardVerticalOffset={variables.contentHeaderHeight + variables.tabSelectorButtonHeight + variables.tabSelectorButtonPadding + insets.top}
>
<FormProvider
formID={ONYXKEYS.FORMS.NEW_ROOM_FORM}
submitButtonText={translate('newRoomPage.createRoom')}
style={[styles.mh5, styles.flexGrow1]}
validate={validate}
onSubmit={submit}
enabledWhenOffline
disablePressOnEnter={false}
>
<View style={styles.mb5}>
<InputWrapper
InputComponent={RoomNameInput}
ref={inputCallbackRef}
inputID={INPUT_IDS.ROOM_NAME}
isFocused={isFocused}
shouldDelayFocus
autoFocus
/>
</View>
<View style={styles.mb5}>
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.REPORT_DESCRIPTION}
label={translate('reportDescriptionPage.roomDescriptionOptional')}
accessibilityLabel={translate('reportDescriptionPage.roomDescriptionOptional')}
role={CONST.ROLE.PRESENTATION}
autoGrowHeight
maxAutoGrowHeight={variables.textInputAutoGrowMaxHeight}
maxLength={CONST.REPORT_DESCRIPTION.MAX_LENGTH}
autoCapitalize="none"
shouldInterceptSwipe
/>
</View>
<View style={[styles.mhn5]}>
<InputWrapper
InputComponent={ValuePicker}
inputID={INPUT_IDS.POLICY_ID}
label={translate('workspace.common.workspace')}
items={workspaceOptions}
value={policyID}
onValueChange={(value) => setPolicyID(value as typeof policyID)}
/>
</View>
{isPolicyAdmin && (
<View style={styles.mhn5}>
<InputWrapper
InputComponent={ValuePicker}
inputID={INPUT_IDS.WRITE_CAPABILITY}
label={translate('writeCapabilityPage.label')}
items={writeCapabilityOptions}
value={writeCapability}
onValueChange={(value) => setWriteCapability(value as typeof writeCapability)}
/>
</View>
)}
<View style={[styles.mb1, styles.mhn5]}>
<InputWrapper
InputComponent={ValuePicker}
inputID={INPUT_IDS.VISIBILITY}
label={translate('newRoomPage.visibility')}
items={visibilityOptions}
onValueChange={(value) => setVisibility(value as typeof visibility)}
value={visibility}
furtherDetails={visibilityDescription}
shouldShowTooltips={false}
/>
</View>
</FormProvider>
{isSmallScreenWidth && <OfflineIndicator />}
</KeyboardAvoidingView>
)
}
</ScreenWrapper>
);
}
WorkspaceNewRoomPage.displayName = 'WorkspaceNewRoomPage';
export default withOnyx<WorkspaceNewRoomPageProps, WorkspaceNewRoomPageOnyxProps>({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
formState: {
key: ONYXKEYS.FORMS.NEW_ROOM_FORM,
},
session: {
key: ONYXKEYS.SESSION,
},
activePolicyID: {
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
initialValue: null,
},
})(WorkspaceNewRoomPage);