Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions apps/meteor/client/views/admin/customSounds/AddCustomSound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr
const dispatchToastMessage = useToastMessageDispatch();

const [name, setName] = useState('');
const [sound, setSound] = useState<{ name: string }>();
const [sound, setSound] = useState<File>();

const uploadCustomSound = useMethod('uploadCustomSound');
const insertOrUpdateSound = useMethod('insertOrUpdateSound');
Expand All @@ -31,8 +31,7 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr
const [clickUpload] = useSingleFileInput(handleChangeFile, 'audio/mp3');

const saveAction = useCallback(
// FIXME
async (name: string, soundFile: any) => {
async (name: string, soundFile: File) => {
const soundData = createSoundData(soundFile, name);
const validation = validate(soundData, soundFile) as Array<Parameters<typeof t>[0]>;

Expand Down Expand Up @@ -72,6 +71,11 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr
);

const handleSave = useCallback(async () => {
if (!sound) {
// unreachable: Save button is disabled when !sound — guard exists for type narrowing only
return;
}

try {
const result = await saveAction(name, sound);
if (result) {
Expand Down Expand Up @@ -110,7 +114,7 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr
<ContextualbarFooter>
<ButtonGroup stretch>
<Button onClick={close}>{t('Cancel')}</Button>
<Button primary onClick={handleSave}>
<Button primary onClick={handleSave} disabled={!sound || !name}>
{t('Save')}
</Button>
</ButtonGroup>
Expand Down
23 changes: 18 additions & 5 deletions apps/meteor/client/views/admin/customSounds/EditSound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ type EditSoundProps = {
};
};

type ExistingSound = {
_id: string;
name: string;
extension?: string;
};

type SoundFile = File | ExistingSound;

function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactElement {
const { t } = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();
Expand Down Expand Up @@ -52,10 +60,15 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl
const hasUnsavedChanges = useMemo(() => previousName !== name || previousSound !== sound, [name, previousName, previousSound, sound]);

const saveAction = useCallback(
// FIXME
async (sound: any) => {
const soundData = createSoundData(sound, name, { previousName, previousSound, _id, extension: sound.extension });
const validation = validate(soundData, sound);
async (sound: SoundFile) => {
const soundData = createSoundData(sound, name, {
previousName,
previousSound,
_id,
extension: sound instanceof File ? '' : (sound.extension ?? ''),
});
const soundFile = sound instanceof File ? sound : undefined;
const validation = validate(soundData, soundFile);
if (validation.length === 0) {
let soundId: string;
try {
Expand All @@ -68,7 +81,7 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl
soundData._id = soundId;
soundData.random = Math.round(Math.random() * 1000);

if (sound && sound !== previousSound) {
if (sound instanceof File) {
dispatchToastMessage({ type: 'success', message: t('Uploading_file') });

const reader = new FileReader();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/customSounds/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function validate(soundData: ICustomSoundData, soundFile?: ICustomSoundFi
}

export const createSoundData = (
soundFile: ICustomSoundFile,
soundFile: { name: string },
name: string,
previousData?: {
_id: string;
Expand Down
8 changes: 8 additions & 0 deletions packages/gazzodown/src/Markup.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ jest.mock('highlight.js', () => ({
highlightElement: (): void => undefined,
}));

jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));

jest.mock('@rocket.chat/ui-contexts', () => ({
useToastMessageDispatch: () => (): void => undefined,
}));

it('renders empty', () => {
const { container } = render(<Markup tokens={[]} />);
expect(container).toBeEmptyDOMElement();
Expand Down
Loading