signal-desktop/ts/components/conversation/AudioCapture.tsx

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2016 Signal Messenger, LLC
2021-09-29 20:23:06 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useCallback } from 'react';
2021-09-29 20:23:06 +00:00
import type { ShowToastAction } from '../../state/ducks/toast';
2023-03-02 20:55:40 +00:00
import type { AttachmentDraftType } from '../../types/Attachment';
import type { LocalizerType } from '../../types/Util';
import { ToastType } from '../../types/Toast';
2021-09-29 20:23:06 +00:00
import {
useStartRecordingShortcut,
2021-09-29 20:23:06 +00:00
useKeyboardShortcuts,
} from '../../hooks/useKeyboardShortcuts';
export type PropsType = {
conversationId: string;
draftAttachments: ReadonlyArray<AttachmentDraftType>;
2021-09-29 20:23:06 +00:00
i18n: LocalizerType;
startRecording: (id: string) => unknown;
showToast: ShowToastAction;
2021-09-29 20:23:06 +00:00
};
2022-11-18 00:45:19 +00:00
export function AudioCapture({
2021-09-29 20:23:06 +00:00
conversationId,
draftAttachments,
i18n,
startRecording,
showToast,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
const recordConversation = useCallback(
() => startRecording(conversationId),
[conversationId, startRecording]
);
const startRecordingShortcut = useStartRecordingShortcut(recordConversation);
2021-09-29 20:23:06 +00:00
useKeyboardShortcuts(startRecordingShortcut);
2023-03-02 20:55:40 +00:00
const handleClick = useCallback(() => {
if (draftAttachments.length) {
showToast({ toastType: ToastType.VoiceNoteMustBeTheOnlyAttachment });
2023-03-02 20:55:40 +00:00
} else {
startRecording(conversationId);
2021-09-29 20:23:06 +00:00
}
}, [conversationId, draftAttachments, showToast, startRecording]);
2021-09-29 20:23:06 +00:00
return (
<div className="AudioCapture">
<button
aria-label={i18n('icu:voiceRecording--start')}
className="AudioCapture__microphone"
onClick={handleClick}
title={i18n('icu:voiceRecording--start')}
type="button"
/>
</div>
2021-09-29 20:23:06 +00:00
);
2022-11-18 00:45:19 +00:00
}