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

104 lines
3 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { memo, useCallback } from 'react';
2023-02-24 23:18:57 +00:00
import { useSelector } from 'react-redux';
import { MessageAudio } from '../../components/conversation/MessageAudio';
import type { OwnProps as MessageAudioOwnProps } from '../../components/conversation/MessageAudio';
import type { ActiveAudioPlayerStateType } from '../ducks/audioPlayer';
2023-03-02 20:55:40 +00:00
import {
AudioPlayerContent,
useAudioPlayerActions,
} from '../ducks/audioPlayer';
2023-02-24 23:18:57 +00:00
import {
selectAudioPlayerActive,
selectVoiceNoteAndConsecutive,
} from '../selectors/audioPlayer';
import { useConversationsActions } from '../ducks/conversations';
import { getUserConversationId } from '../selectors/user';
import * as log from '../../logging/log';
2023-03-02 20:55:40 +00:00
import {
getConversationByIdSelector,
getSelectedConversationId,
} from '../selectors/conversations';
2023-02-24 23:18:57 +00:00
export type Props = Omit<MessageAudioOwnProps, 'active' | 'onPlayMessage'> & {
renderingContext: string;
};
export const SmartMessageAudio = memo(function SmartMessageAudio({
2023-02-24 23:18:57 +00:00
renderingContext,
...props
}: Props) {
2023-02-24 23:18:57 +00:00
const active = useSelector(selectAudioPlayerActive);
2023-03-02 20:55:40 +00:00
const { loadVoiceNoteAudio, setIsPlaying, setPlaybackRate, setPosition } =
2023-02-24 23:18:57 +00:00
useAudioPlayerActions();
const { pushPanelForConversation } = useConversationsActions();
const getVoiceNoteData = useSelector(selectVoiceNoteAndConsecutive);
const ourConversationId = useSelector(getUserConversationId);
2023-03-02 20:55:40 +00:00
const getConversationById = useSelector(getConversationByIdSelector);
const selectedConversationId = useSelector(getSelectedConversationId);
if (!selectedConversationId) {
throw new Error('No selected conversation');
}
const playbackRate =
getConversationById(selectedConversationId)?.voiceNotePlaybackRate ?? 1;
const content = active?.content;
const messageActive: ActiveAudioPlayerStateType | undefined =
2023-03-02 20:55:40 +00:00
content &&
AudioPlayerContent.isVoiceNote(content) &&
content.current.id === props.id &&
content.context === renderingContext
2023-02-24 23:18:57 +00:00
? active
: undefined;
2023-02-24 23:18:57 +00:00
const handlePlayMessage = useCallback(
(id: string, position: number) => {
const voiceNoteData = getVoiceNoteData(id);
if (!voiceNoteData) {
log.warn('SmartMessageAudio: voice note not found', {
message: id,
});
return;
}
if (!ourConversationId) {
log.warn('SmartMessageAudio: no ourConversationId');
return;
}
2023-03-02 20:55:40 +00:00
loadVoiceNoteAudio({
2023-02-24 23:18:57 +00:00
voiceNoteData,
position,
context: renderingContext,
ourConversationId,
2023-03-02 20:55:40 +00:00
playbackRate,
2023-02-24 23:18:57 +00:00
});
},
2023-03-02 20:55:40 +00:00
[
getVoiceNoteData,
loadVoiceNoteAudio,
ourConversationId,
renderingContext,
playbackRate,
]
2023-02-24 23:18:57 +00:00
);
return (
<MessageAudio
active={messageActive}
onPlayMessage={handlePlayMessage}
setPlaybackRate={setPlaybackRate}
setIsPlaying={setIsPlaying}
2023-02-28 13:07:40 +00:00
setPosition={setPosition}
2023-02-24 23:18:57 +00:00
pushPanelForConversation={pushPanelForConversation}
{...props}
/>
);
});