Files
jitsi-meet/react/features/subtitles/reducer.ts
Дамян Минков d5269e881a fix(transcribing): Handle transcriber status changed.
* fix(subtitles): Handle errors to revert to default state.

* fix(transcribing): Handle transcriber status changed.

Drops potential transcribers and hidden participant actions and handling. Expect ljm to detect transcriptions on and off.

* feat(transcriptions): Adds a notification if transcriber leaves abruptly.

* squash: Renames action.

* chore(deps) lib-jitsi-meet@latest

https://github.com/jitsi/lib-jitsi-meet/compare/v1869.0.0+5671c5d6...v1872.0.0+8940b5c9
2024-10-02 18:59:04 -05:00

104 lines
3.3 KiB
TypeScript

import ReducerRegistry from '../base/redux/ReducerRegistry';
import { TRANSCRIBER_LEFT } from '../transcribing/actionTypes';
import {
REMOVE_TRANSCRIPT_MESSAGE,
SET_REQUESTING_SUBTITLES,
TOGGLE_REQUESTING_SUBTITLES,
UPDATE_TRANSCRIPT_MESSAGE
} from './actionTypes';
import { ITranscriptMessage } from './types';
/**
* Default State for 'features/transcription' feature.
*/
const defaultState = {
_displaySubtitles: false,
_transcriptMessages: new Map(),
_requestingSubtitles: false,
_language: null
};
export interface ISubtitlesState {
_displaySubtitles: boolean;
_language: string | null;
_requestingSubtitles: boolean;
_transcriptMessages: Map<string, ITranscriptMessage>;
}
/**
* Listen for actions for the transcription feature to be used by the actions
* to update the rendered transcription subtitles.
*/
ReducerRegistry.register<ISubtitlesState>('features/subtitles', (
state = defaultState, action): ISubtitlesState => {
switch (action.type) {
case REMOVE_TRANSCRIPT_MESSAGE:
return _removeTranscriptMessage(state, action);
case UPDATE_TRANSCRIPT_MESSAGE:
return _updateTranscriptMessage(state, action);
case SET_REQUESTING_SUBTITLES:
return {
...state,
_displaySubtitles: action.displaySubtitles,
_language: action.language,
_requestingSubtitles: action.enabled
};
case TOGGLE_REQUESTING_SUBTITLES:
return {
...state,
_requestingSubtitles: !state._requestingSubtitles
};
case TRANSCRIBER_LEFT:
return {
...state,
...defaultState
};
}
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.
*/
function _removeTranscriptMessage(state: ISubtitlesState, { transcriptMessageID }: { transcriptMessageID: string; }) {
const newTranscriptMessages = new Map(state._transcriptMessages);
// Deletes the key from Map once a final message arrives.
newTranscriptMessages.delete(transcriptMessageID);
return {
...state,
_transcriptMessages: newTranscriptMessages
};
}
/**
* 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.
*/
function _updateTranscriptMessage(state: ISubtitlesState, { transcriptMessageID, newTranscriptMessage }:
{ newTranscriptMessage: ITranscriptMessage; transcriptMessageID: string; }) {
const newTranscriptMessages = new Map(state._transcriptMessages);
// Updates the new message for the given key in the Map.
newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
return {
...state,
_transcriptMessages: newTranscriptMessages
};
}