2021-03-10 20:36:58 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-06-29 19:58:29 +00:00
|
|
|
SwitchToAssociatedViewActionType,
|
|
|
|
MessageDeletedActionType,
|
|
|
|
MessageChangedActionType,
|
|
|
|
} from './conversations';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
|
|
|
export type AudioPlayerStateType = {
|
|
|
|
readonly activeAudioID: string | undefined;
|
2021-06-29 19:58:29 +00:00
|
|
|
readonly activeAudioContext: string | undefined;
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
type SetActiveAudioIDAction = {
|
|
|
|
type: 'audioPlayer/SET_ACTIVE_AUDIO_ID';
|
|
|
|
payload: {
|
|
|
|
id: string | undefined;
|
2021-06-29 19:58:29 +00:00
|
|
|
context: string | undefined;
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type AudioPlayerActionType = SetActiveAudioIDAction;
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
setActiveAudioID,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useActions = (): typeof actions => useBoundActions(actions);
|
|
|
|
|
2021-06-29 19:58:29 +00:00
|
|
|
function setActiveAudioID(
|
|
|
|
id: string | undefined,
|
|
|
|
context: string
|
|
|
|
): SetActiveAudioIDAction {
|
2021-03-10 20:36:58 +00:00
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_ACTIVE_AUDIO_ID',
|
2021-06-29 19:58:29 +00:00
|
|
|
payload: { id, context },
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
function getEmptyState(): AudioPlayerStateType {
|
|
|
|
return {
|
|
|
|
activeAudioID: undefined,
|
2021-06-29 19:58:29 +00:00
|
|
|
activeAudioContext: undefined,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: Readonly<AudioPlayerStateType> = getEmptyState(),
|
2021-06-29 19:58:29 +00:00
|
|
|
action: Readonly<
|
|
|
|
| AudioPlayerActionType
|
|
|
|
| SwitchToAssociatedViewActionType
|
|
|
|
| MessageDeletedActionType
|
|
|
|
| MessageChangedActionType
|
|
|
|
>
|
2021-03-10 20:36:58 +00:00
|
|
|
): AudioPlayerStateType {
|
|
|
|
if (action.type === 'audioPlayer/SET_ACTIVE_AUDIO_ID') {
|
|
|
|
const { payload } = action;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
activeAudioID: payload.id,
|
2021-06-29 19:58:29 +00:00
|
|
|
activeAudioContext: payload.context,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset activeAudioID on conversation change.
|
|
|
|
if (action.type === 'SWITCH_TO_ASSOCIATED_VIEW') {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
activeAudioID: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-29 19:58:29 +00:00
|
|
|
// Reset activeAudioID on when played message is deleted on expiration.
|
|
|
|
if (action.type === 'MESSAGE_DELETED') {
|
|
|
|
const { id } = action.payload;
|
|
|
|
if (state.activeAudioID !== id) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
activeAudioID: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset activeAudioID on when played message is deleted for everyone.
|
|
|
|
if (action.type === 'MESSAGE_CHANGED') {
|
|
|
|
const { id, data } = action.payload;
|
|
|
|
|
|
|
|
if (state.activeAudioID !== id) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.deletedForEveryone) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
activeAudioID: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:36:58 +00:00
|
|
|
return state;
|
|
|
|
}
|