2022-08-24 23:40:03 +08:00
|
|
|
import React, { useRef, useEffect } from 'react';
|
2022-03-25 03:05:20 +08:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import Service from '/imports/ui/components/audio/captions/service';
|
2022-08-24 23:40:03 +08:00
|
|
|
import SpeechService from '/imports/ui/components/audio/captions/speech/service';
|
|
|
|
import ButtonEmoji from '/imports/ui/components/common/button/button-emoji/ButtonEmoji';
|
|
|
|
import BBBMenu from '/imports/ui/components/common/menu/component';
|
2022-06-30 01:18:15 +08:00
|
|
|
import Styled from './styles';
|
2022-03-25 03:05:20 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
start: {
|
|
|
|
id: 'app.audio.captions.button.start',
|
|
|
|
description: 'Start audio captions',
|
|
|
|
},
|
|
|
|
stop: {
|
|
|
|
id: 'app.audio.captions.button.stop',
|
|
|
|
description: 'Stop audio captions',
|
|
|
|
},
|
2022-08-24 23:40:03 +08:00
|
|
|
transcriptionSettings: {
|
|
|
|
id: 'app.audio.captions.button.transcriptionSettings',
|
|
|
|
description: 'Audio captions settings modal',
|
|
|
|
},
|
|
|
|
transcription: {
|
|
|
|
id: 'app.audio.captions.button.transcription',
|
|
|
|
description: 'Audio speech transcription label',
|
|
|
|
},
|
|
|
|
transcriptionOn: {
|
|
|
|
id: 'app.switch.onLabel',
|
|
|
|
},
|
|
|
|
transcriptionOff: {
|
|
|
|
id: 'app.switch.offLabel',
|
|
|
|
},
|
|
|
|
language: {
|
|
|
|
id: 'app.audio.captions.button.language',
|
|
|
|
description: 'Audio speech recognition language label',
|
|
|
|
},
|
|
|
|
'de-DE': {
|
|
|
|
id: 'app.audio.captions.select.de-DE',
|
|
|
|
description: 'Audio speech recognition german language',
|
|
|
|
},
|
|
|
|
'en-US': {
|
|
|
|
id: 'app.audio.captions.select.en-US',
|
|
|
|
description: 'Audio speech recognition english language',
|
|
|
|
},
|
|
|
|
'es-ES': {
|
|
|
|
id: 'app.audio.captions.select.es-ES',
|
|
|
|
description: 'Audio speech recognition spanish language',
|
|
|
|
},
|
|
|
|
'fr-FR': {
|
|
|
|
id: 'app.audio.captions.select.fr-FR',
|
|
|
|
description: 'Audio speech recognition french language',
|
|
|
|
},
|
|
|
|
'hi-ID': {
|
|
|
|
id: 'app.audio.captions.select.hi-ID',
|
|
|
|
description: 'Audio speech recognition indian language',
|
|
|
|
},
|
|
|
|
'it-IT': {
|
|
|
|
id: 'app.audio.captions.select.it-IT',
|
|
|
|
description: 'Audio speech recognition italian language',
|
|
|
|
},
|
|
|
|
'ja-JP': {
|
|
|
|
id: 'app.audio.captions.select.ja-JP',
|
|
|
|
description: 'Audio speech recognition japanese language',
|
|
|
|
},
|
|
|
|
'pt-BR': {
|
|
|
|
id: 'app.audio.captions.select.pt-BR',
|
|
|
|
description: 'Audio speech recognition portuguese language',
|
|
|
|
},
|
|
|
|
'ru-RU': {
|
|
|
|
id: 'app.audio.captions.select.ru-RU',
|
|
|
|
description: 'Audio speech recognition russian language',
|
|
|
|
},
|
|
|
|
'zh-CN': {
|
|
|
|
id: 'app.audio.captions.select.zh-CN',
|
|
|
|
description: 'Audio speech recognition chinese language',
|
|
|
|
},
|
2022-03-25 03:05:20 +08:00
|
|
|
});
|
|
|
|
|
2022-08-24 23:40:03 +08:00
|
|
|
const DEFAULT_LOCALE = 'en-US';
|
|
|
|
const DISABLED = '';
|
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
const CaptionsButton = ({
|
|
|
|
intl,
|
|
|
|
active,
|
2022-08-24 23:40:03 +08:00
|
|
|
isRTL,
|
2022-03-25 03:05:20 +08:00
|
|
|
enabled,
|
2022-08-24 23:40:03 +08:00
|
|
|
currentSpeechLocale,
|
|
|
|
availableVoices,
|
|
|
|
isSupported,
|
|
|
|
isVoiceUser,
|
2022-03-25 03:05:20 +08:00
|
|
|
}) => {
|
2022-08-24 23:40:03 +08:00
|
|
|
const isTranscriptionDisabled = () => (
|
|
|
|
currentSpeechLocale === DISABLED
|
|
|
|
);
|
|
|
|
|
|
|
|
const fallbackLocale = availableVoices.includes(navigator.language)
|
|
|
|
? navigator.language : DEFAULT_LOCALE;
|
|
|
|
|
|
|
|
const getSelectedLocaleValue = (isTranscriptionDisabled() ? fallbackLocale : currentSpeechLocale);
|
|
|
|
|
|
|
|
const selectedLocale = useRef(getSelectedLocaleValue);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isTranscriptionDisabled()) selectedLocale.current = getSelectedLocaleValue;
|
|
|
|
}, [currentSpeechLocale]);
|
|
|
|
|
2022-11-22 20:16:26 +08:00
|
|
|
if (!enabled) return null;
|
|
|
|
|
2022-08-24 23:40:03 +08:00
|
|
|
const shouldRenderChevron = isSupported && isVoiceUser;
|
|
|
|
|
2023-08-24 22:59:04 +08:00
|
|
|
const getAvailableLocales = () => {
|
|
|
|
let indexToInsertSeparator = -1;
|
|
|
|
const availableVoicesObjectToMenu = availableVoices.map((availableVoice, index) => {
|
|
|
|
if (availableVoice === availableVoices[0]) {
|
|
|
|
indexToInsertSeparator = index;
|
2022-08-24 23:40:03 +08:00
|
|
|
}
|
2023-08-24 22:59:04 +08:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
icon: '',
|
|
|
|
label: intl.formatMessage(intlMessages[availableVoice]),
|
|
|
|
key: availableVoice,
|
|
|
|
iconRight: selectedLocale.current === availableVoice ? 'check' : null,
|
|
|
|
customStyles: (selectedLocale.current === availableVoice) && Styled.SelectedLabel,
|
|
|
|
disabled: isTranscriptionDisabled(),
|
|
|
|
onClick: () => {
|
|
|
|
selectedLocale.current = availableVoice;
|
|
|
|
SpeechService.setSpeechLocale(selectedLocale.current);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (indexToInsertSeparator >= 0) {
|
|
|
|
availableVoicesObjectToMenu.splice(indexToInsertSeparator, 0, {
|
|
|
|
key: 'separator-01',
|
|
|
|
isSeparator: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
...availableVoicesObjectToMenu,
|
|
|
|
];
|
|
|
|
};
|
2022-08-24 23:40:03 +08:00
|
|
|
|
|
|
|
const toggleTranscription = () => {
|
|
|
|
SpeechService.setSpeechLocale(isTranscriptionDisabled() ? selectedLocale.current : DISABLED);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAvailableLocalesList = () => (
|
|
|
|
[{
|
|
|
|
key: 'availableLocalesList',
|
|
|
|
label: intl.formatMessage(intlMessages.language),
|
|
|
|
customStyles: Styled.TitleLabel,
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
...getAvailableLocales(),
|
|
|
|
{
|
|
|
|
key: 'divider',
|
|
|
|
label: intl.formatMessage(intlMessages.transcription),
|
|
|
|
customStyles: Styled.TitleLabel,
|
|
|
|
disabled: true,
|
2023-08-24 22:59:04 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'separator-02',
|
|
|
|
isSeparator: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-24 23:40:03 +08:00
|
|
|
key: 'transcriptionStatus',
|
|
|
|
label: intl.formatMessage(
|
|
|
|
isTranscriptionDisabled()
|
|
|
|
? intlMessages.transcriptionOn
|
|
|
|
: intlMessages.transcriptionOff,
|
|
|
|
),
|
|
|
|
customStyles: isTranscriptionDisabled()
|
|
|
|
? Styled.EnableTrascription : Styled.DisableTrascription,
|
|
|
|
disabled: false,
|
|
|
|
onClick: toggleTranscription,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onToggleClick = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
Service.setAudioCaptions(!active);
|
|
|
|
};
|
|
|
|
|
|
|
|
const startStopCaptionsButton = (
|
2022-06-30 01:18:15 +08:00
|
|
|
<Styled.ClosedCaptionToggleButton
|
2022-04-14 21:59:54 +08:00
|
|
|
icon={active ? 'closed_caption' : 'closed_caption_stop'}
|
2022-03-25 03:05:20 +08:00
|
|
|
label={intl.formatMessage(active ? intlMessages.stop : intlMessages.start)}
|
|
|
|
color={active ? 'primary' : 'default'}
|
|
|
|
ghost={!active}
|
|
|
|
hideLabel
|
|
|
|
circle
|
|
|
|
size="lg"
|
2022-08-24 23:40:03 +08:00
|
|
|
onClick={onToggleClick}
|
2022-03-25 03:05:20 +08:00
|
|
|
/>
|
|
|
|
);
|
2022-08-24 23:40:03 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
shouldRenderChevron
|
|
|
|
? (
|
|
|
|
<Styled.SpanButtonWrapper>
|
|
|
|
<BBBMenu
|
|
|
|
trigger={(
|
|
|
|
<>
|
|
|
|
{ startStopCaptionsButton }
|
|
|
|
<ButtonEmoji
|
|
|
|
emoji="device_list_selector"
|
|
|
|
hideLabel
|
|
|
|
label={intl.formatMessage(intlMessages.transcriptionSettings)}
|
|
|
|
tabIndex={0}
|
|
|
|
rotate
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
actions={getAvailableLocalesList()}
|
|
|
|
opts={{
|
|
|
|
id: 'default-dropdown-menu',
|
|
|
|
keepMounted: true,
|
|
|
|
transitionDuration: 0,
|
|
|
|
elevation: 3,
|
2023-05-10 09:31:48 +08:00
|
|
|
getcontentanchorel: null,
|
2022-08-24 23:40:03 +08:00
|
|
|
fullwidth: 'true',
|
|
|
|
anchorOrigin: { vertical: 'top', horizontal: isRTL ? 'right' : 'left' },
|
|
|
|
transformOrigin: { vertical: 'bottom', horizontal: isRTL ? 'right' : 'left' },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Styled.SpanButtonWrapper>
|
|
|
|
) : startStopCaptionsButton
|
|
|
|
);
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CaptionsButton.propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
active: PropTypes.bool.isRequired,
|
2022-08-24 23:40:03 +08:00
|
|
|
isRTL: PropTypes.bool.isRequired,
|
2022-03-25 03:05:20 +08:00
|
|
|
enabled: PropTypes.bool.isRequired,
|
2022-08-24 23:40:03 +08:00
|
|
|
currentSpeechLocale: PropTypes.string.isRequired,
|
|
|
|
availableVoices: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
|
|
isSupported: PropTypes.bool.isRequired,
|
|
|
|
isVoiceUser: PropTypes.bool.isRequired,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default injectIntl(CaptionsButton);
|