signal-desktop/ts/state/ducks/audioPlayer.ts
Fedor Indutny 12d7f24d0f New UI for audio playback and global audio player
Introduce new UI and behavior for playing audio attachments in
conversations. Previously, playback stopped unexpectedly during window
resizes and scrolling through the messages due to the row height
recomputation in `react-virtualized`.

With this commit we introduce `<GlobalAudioContext/>` instance that
wraps whole conversation and provides an `<audio/>` element that
doesn't get re-rendered (or destroyed) whenever `react-virtualized`
recomputes messages. The audio players (with a freshly designed UI) now
share this global `<audio/>` instance and manage access to it using
`audioPlayer.owner` state from the redux.

New UI computes on the fly, caches, and displays waveforms for each
audio attachment. Storybook had to be slightly modified to accomodate
testing of Android bubbles by introducing the new knob for
`authorColor`.
2021-03-19 16:57:35 -04:00

70 lines
1.4 KiB
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useBoundActions } from '../../util/hooks';
import { SwitchToAssociatedViewActionType } from './conversations';
// State
export type AudioPlayerStateType = {
readonly activeAudioID: string | undefined;
};
// Actions
type SetActiveAudioIDAction = {
type: 'audioPlayer/SET_ACTIVE_AUDIO_ID';
payload: {
id: string | undefined;
};
};
type AudioPlayerActionType = SetActiveAudioIDAction;
// Action Creators
export const actions = {
setActiveAudioID,
};
export const useActions = (): typeof actions => useBoundActions(actions);
function setActiveAudioID(id: string | undefined): SetActiveAudioIDAction {
return {
type: 'audioPlayer/SET_ACTIVE_AUDIO_ID',
payload: { id },
};
}
// Reducer
function getEmptyState(): AudioPlayerStateType {
return {
activeAudioID: undefined,
};
}
export function reducer(
state: Readonly<AudioPlayerStateType> = getEmptyState(),
action: Readonly<AudioPlayerActionType | SwitchToAssociatedViewActionType>
): AudioPlayerStateType {
if (action.type === 'audioPlayer/SET_ACTIVE_AUDIO_ID') {
const { payload } = action;
return {
...state,
activeAudioID: payload.id,
};
}
// Reset activeAudioID on conversation change.
if (action.type === 'SWITCH_TO_ASSOCIATED_VIEW') {
return {
...state,
activeAudioID: undefined,
};
}
return state;
}