2022-08-25 11:35:19 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2022-03-29 09:07:55 +00:00
|
|
|
import { makeStyles } from '@material-ui/styles';
|
2021-09-10 07:00:54 +00:00
|
|
|
import React, { useCallback, useRef } from 'react';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { WithTranslation } from 'react-i18next';
|
2021-10-25 10:04:51 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2021-09-10 07:00:54 +00:00
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
import { translate } from '../../base/i18n/functions';
|
|
|
|
import Icon from '../../base/icons/components/Icon';
|
2022-09-06 17:32:20 +00:00
|
|
|
import { IconPlusCircle } from '../../base/icons/svg';
|
2021-09-10 07:00:54 +00:00
|
|
|
import { VIRTUAL_BACKGROUND_TYPE, type Image } from '../constants';
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2021-09-10 07:00:54 +00:00
|
|
|
import { resizeImage } from '../functions';
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2021-09-10 07:00:54 +00:00
|
|
|
import logger from '../logger';
|
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
interface Props extends WithTranslation {
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used to set the 'loading' state of the parent component.
|
|
|
|
*/
|
|
|
|
setLoading: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used to set the options.
|
|
|
|
*/
|
|
|
|
setOptions: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used to set the storedImages array.
|
|
|
|
*/
|
|
|
|
setStoredImages: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If a label should be displayed alongside the button.
|
|
|
|
*/
|
|
|
|
showLabel: boolean,
|
|
|
|
|
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* A list of images locally stored.
|
2021-09-10 07:00:54 +00:00
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
storedImages: Array<Image>
|
2021-09-10 07:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const useStyles = makeStyles((theme: any) => {
|
2022-03-29 09:07:55 +00:00
|
|
|
return {
|
|
|
|
addBackground: {
|
|
|
|
marginRight: `${theme.spacing(2)}px`
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
display: 'none'
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
fontSize: '14px',
|
|
|
|
fontWeight: '600',
|
|
|
|
lineHeight: '20px',
|
|
|
|
marginLeft: '-10px',
|
|
|
|
marginTop: `${theme.spacing(3)}px`,
|
|
|
|
marginBottom: `${theme.spacing(2)}px`,
|
|
|
|
color: '#669aec',
|
|
|
|
display: 'inline-flex',
|
|
|
|
cursor: 'pointer'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-09-10 07:00:54 +00:00
|
|
|
/**
|
|
|
|
* Component used to upload an image.
|
|
|
|
*
|
|
|
|
* @param {Object} Props - The props of the component.
|
|
|
|
* @returns {React$Node}
|
|
|
|
*/
|
|
|
|
function UploadImageButton({
|
|
|
|
setLoading,
|
|
|
|
setOptions,
|
|
|
|
setStoredImages,
|
|
|
|
showLabel,
|
|
|
|
storedImages,
|
|
|
|
t
|
|
|
|
}: Props) {
|
2022-03-29 09:07:55 +00:00
|
|
|
const classes = useStyles();
|
2022-08-25 11:35:19 +00:00
|
|
|
const uploadImageButton = useRef<HTMLInputElement>(null);
|
2021-09-10 07:00:54 +00:00
|
|
|
const uploadImageKeyPress = useCallback(e => {
|
|
|
|
if (uploadImageButton.current && (e.key === ' ' || e.key === 'Enter')) {
|
|
|
|
e.preventDefault();
|
|
|
|
uploadImageButton.current.click();
|
|
|
|
}
|
|
|
|
}, [ uploadImageButton.current ]);
|
|
|
|
|
|
|
|
|
|
|
|
const uploadImage = useCallback(async e => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
const imageFile = e.target.files;
|
|
|
|
|
|
|
|
reader.readAsDataURL(imageFile[0]);
|
|
|
|
reader.onload = async () => {
|
|
|
|
const url = await resizeImage(reader.result);
|
2021-10-25 10:04:51 +00:00
|
|
|
const uuId = uuidv4();
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
setStoredImages([
|
|
|
|
...storedImages,
|
|
|
|
{
|
|
|
|
id: uuId,
|
|
|
|
src: url
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
setOptions({
|
|
|
|
backgroundType: VIRTUAL_BACKGROUND_TYPE.IMAGE,
|
|
|
|
enabled: true,
|
|
|
|
url,
|
|
|
|
selectedThumbnail: uuId
|
|
|
|
});
|
|
|
|
};
|
|
|
|
logger.info('New virtual background image uploaded!');
|
|
|
|
|
|
|
|
reader.onerror = () => {
|
|
|
|
setLoading(false);
|
|
|
|
logger.error('Failed to upload virtual image!');
|
|
|
|
};
|
|
|
|
}, [ storedImages ]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{showLabel && <label
|
|
|
|
aria-label = { t('virtualBackground.uploadImage') }
|
2022-03-29 09:07:55 +00:00
|
|
|
className = { classes.label }
|
2021-09-10 07:00:54 +00:00
|
|
|
htmlFor = 'file-upload'
|
|
|
|
onKeyPress = { uploadImageKeyPress }
|
|
|
|
tabIndex = { 0 } >
|
|
|
|
<Icon
|
2022-03-29 09:07:55 +00:00
|
|
|
className = { classes.addBackground }
|
2021-09-10 07:00:54 +00:00
|
|
|
size = { 20 }
|
|
|
|
src = { IconPlusCircle } />
|
|
|
|
{t('virtualBackground.addBackground')}
|
|
|
|
</label>}
|
|
|
|
|
|
|
|
<input
|
|
|
|
accept = 'image/*'
|
2022-03-29 09:07:55 +00:00
|
|
|
className = { classes.button }
|
2021-09-10 07:00:54 +00:00
|
|
|
id = 'file-upload'
|
|
|
|
onChange = { uploadImage }
|
|
|
|
ref = { uploadImageButton }
|
|
|
|
type = 'file' />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(UploadImageButton);
|