// @flow /* eslint-disable react/jsx-no-bind, no-return-assign */ import Spinner from '@atlaskit/spinner'; import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage'; import React, { useState, useEffect } from 'react'; import uuid from 'uuid'; import { Dialog } from '../../base/dialog'; import { translate } from '../../base/i18n'; import { Icon, IconBlurBackground, IconCancelSelection } from '../../base/icons'; import { connect } from '../../base/redux'; import { Tooltip } from '../../base/tooltip'; import { toggleBackgroundEffect, setVirtualBackground } from '../actions'; import { resizeImage, toDataURL } from '../functions'; import logger from '../logger'; // The limit of virtual background uploads is 21. When the number // of uploads is 22 we trigger the deleteStoredImage function to delete // the first/oldest uploaded background. const backgroundsLimit = 22; const images = [ { id: 1, src: 'images/virtual-background/background-1.jpg' }, { id: 2, src: 'images/virtual-background/background-2.jpg' }, { id: 3, src: 'images/virtual-background/background-3.jpg' }, { id: 4, src: 'images/virtual-background/background-4.jpg' } ]; type Props = { /** * The redux {@code dispatch} function. */ dispatch: Function, /** * Invoked to obtain translated strings. */ t: Function }; /** * Renders virtual background dialog. * * @returns {ReactElement} */ function VirtualBackground({ dispatch, t }: Props) { const localImages = jitsiLocalStorage.getItem('virtualBackgrounds'); const [ storedImages, setStoredImages ] = useState((localImages && JSON.parse(localImages)) || []); const [ loading, isloading ] = useState(false); const deleteStoredImage = image => { setStoredImages(storedImages.filter(item => item !== image)); }; /** * Updates stored images on local storage. */ useEffect(() => { jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages)); if (storedImages.length === backgroundsLimit) { deleteStoredImage(storedImages[0]); } }, [ storedImages ]); const [ selected, setSelected ] = useState(''); const enableBlur = async () => { isloading(true); setSelected('blur'); await dispatch(setVirtualBackground('', false)); await dispatch(toggleBackgroundEffect(true)); isloading(false); }; const removeBackground = async () => { isloading(true); setSelected('none'); await dispatch(setVirtualBackground('', false)); await dispatch(toggleBackgroundEffect(false)); isloading(false); }; const setUploadedImageBackground = async image => { isloading(true); setSelected(image.id); await dispatch(setVirtualBackground(image.src, true)); await dispatch(toggleBackgroundEffect(true)); isloading(false); }; const setImageBackground = async image => { isloading(true); setSelected(image.id); await dispatch(setVirtualBackground(await toDataURL(image.src), true)); await dispatch(toggleBackgroundEffect(true)); isloading(false); }; const uploadImage = async imageFile => { const reader = new FileReader(); reader.readAsDataURL(imageFile[0]); reader.onload = async () => { const resizedImage = await resizeImage(reader.result); isloading(true); setStoredImages([ ...storedImages, { id: uuid.v4(), src: resizedImage } ]); await dispatch(setVirtualBackground(resizedImage, true)); await dispatch(toggleBackgroundEffect(true)); isloading(false); }; reader.onerror = () => { isloading(false); logger.error('Failed to upload virtual image!'); }; }; return ( {loading ? (
{t('virtualBackground.pleaseWait')}
) : (
{t('virtualBackground.none')}
enableBlur() } size = { 50 } src = { IconBlurBackground } /> {images.map((image, index) => ( setImageBackground(image) } onError = { event => event.target.style.display = 'none' } src = { image.src } /> ))} uploadImage(e.target.files) } type = 'file' />
{storedImages.map((image, index) => (
setUploadedImageBackground(image) } onError = { event => event.target.style.display = 'none' } src = { image.src } /> deleteStoredImage(image) } size = { 15 } src = { IconCancelSelection } />
))}
)}
); } export default translate(connect()(VirtualBackground));