signal-desktop/ts/state/smart/CompositionRecording.tsx

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-03-02 20:55:40 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { memo, useCallback } from 'react';
2023-03-02 20:55:40 +00:00
import { useSelector } from 'react-redux';
import { CompositionRecording } from '../../components/CompositionRecording';
import { useAudioRecorderActions } from '../ducks/audioRecorder';
import { useComposerActions } from '../ducks/composer';
import { useToastActions } from '../ducks/toast';
2023-03-02 20:55:40 +00:00
import { getSelectedConversationId } from '../selectors/conversations';
import { getIntl } from '../selectors/user';
export type SmartCompositionRecordingProps = {
onBeforeSend: () => void;
};
export const SmartCompositionRecording = memo(
function SmartCompositionRecording({
2023-03-02 20:55:40 +00:00
onBeforeSend,
}: SmartCompositionRecordingProps) {
const i18n = useSelector(getIntl);
const selectedConversationId = useSelector(getSelectedConversationId);
const { errorRecording, cancelRecording, completeRecording } =
useAudioRecorderActions();
2023-03-02 20:55:40 +00:00
const { sendMultiMediaMessage, addAttachment, saveDraftRecordingIfNeeded } =
useComposerActions();
const { hideToast, showToast } = useToastActions();
const handleCancel = useCallback(() => {
cancelRecording();
}, [cancelRecording]);
const handleSend = useCallback(() => {
if (selectedConversationId) {
completeRecording(selectedConversationId, voiceNoteAttachment => {
onBeforeSend();
sendMultiMediaMessage(selectedConversationId, {
voiceNoteAttachment,
});
});
}
}, [
selectedConversationId,
completeRecording,
onBeforeSend,
sendMultiMediaMessage,
]);
2023-03-02 20:55:40 +00:00
if (!selectedConversationId) {
return null;
}
return (
<CompositionRecording
i18n={i18n}
onCancel={handleCancel}
onSend={handleSend}
errorRecording={errorRecording}
addAttachment={addAttachment}
completeRecording={completeRecording}
saveDraftRecordingIfNeeded={saveDraftRecordingIfNeeded}
showToast={showToast}
hideToast={hideToast}
/>
);
}
);