2023-02-24 23:18:57 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// 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';
|
|
|
|
import { MiniPlayer, PlayerState } from '../../components/MiniPlayer';
|
2023-03-20 18:03:21 +00:00
|
|
|
import type { Props as DumbProps } from '../../components/MiniPlayer';
|
2023-03-02 20:55:40 +00:00
|
|
|
import {
|
|
|
|
AudioPlayerContent,
|
|
|
|
useAudioPlayerActions,
|
|
|
|
} from '../ducks/audioPlayer';
|
2023-02-24 23:18:57 +00:00
|
|
|
import {
|
|
|
|
selectAudioPlayerActive,
|
|
|
|
selectVoiceNoteTitle,
|
|
|
|
} from '../selectors/audioPlayer';
|
|
|
|
import { getIntl } from '../selectors/user';
|
|
|
|
|
2023-03-20 18:03:21 +00:00
|
|
|
type Props = Pick<DumbProps, 'shouldFlow'>;
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
/**
|
|
|
|
* Wires the dispatch props and shows/hides the MiniPlayer
|
|
|
|
*
|
|
|
|
* It also triggers side-effecting actions (actual playback) in response to changes in
|
|
|
|
* the state
|
|
|
|
*/
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartMiniPlayer = memo(function SmartMiniPlayer({
|
|
|
|
shouldFlow,
|
|
|
|
}: Props): JSX.Element | null {
|
2023-02-24 23:18:57 +00:00
|
|
|
const i18n = useSelector(getIntl);
|
|
|
|
const active = useSelector(selectAudioPlayerActive);
|
|
|
|
const getVoiceNoteTitle = useSelector(selectVoiceNoteTitle);
|
2023-02-28 13:07:40 +00:00
|
|
|
const { setIsPlaying, setPlaybackRate, unloadMessageAudio } =
|
|
|
|
useAudioPlayerActions();
|
2023-02-24 23:18:57 +00:00
|
|
|
const handlePlay = useCallback(() => setIsPlaying(true), [setIsPlaying]);
|
|
|
|
const handlePause = useCallback(() => setIsPlaying(false), [setIsPlaying]);
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
if (!active) {
|
2023-02-24 23:18:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
const { content } = active;
|
|
|
|
|
|
|
|
const url = AudioPlayerContent.isVoiceNote(content)
|
|
|
|
? content.current.url
|
|
|
|
: content.url;
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
let state = PlayerState.loading;
|
2023-03-02 20:55:40 +00:00
|
|
|
if (url) {
|
2023-02-24 23:18:57 +00:00
|
|
|
state = active.playing ? PlayerState.playing : PlayerState.paused;
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:12:32 +00:00
|
|
|
const title = AudioPlayerContent.isDraft(content)
|
|
|
|
? i18n('icu:you')
|
|
|
|
: getVoiceNoteTitle(content.current);
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
return (
|
|
|
|
<MiniPlayer
|
|
|
|
i18n={i18n}
|
2024-03-19 17:12:32 +00:00
|
|
|
title={title}
|
2023-02-24 23:18:57 +00:00
|
|
|
onPlay={handlePlay}
|
|
|
|
onPause={handlePause}
|
|
|
|
onPlaybackRate={setPlaybackRate}
|
|
|
|
onClose={unloadMessageAudio}
|
2023-03-20 18:03:21 +00:00
|
|
|
shouldFlow={shouldFlow}
|
2023-02-24 23:18:57 +00:00
|
|
|
state={state}
|
|
|
|
currentTime={active.currentTime}
|
|
|
|
duration={active.duration}
|
|
|
|
playbackRate={active.playbackRate}
|
|
|
|
/>
|
|
|
|
);
|
2024-03-13 20:44:13 +00:00
|
|
|
});
|