You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LanguageSelectorDialog.web.tsx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import i18next from 'i18next';
  2. import React, { useCallback, useEffect, useState } from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { useDispatch } from 'react-redux';
  5. import { makeStyles } from 'tss-react/mui';
  6. import { IReduxState } from '../../app/types';
  7. // eslint-disable-next-line lines-around-comment
  8. // @ts-ignore
  9. import { TRANSLATION_LANGUAGES, TRANSLATION_LANGUAGES_HEAD } from '../../base/i18n';
  10. import { translate, translateToHTML } from '../../base/i18n/functions';
  11. import { connect } from '../../base/redux/functions';
  12. import Dialog from '../../base/ui/components/web/Dialog';
  13. import { openSettingsDialog } from '../../settings/actions';
  14. import { SETTINGS_TABS } from '../../settings/constants';
  15. import { setRequestingSubtitles, toggleLanguageSelectorDialog, updateTranslationLanguage } from '../actions';
  16. import LanguageList from './LanguageList.web';
  17. interface ILanguageSelectorDialogProps extends WithTranslation {
  18. _language: string;
  19. _translationLanguages: Array<string>;
  20. _translationLanguagesHead: Array<string>;
  21. }
  22. const useStyles = makeStyles()(theme => {
  23. return {
  24. paragraphWrapper: {
  25. fontSize: 14,
  26. margin: '10px 0px',
  27. color: theme.palette.text01
  28. },
  29. spanWrapper: {
  30. fontWeight: 700,
  31. cursor: 'pointer',
  32. color: theme.palette.link01,
  33. '&:hover': {
  34. backgroundColor: theme.palette.ui04,
  35. color: theme.palette.link01Hover
  36. }
  37. }
  38. };
  39. });
  40. /**
  41. * Component that renders the subtitle language selector dialog.
  42. *
  43. * @returns {React$Element<any>}
  44. */
  45. const LanguageSelectorDialog = ({
  46. t,
  47. _language,
  48. _translationLanguages,
  49. _translationLanguagesHead
  50. }: ILanguageSelectorDialogProps) => {
  51. const { classes: styles } = useStyles();
  52. const dispatch = useDispatch();
  53. const off = 'transcribing.subtitlesOff';
  54. const [ language, setLanguage ] = useState(off);
  55. const languagesHead = _translationLanguagesHead.map((lang: string) => `translation-languages:${lang}`);
  56. // The off and the head languages are always on the top of the list. But once you are selecting
  57. // a language from the translationLanguages, that language is moved under the fixedItems list,
  58. // until a new languages is selected. FixedItems keep their positions.
  59. const fixedItems = [ off, ...languagesHead ];
  60. const languages = _translationLanguages
  61. .map((lang: string) => `translation-languages:${lang}`)
  62. .filter((lang: string) => !(lang === language || languagesHead.includes(lang)));
  63. const listItems = (fixedItems.includes(language)
  64. ? [ ...fixedItems, ...languages ]
  65. : [ ...fixedItems, language, ...languages ])
  66. .map((lang, index) => {
  67. return {
  68. id: lang + index,
  69. lang,
  70. selected: lang === language
  71. };
  72. });
  73. useEffect(() => {
  74. _language ? setLanguage(_language) : setLanguage(off);
  75. }, []);
  76. const onLanguageSelected = useCallback((e: string) => {
  77. setLanguage(e);
  78. dispatch(updateTranslationLanguage(e));
  79. dispatch(setRequestingSubtitles(e !== off));
  80. dispatch(toggleLanguageSelectorDialog());
  81. }, [ _language ]);
  82. const onSourceLanguageClick = useCallback(() => {
  83. dispatch(openSettingsDialog(SETTINGS_TABS.PROFILE, false));
  84. }, []);
  85. return (
  86. <Dialog
  87. cancel = {{ hidden: true }}
  88. ok = {{ hidden: true }}
  89. titleKey = 'transcribing.subtitles'>
  90. <p className = { styles.paragraphWrapper } >
  91. {
  92. translateToHTML(t, 'transcribing.sourceLanguageDesc', {
  93. 'sourceLanguage': t(`languages:${i18next.language}`).toLowerCase()
  94. })
  95. }<span
  96. className = { styles.spanWrapper }
  97. onClick = { onSourceLanguageClick }>{t('transcribing.sourceLanguageHere')}.</span>
  98. </p>
  99. <LanguageList
  100. items = { listItems }
  101. onLanguageSelected = { onLanguageSelected }
  102. selectedLanguage = { language } />
  103. </Dialog>
  104. );
  105. };
  106. /**
  107. * Maps (parts of) the Redux state to the associated props for the
  108. * {@code LanguageSelectorDialog} component.
  109. *
  110. * @param {Object} state - The Redux state.
  111. * @private
  112. * @returns {Props}
  113. */
  114. function mapStateToProps(state: IReduxState) {
  115. const { conference } = state['features/base/conference'];
  116. const { _language } = state['features/subtitles'];
  117. const { transcription } = state['features/base/config'];
  118. const languages = transcription?.translationLanguages ?? TRANSLATION_LANGUAGES;
  119. const languagesHead = transcription?.translationLanguagesHead ?? TRANSLATION_LANGUAGES_HEAD;
  120. return {
  121. _conference: conference,
  122. _language,
  123. _translationLanguages: languages,
  124. _translationLanguagesHead: languagesHead
  125. };
  126. }
  127. export default translate(connect(mapStateToProps)(LanguageSelectorDialog));