import React, { useState, useRef } from 'react'; import { findDOMNode } from 'react-dom'; import { defineMessages, injectIntl } from 'react-intl'; import PropTypes from 'prop-types'; import { styles } from './styles'; import Button from '/imports/ui/components/button/component'; import { EFFECT_TYPES, BLUR_FILENAME, IMAGE_NAMES, getVirtualBackgroundThumbnail, isVirtualBackgroundSupported, } from '/imports/ui/services/virtual-background/service'; import { capitalizeFirstLetter } from '/imports/utils/string-utils'; const propTypes = { intl: PropTypes.shape({ formatMessage: PropTypes.func.isRequired, }).isRequired, handleVirtualBgSelected: PropTypes.func.isRequired, locked: PropTypes.bool.isRequired, showThumbnails: PropTypes.bool, initialVirtualBgState: PropTypes.shape({ type: PropTypes.string.isRequired, name: PropTypes.string, }), }; const intlMessages = defineMessages({ virtualBackgroundSettingsLabel: { id: 'app.videoPreview.webcamVirtualBackgroundLabel', description: 'Label for the virtual background', }, virtualBackgroundSettingsDisabledLabel: { id: 'app.videoPreview.webcamVirtualBackgroundDisabledLabel', description: 'Label for the unsupported virtual background', }, noneLabel: { id: 'app.video.virtualBackground.none', description: 'Label for no virtual background selected', }, blurLabel: { id: 'app.video.virtualBackground.blur', description: 'Label for the blurred camera option', }, camBgAriaDesc: { id: 'app.video.virtualBackground.camBgAriaDesc', description: 'Label for virtual background button aria', } }); const VirtualBgSelector = ({ intl, handleVirtualBgSelected, locked, showThumbnails, initialVirtualBgState, }) => { const [currentVirtualBg, setCurrentVirtualBg] = useState({ ...initialVirtualBgState, }); const inputElementsRef = useRef([]); const _virtualBgSelected = (type, name, index) => handleVirtualBgSelected(type, name) .then(switched => { // Reset to the base NONE_TYPE effect if it failed because the expected // behaviour from upstream's method is to actually stop/reset the effect // service if it fails if (!switched) { return setCurrentVirtualBg({ type: EFFECT_TYPES.NONE_TYPE }); } if (index >= 0) { findDOMNode(inputElementsRef.current[index]).focus(); } return setCurrentVirtualBg({ type, name }); }); const renderDropdownSelector = () => { const disabled = locked || !isVirtualBackgroundSupported(); return (
); } const renderThumbnailSelector = () => { const disabled = locked || !isVirtualBackgroundSupported(); return (
<>
) })}
); }; const renderSelector = () => { if (showThumbnails) return renderThumbnailSelector(); return renderDropdownSelector(); }; return ( <> {renderSelector()} ); }; VirtualBgSelector.propTypes = propTypes; VirtualBgSelector.defaultProps = { showThumbnails: false, initialVirtualBgState: { type: EFFECT_TYPES.NONE_TYPE, }, }; export default injectIntl(VirtualBgSelector);