/
CreateDiscussion.tsx
152 lines (137 loc) · 4.79 KB
/
CreateDiscussion.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
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { Modal, Field, FieldGroup, ToggleSwitch, TextInput, TextAreaInput, Button, Icon, Box } from '@rocket.chat/fuselage';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useTranslation } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React from 'react';
import { useEndpointAction } from '../../hooks/useEndpointAction';
import { useForm } from '../../hooks/useForm';
import { goToRoomById } from '../../lib/utils/goToRoomById';
import RoomAutoComplete from '../RoomAutoComplete';
import UserAutoCompleteMultiple from '../UserAutoCompleteMultiple';
import DefaultParentRoomField from './DefaultParentRoomField';
type CreateDiscussionFormValues = {
name: string;
parentRoom: IRoom['_id'];
encrypted: boolean;
usernames: Array<IUser['username']>;
firstMessage: string;
};
type CreateDiscussionProps = {
parentMessageId: IMessage['_id'];
onClose: () => void;
defaultParentRoom?: IRoom['_id'];
nameSuggestion?: string;
};
const CreateDiscussion = ({ onClose, defaultParentRoom, parentMessageId, nameSuggestion }: CreateDiscussionProps): ReactElement => {
const t = useTranslation();
const { values, handlers } = useForm({
name: nameSuggestion || '',
parentRoom: '',
encrypted: false,
usernames: [],
firstMessage: '',
});
const { name, parentRoom, encrypted, usernames, firstMessage } = values as CreateDiscussionFormValues;
const { handleName, handleParentRoom, handleEncrypted, handleUsernames, handleFirstMessage } = handlers;
const canCreate = (parentRoom || defaultParentRoom) && name;
const createDiscussion = useEndpointAction('POST', '/v1/rooms.createDiscussion');
const create = useMutableCallback(async (): Promise<void> => {
try {
const result = await createDiscussion({
prid: defaultParentRoom || parentRoom,
t_name: name,
users: usernames,
reply: encrypted ? undefined : firstMessage,
...(parentMessageId && { pmid: parentMessageId }),
});
goToRoomById(result.discussion._id);
onClose();
} catch (error) {
console.warn(error);
}
});
const onChangeUsers = useMutableCallback((value, action) => {
if (!action) {
if (usernames.includes(value)) {
return;
}
return handleUsernames([...usernames, value]);
}
handleUsernames(usernames.filter((current) => current !== value));
});
return (
<Modal>
<Modal.Header>
<Modal.Title>{t('Discussion_title')}</Modal.Title>
<Modal.Close onClick={onClose} />
</Modal.Header>
<Modal.Content>
<FieldGroup>
<Field>
<Field.Description>{t('Discussion_description')}</Field.Description>
</Field>
<Field>
<Field.Label>{t('Discussion_target_channel')}</Field.Label>
<Field.Row>
{defaultParentRoom && <DefaultParentRoomField defaultParentRoom={defaultParentRoom} />}
{!defaultParentRoom && (
<RoomAutoComplete
value={parentRoom}
onChange={handleParentRoom}
placeholder={t('Discussion_target_channel_description')}
disabled={Boolean(defaultParentRoom)}
/>
)}
</Field.Row>
</Field>
<Field display='flex' alignItems='center' flexDirection='row' justifyContent='spaceBetween' flexGrow={1}>
<Box display='flex' flexDirection='column' width='full'>
<Field.Label>{t('Encrypted')}</Field.Label>
</Box>
<ToggleSwitch checked={encrypted} onChange={handleEncrypted} />
</Field>
<Field>
<Field.Label>{t('Discussion_name')}</Field.Label>
<Field.Row>
<TextInput
value={name}
onChange={handleName}
addon={<Icon name='baloons' size='x20' />}
placeholder={t('New_discussion_name')}
/>
</Field.Row>
</Field>
<Field>
<Field.Label>{t('Invite_Users')}</Field.Label>
<Field.Row w='full' display='flex' flexDirection='column' alignItems='stretch'>
<UserAutoCompleteMultiple value={usernames} onChange={onChangeUsers} placeholder={t('Username_Placeholder')} />
</Field.Row>
</Field>
<Field>
<Field.Label>{t('Discussion_first_message_title')}</Field.Label>
<Field.Row>
<TextAreaInput
value={firstMessage}
onChange={handleFirstMessage}
placeholder={t('New_discussion_first_message')}
rows={5}
disabled={encrypted}
/>
</Field.Row>
{encrypted && <Field.Description>{t('Discussion_first_message_disabled_due_to_e2e')}</Field.Description>}
</Field>
</FieldGroup>
</Modal.Content>
<Modal.Footer>
<Modal.FooterControllers>
<Button onClick={onClose}>{t('Cancel')}</Button>
<Button primary disabled={!canCreate} onClick={create}>
{t('Create')}
</Button>
</Modal.FooterControllers>
</Modal.Footer>
</Modal>
);
};
export default CreateDiscussion;