2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-03-10 20:36:58 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2024-03-13 20:44:13 +00:00
|
|
|
import React, { memo, useCallback } from 'react';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { useSelector } from 'react-redux';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
|
|
|
import { MessageAudio } from '../../components/conversation/MessageAudio';
|
2022-09-15 20:10:46 +00:00
|
|
|
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';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
export type Props = Omit<MessageAudioOwnProps, 'active' | 'onPlayMessage'> & {
|
|
|
|
renderingContext: string;
|
|
|
|
};
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartMessageAudio = memo(function SmartMessageAudio({
|
2023-02-24 23:18:57 +00:00
|
|
|
renderingContext,
|
|
|
|
...props
|
2024-03-13 20:44:13 +00:00
|
|
|
}: 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;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
|
|
|
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
|
2022-09-15 20:10:46 +00:00
|
|
|
: undefined;
|
2021-03-10 20:36:58 +00:00
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
2024-03-13 20:44:13 +00:00
|
|
|
});
|