2022-08-31 15:57:31 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2022-09-27 07:10:28 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2022-08-31 15:57:31 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
2022-09-05 11:24:13 +00:00
|
|
|
import { IState } from '../../app/types';
|
2022-08-31 15:57:31 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import { Dialog } from '../../base/dialog';
|
|
|
|
// @ts-ignore
|
2022-09-29 11:44:59 +00:00
|
|
|
import { TRANSLATION_LANGUAGES, TRANSLATION_LANGUAGES_HEAD } from '../../base/i18n';
|
2022-09-05 11:24:13 +00:00
|
|
|
import { connect } from '../../base/redux/functions';
|
2022-08-31 15:57:31 +00:00
|
|
|
// @ts-ignore
|
2022-09-29 11:44:59 +00:00
|
|
|
import { setRequestingSubtitles, toggleLanguageSelectorDialog, updateTranslationLanguage } from '../actions';
|
2022-08-31 15:57:31 +00:00
|
|
|
|
|
|
|
import LanguageList from './LanguageList';
|
|
|
|
|
|
|
|
interface ILanguageSelectorDialogProps {
|
2022-09-08 09:52:36 +00:00
|
|
|
_language: string;
|
2022-09-29 11:44:59 +00:00
|
|
|
_translationLanguages: Array<string>;
|
|
|
|
_translationLanguagesHead: Array<string>;
|
2022-09-08 09:52:36 +00:00
|
|
|
t: Function;
|
2022-08-31 15:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that renders the subtitle language selector dialog.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<any>}
|
|
|
|
*/
|
2022-09-29 11:44:59 +00:00
|
|
|
const LanguageSelectorDialog = ({ _language, _translationLanguages, _translationLanguagesHead }:
|
|
|
|
ILanguageSelectorDialogProps) => {
|
|
|
|
|
2022-08-31 15:57:31 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const off = 'transcribing.subtitlesOff';
|
|
|
|
const [ language, setLanguage ] = useState(off);
|
|
|
|
|
2022-09-29 11:44:59 +00:00
|
|
|
const languagesHead = _translationLanguagesHead.map((lang: string) => `translation-languages:${lang}`);
|
|
|
|
// The off and the head languages are always on the top of the list. But once you are selecting
|
|
|
|
// a language from the translationLanguages, that language is moved under the fixedItems list,
|
|
|
|
// until a new languages is selected. FixedItems keep their positions.
|
|
|
|
const fixedItems = [ off, ...languagesHead ];
|
|
|
|
const languages = _translationLanguages
|
|
|
|
.map((lang: string) => `translation-languages:${lang}`)
|
|
|
|
.filter((lang: string) => !(lang === language || languagesHead.includes(lang)));
|
2022-08-31 15:57:31 +00:00
|
|
|
|
|
|
|
const listItems = (fixedItems.includes(language)
|
|
|
|
? [ ...fixedItems, ...languages ]
|
|
|
|
: [ ...fixedItems, language, ...languages ])
|
|
|
|
.map((lang, index) => {
|
|
|
|
return {
|
|
|
|
id: lang + index,
|
|
|
|
lang,
|
|
|
|
selected: lang === language
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
_language ? setLanguage(_language) : setLanguage(off);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onLanguageSelected = useCallback((e: string) => {
|
|
|
|
setLanguage(e);
|
|
|
|
dispatch(updateTranslationLanguage(e));
|
|
|
|
dispatch(setRequestingSubtitles(e !== off));
|
2022-09-29 11:44:59 +00:00
|
|
|
dispatch(toggleLanguageSelectorDialog());
|
2022-08-31 15:57:31 +00:00
|
|
|
}, [ _language ]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
hideCancelButton = { true }
|
|
|
|
submitDisabled = { true }
|
|
|
|
titleKey = 'transcribing.subtitles'
|
|
|
|
width = { 'small' }>
|
|
|
|
<LanguageList
|
|
|
|
items = { listItems }
|
|
|
|
onLanguageSelected = { onLanguageSelected }
|
|
|
|
selectedLanguage = { language } />
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps (parts of) the Redux state to the associated props for the
|
|
|
|
* {@code LanguageSelectorDialog} component.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @private
|
|
|
|
* @returns {Props}
|
|
|
|
*/
|
2022-09-05 11:24:13 +00:00
|
|
|
function mapStateToProps(state: IState) {
|
2022-09-29 11:44:59 +00:00
|
|
|
const { conference } = state['features/base/conference'];
|
|
|
|
const { _language } = state['features/subtitles'];
|
|
|
|
const { transcription } = state['features/base/config'];
|
2022-08-31 15:57:31 +00:00
|
|
|
|
2022-09-29 11:44:59 +00:00
|
|
|
const languages = transcription?.translationLanguages ?? TRANSLATION_LANGUAGES;
|
|
|
|
const languagesHead = transcription?.translationLanguagesHead ?? TRANSLATION_LANGUAGES_HEAD;
|
2022-08-31 15:57:31 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
_conference: conference,
|
2022-09-29 11:44:59 +00:00
|
|
|
_language,
|
|
|
|
_translationLanguages: languages,
|
|
|
|
_translationLanguagesHead: languagesHead
|
2022-08-31 15:57:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-05 11:24:13 +00:00
|
|
|
export default connect(mapStateToProps)(LanguageSelectorDialog);
|