2022-08-25 11:35:19 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2022-09-13 07:36:00 +00:00
|
|
|
|
|
|
|
import { Theme } from '@mui/material';
|
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';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
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';
|
2022-09-27 07:10:28 +00:00
|
|
|
import { type Image, VIRTUAL_BACKGROUND_TYPE } 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.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
setLoading: Function;
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used to set the options.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
setOptions: Function;
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used to set the storedImages array.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
setStoredImages: Function;
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a label should be displayed alongside the button.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
showLabel: boolean;
|
2021-09-10 07:00:54 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* A list of images locally stored.
|
2021-09-10 07:00:54 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
storedImages: Array<Image>;
|
2021-09-10 07:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2022-09-13 07:36:00 +00:00
|
|
|
const useStyles = makeStyles()((theme: Theme) => {
|
2022-03-29 09:07:55 +00:00
|
|
|
return {
|
|
|
|
addBackground: {
|
2022-09-13 07:36:00 +00:00
|
|
|
marginRight: theme.spacing(2)
|
2022-03-29 09:07:55 +00:00
|
|
|
},
|
|
|
|
button: {
|
|
|
|
display: 'none'
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
fontSize: '14px',
|
|
|
|
fontWeight: '600',
|
|
|
|
lineHeight: '20px',
|
|
|
|
marginLeft: '-10px',
|
2022-09-13 07:36:00 +00:00
|
|
|
marginTop: theme.spacing(3),
|
|
|
|
marginBottom: theme.spacing(2),
|
2022-03-29 09:07:55 +00:00
|
|
|
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-09-13 07:36:00 +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);
|