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';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../../types/Util';
|
2021-09-29 20:23:06 +00:00
|
|
|
import { ToastVoiceNoteMustBeOnlyAttachment } from '../ToastVoiceNoteMustBeOnlyAttachment';
|
|
|
|
import {
|
2021-10-15 18:51:58 +00:00
|
|
|
useStartRecordingShortcut,
|
2021-09-29 20:23:06 +00:00
|
|
|
useKeyboardShortcuts,
|
|
|
|
} from '../../hooks/useKeyboardShortcuts';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
conversationId: string;
|
2021-11-15 21:54:33 +00:00
|
|
|
draftAttachments: ReadonlyArray<AttachmentDraftType>;
|
2021-09-29 20:23:06 +00:00
|
|
|
i18n: LocalizerType;
|
2023-01-05 00:22:36 +00:00
|
|
|
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
|
|
|
|
2023-01-05 00:22:36 +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);
|
2021-10-27 16:16:09 +00:00
|
|
|
}, []);
|
|
|
|
|
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
|
|
|
}
|
2021-10-27 16:16:09 +00:00
|
|
|
}, [
|
2023-03-02 20:55:40 +00:00
|
|
|
conversationId,
|
|
|
|
draftAttachments,
|
|
|
|
setShowOnlyAttachmentToast,
|
|
|
|
startRecording,
|
2021-10-27 16:16:09 +00:00
|
|
|
]);
|
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
|
|
|
}
|