-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathAddCustomSound.tsx
More file actions
123 lines (107 loc) · 3.94 KB
/
AddCustomSound.tsx
File metadata and controls
123 lines (107 loc) · 3.94 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
import { Field, FieldLabel, FieldRow, TextInput, Box, Margins, Button, ButtonGroup, IconButton } from '@rocket.chat/fuselage';
import { ContextualbarScrollableContent, ContextualbarFooter } from '@rocket.chat/ui-client';
import { useToastMessageDispatch, useMethod } from '@rocket.chat/ui-contexts';
import type { ReactElement, FormEvent } from 'react';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { validate, createSoundData } from './lib';
import { useSingleFileInput } from '../../../hooks/useSingleFileInput';
type AddCustomSoundProps = {
goToNew: (_id: string) => () => void;
close: () => void;
onChange: () => void;
};
const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundProps): ReactElement => {
const { t } = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();
const [name, setName] = useState('');
const [sound, setSound] = useState<{ name: string }>();
const uploadCustomSound = useMethod('uploadCustomSound');
const insertOrUpdateSound = useMethod('insertOrUpdateSound');
const handleChangeFile = useCallback((soundFile: File) => {
setSound(soundFile);
}, []);
const [clickUpload] = useSingleFileInput(handleChangeFile, 'sound', { fileType: 'audio/mp3' });
const saveAction = useCallback(
// FIXME
async (name: string, soundFile: any) => {
const soundData = createSoundData(soundFile, name);
const validation = validate(soundData, soundFile) as Array<Parameters<typeof t>[0]>;
validation.forEach((invalidFieldName) => {
throw new Error(t('Required_field', { field: t(invalidFieldName) }));
});
try {
const soundId = await insertOrUpdateSound(soundData);
if (!soundId) {
return undefined;
}
dispatchToastMessage({ type: 'success', message: t('Uploading_file') });
const reader = new FileReader();
reader.readAsBinaryString(soundFile);
reader.onloadend = (): void => {
try {
uploadCustomSound(reader.result as string, soundFile.type, {
...soundData,
_id: soundId,
random: Math.round(Math.random() * 1000),
});
dispatchToastMessage({ type: 'success', message: t('File_uploaded') });
} catch (error) {
(typeof error === 'string' || error instanceof Error) && dispatchToastMessage({ type: 'error', message: error });
}
};
close();
return soundId;
} catch (error) {
(typeof error === 'string' || error instanceof Error) && dispatchToastMessage({ type: 'error', message: error });
}
},
[dispatchToastMessage, insertOrUpdateSound, t, uploadCustomSound],
);
const handleSave = useCallback(async () => {
try {
const result = await saveAction(name, sound);
if (result) {
dispatchToastMessage({ type: 'success', message: t('Custom_Sound_Saved_Successfully') });
}
result && goToNew(result);
onChange();
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
}, [dispatchToastMessage, goToNew, name, onChange, saveAction, sound, t]);
return (
<>
<ContextualbarScrollableContent {...props}>
<Field>
<FieldLabel>{t('Name')}</FieldLabel>
<FieldRow>
<TextInput
value={name}
onChange={(e: FormEvent<HTMLInputElement>): void => setName(e.currentTarget.value)}
placeholder={t('Name')}
/>
</FieldRow>
</Field>
<Field>
<FieldLabel alignSelf='stretch'>{t('Sound_File_mp3')}</FieldLabel>
<Box display='flex' flexDirection='row' mbs='none' alignItems='center'>
<Margins inline={4}>
<IconButton secondary small icon='upload' onClick={clickUpload} />
{sound?.name || t('None')}
</Margins>
</Box>
</Field>
</ContextualbarScrollableContent>
<ContextualbarFooter>
<ButtonGroup stretch>
<Button onClick={close}>{t('Cancel')}</Button>
<Button primary onClick={handleSave}>
{t('Save')}
</Button>
</ButtonGroup>
</ContextualbarFooter>
</>
);
};
export default AddCustomSound;