2022-09-01 11:00:49 +00:00
|
|
|
import ReducerRegistry from '../base/redux/ReducerRegistry';
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
import {
|
2022-08-31 15:57:31 +00:00
|
|
|
REMOVE_TRANSCRIPT_MESSAGE,
|
|
|
|
SET_REQUESTING_SUBTITLES, UPDATE_TRANSCRIPT_MESSAGE, UPDATE_TRANSLATION_LANGUAGE
|
2018-07-17 17:31:12 +00:00
|
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
/**
|
2021-11-04 21:10:43 +00:00
|
|
|
* Default State for 'features/transcription' feature.
|
2018-07-17 17:31:12 +00:00
|
|
|
*/
|
|
|
|
const defaultState = {
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: new Map(),
|
2022-08-31 15:57:31 +00:00
|
|
|
_requestingSubtitles: false,
|
|
|
|
_language: 'transcribing.subtitlesOff'
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
|
2022-09-01 11:00:49 +00:00
|
|
|
export interface ISubtitlesState {
|
|
|
|
_language: string;
|
|
|
|
_requestingSubtitles: boolean;
|
|
|
|
_transcriptMessages: Map<string, Object>;
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:31:12 +00:00
|
|
|
/**
|
|
|
|
* Listen for actions for the transcription feature to be used by the actions
|
|
|
|
* to update the rendered transcription subtitles.
|
|
|
|
*/
|
2022-09-05 09:05:07 +00:00
|
|
|
ReducerRegistry.register<ISubtitlesState>('features/subtitles', (
|
|
|
|
state = defaultState, action): ISubtitlesState => {
|
2018-07-17 17:31:12 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case REMOVE_TRANSCRIPT_MESSAGE:
|
|
|
|
return _removeTranscriptMessage(state, action);
|
|
|
|
case UPDATE_TRANSCRIPT_MESSAGE:
|
|
|
|
return _updateTranscriptMessage(state, action);
|
2022-08-31 15:57:31 +00:00
|
|
|
case UPDATE_TRANSLATION_LANGUAGE:
|
2018-08-22 17:49:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2022-08-31 15:57:31 +00:00
|
|
|
_language: action.value
|
2018-08-22 17:49:58 +00:00
|
|
|
};
|
2022-04-20 08:43:18 +00:00
|
|
|
case SET_REQUESTING_SUBTITLES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
_requestingSubtitles: action.enabled
|
|
|
|
};
|
2018-07-17 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
|
|
|
|
* transcription.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature transcription.
|
|
|
|
* @param {Action} action -The Redux action REMOVE_TRANSCRIPT_MESSAGE to reduce.
|
|
|
|
* @returns {Object} The new state of the feature transcription after the
|
|
|
|
* reduction of the specified action.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
function _removeTranscriptMessage(state: ISubtitlesState, { transcriptMessageID }: { transcriptMessageID: string; }) {
|
2018-09-11 18:08:15 +00:00
|
|
|
const newTranscriptMessages = new Map(state._transcriptMessages);
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
// Deletes the key from Map once a final message arrives.
|
|
|
|
newTranscriptMessages.delete(transcriptMessageID);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: newTranscriptMessages
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduces a specific Redux action UPDATE_TRANSCRIPT_MESSAGE of the feature
|
|
|
|
* transcription.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature transcription.
|
|
|
|
* @param {Action} action -The Redux action UPDATE_TRANSCRIPT_MESSAGE to reduce.
|
|
|
|
* @returns {Object} The new state of the feature transcription after the
|
|
|
|
* reduction of the specified action.
|
|
|
|
*/
|
2022-09-01 11:00:49 +00:00
|
|
|
function _updateTranscriptMessage(state: ISubtitlesState, { transcriptMessageID, newTranscriptMessage }:
|
2022-09-08 09:52:36 +00:00
|
|
|
{ newTranscriptMessage: Object; transcriptMessageID: string; }) {
|
2018-09-11 18:08:15 +00:00
|
|
|
const newTranscriptMessages = new Map(state._transcriptMessages);
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
// Updates the new message for the given key in the Map.
|
|
|
|
newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: newTranscriptMessages
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
}
|