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

73 lines
1.9 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
2023-03-02 20:55:40 +00:00
import React, { useCallback, useState } from 'react';
2021-09-29 20:23:06 +00:00
2023-03-02 20:55:40 +00:00
import type { AttachmentDraftType } from '../../types/Attachment';
import type { LocalizerType } from '../../types/Util';
2021-09-29 20:23:06 +00:00
import { ToastVoiceNoteMustBeOnlyAttachment } from '../ToastVoiceNoteMustBeOnlyAttachment';
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;
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,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2023-03-02 20:55:40 +00:00
const [showOnlyAttachmentToast, setShowOnlyAttachmentToast] = useState(false);
2021-09-29 20:23:06 +00:00
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 handleCloseToast = useCallback(() => {
setShowOnlyAttachmentToast(false);
}, []);
2023-03-02 20:55:40 +00:00
const handleClick = useCallback(() => {
if (draftAttachments.length) {
setShowOnlyAttachmentToast(true);
} else {
startRecording(conversationId);
2021-09-29 20:23:06 +00:00
}
}, [
2023-03-02 20:55:40 +00:00
conversationId,
draftAttachments,
setShowOnlyAttachmentToast,
startRecording,
]);
2021-09-29 20:23:06 +00:00
return (
<>
<div className="AudioCapture">
<button
2023-03-30 00:03:25 +00:00
aria-label={i18n('icu:voiceRecording--start')}
2021-09-29 20:23:06 +00:00
className="AudioCapture__microphone"
2023-03-02 20:55:40 +00:00
onClick={handleClick}
2023-03-30 00:03:25 +00:00
title={i18n('icu:voiceRecording--start')}
2021-09-29 20:23:06 +00:00
type="button"
/>
</div>
2023-03-02 20:55:40 +00:00
{showOnlyAttachmentToast && (
<ToastVoiceNoteMustBeOnlyAttachment
i18n={i18n}
onClose={handleCloseToast}
/>
)}
2021-09-29 20:23:06 +00:00
</>
);
2022-11-18 00:45:19 +00:00
}