You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VirtualBackgrounds.tsx 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* eslint-disable lines-around-comment */
  2. import Spinner from '@atlaskit/spinner';
  3. // @ts-ignore
  4. import Bourne from '@hapi/bourne';
  5. // @ts-ignore
  6. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  7. import React, { useCallback, useEffect, useState } from 'react';
  8. import { WithTranslation } from 'react-i18next';
  9. import { connect } from 'react-redux';
  10. import { makeStyles } from 'tss-react/mui';
  11. import { IReduxState } from '../../app/types';
  12. import { getMultipleVideoSendingSupportFeatureFlag } from '../../base/config/functions.any';
  13. import { translate } from '../../base/i18n/functions';
  14. import Icon from '../../base/icons/components/Icon';
  15. import { IconCloseLarge } from '../../base/icons/svg';
  16. import { withPixelLineHeight } from '../../base/styles/functions.web';
  17. // @ts-ignore
  18. import { Tooltip } from '../../base/tooltip';
  19. import { BACKGROUNDS_LIMIT, IMAGES, type Image, VIRTUAL_BACKGROUND_TYPE } from '../constants';
  20. import { toDataURL } from '../functions';
  21. import logger from '../logger';
  22. import UploadImageButton from './UploadImageButton';
  23. import VirtualBackgroundPreview from './VirtualBackgroundPreview';
  24. /* eslint-enable lines-around-comment */
  25. interface IProps extends WithTranslation {
  26. /**
  27. * The list of Images to choose from.
  28. */
  29. _images: Array<Image>;
  30. /**
  31. * Returns the jitsi track that will have background effect applied.
  32. */
  33. _jitsiTrack: Object;
  34. /**
  35. * The current local flip x status.
  36. */
  37. _localFlipX: boolean;
  38. /**
  39. * Whether or not multi-stream send support is enabled.
  40. */
  41. _multiStreamModeEnabled: boolean;
  42. /**
  43. * If the upload button should be displayed or not.
  44. */
  45. _showUploadButton: boolean;
  46. /**
  47. * Returns the selected virtual background object.
  48. */
  49. _virtualBackground: any;
  50. /**
  51. * The redux {@code dispatch} function.
  52. */
  53. dispatch: Function;
  54. /**
  55. * The initial options copied in the state for the {@code VirtualBackground} component.
  56. *
  57. * NOTE: currently used only for electron in order to open the dialog in the correct state after desktop sharing
  58. * selection.
  59. */
  60. initialOptions?: Object;
  61. /**
  62. * Options change handler.
  63. */
  64. onOptionsChange: Function;
  65. /**
  66. * Virtual background options.
  67. */
  68. options: any;
  69. /**
  70. * Returns the selected thumbnail identifier.
  71. */
  72. selectedThumbnail: string;
  73. }
  74. const onError = (event: any) => {
  75. event.target.style.display = 'none';
  76. };
  77. const useStyles = makeStyles()(theme => {
  78. return {
  79. virtualBackgroundLoading: {
  80. width: '100%',
  81. display: 'flex',
  82. alignItems: 'center',
  83. justifyContent: 'center',
  84. height: '50px'
  85. },
  86. container: {
  87. width: '100%',
  88. display: 'flex',
  89. flexDirection: 'column'
  90. },
  91. thumbnailContainer: {
  92. width: '100%',
  93. display: 'inline-grid',
  94. gridTemplateColumns: '1fr 1fr 1fr 1fr 1fr',
  95. gap: theme.spacing(1),
  96. '@media (min-width: 608px) and (max-width: 712px)': {
  97. gridTemplateColumns: '1fr 1fr 1fr 1fr'
  98. },
  99. '@media (max-width: 607px)': {
  100. gridTemplateColumns: '1fr 1fr 1fr',
  101. gap: theme.spacing(2)
  102. }
  103. },
  104. thumbnail: {
  105. height: '54px',
  106. width: '100%',
  107. borderRadius: '4px',
  108. boxSizing: 'border-box',
  109. display: 'flex',
  110. alignItems: 'center',
  111. justifyContent: 'center',
  112. textAlign: 'center',
  113. ...withPixelLineHeight(theme.typography.labelBold),
  114. color: theme.palette.text01,
  115. objectFit: 'cover',
  116. [[ '&:hover', '&:focus' ] as any]: {
  117. opacity: 0.5,
  118. cursor: 'pointer',
  119. '& ~ .delete-image-icon': {
  120. display: 'block'
  121. }
  122. },
  123. '@media (max-width: 607px)': {
  124. height: '70px'
  125. }
  126. },
  127. selectedThumbnail: {
  128. border: `2px solid ${theme.palette.action01Hover}`
  129. },
  130. noneThumbnail: {
  131. backgroundColor: theme.palette.ui04
  132. },
  133. slightBlur: {
  134. boxShadow: 'inset 0 0 12px #000000',
  135. background: '#a4a4a4'
  136. },
  137. blur: {
  138. boxShadow: 'inset 0 0 12px #000000',
  139. background: '#7e8287'
  140. },
  141. storedImageContainer: {
  142. position: 'relative',
  143. display: 'flex',
  144. flexDirection: 'column',
  145. '&:focus-within .delete-image-container': {
  146. display: 'block'
  147. }
  148. },
  149. deleteImageIcon: {
  150. position: 'absolute',
  151. top: '3px',
  152. right: '3px',
  153. background: theme.palette.ui03,
  154. borderRadius: '3px',
  155. cursor: 'pointer',
  156. display: 'none',
  157. '@media (max-width: 607px)': {
  158. display: 'block',
  159. padding: '3px'
  160. },
  161. [[ '&:hover', '&:focus' ] as any]: {
  162. display: 'block'
  163. }
  164. }
  165. };
  166. });
  167. /**
  168. * Renders virtual background dialog.
  169. *
  170. * @returns {ReactElement}
  171. */
  172. function VirtualBackgrounds({
  173. _images,
  174. _jitsiTrack,
  175. _localFlipX,
  176. selectedThumbnail,
  177. _showUploadButton,
  178. _virtualBackground,
  179. onOptionsChange,
  180. options,
  181. initialOptions,
  182. t
  183. }: IProps) {
  184. const { classes, cx } = useStyles();
  185. const [ previewIsLoaded, setPreviewIsLoaded ] = useState(false);
  186. const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
  187. const [ storedImages, setStoredImages ] = useState<Array<Image>>((localImages && Bourne.parse(localImages)) || []);
  188. const [ loading, setLoading ] = useState(false);
  189. useEffect(() => {
  190. onOptionsChange({ ...initialOptions });
  191. }, []);
  192. const deleteStoredImage = useCallback(e => {
  193. const imageId = e.currentTarget.getAttribute('data-imageid');
  194. setStoredImages(storedImages.filter(item => item.id !== imageId));
  195. }, [ storedImages ]);
  196. const deleteStoredImageKeyPress = useCallback(e => {
  197. if (e.key === ' ' || e.key === 'Enter') {
  198. e.preventDefault();
  199. deleteStoredImage(e);
  200. }
  201. }, [ deleteStoredImage ]);
  202. /**
  203. * Updates stored images on local storage.
  204. */
  205. useEffect(() => {
  206. try {
  207. jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
  208. } catch (err) {
  209. // Preventing localStorage QUOTA_EXCEEDED_ERR
  210. err && setStoredImages(storedImages.slice(1));
  211. }
  212. if (storedImages.length === BACKGROUNDS_LIMIT) {
  213. setStoredImages(storedImages.slice(1));
  214. }
  215. }, [ storedImages ]);
  216. const enableBlur = useCallback(async () => {
  217. onOptionsChange({
  218. backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
  219. enabled: true,
  220. blurValue: 25,
  221. selectedThumbnail: 'blur'
  222. });
  223. logger.info('"Blur" option set for virtual background preview!');
  224. }, []);
  225. const enableBlurKeyPress = useCallback(e => {
  226. if (e.key === ' ' || e.key === 'Enter') {
  227. e.preventDefault();
  228. enableBlur();
  229. }
  230. }, [ enableBlur ]);
  231. const enableSlideBlur = useCallback(async () => {
  232. onOptionsChange({
  233. backgroundType: VIRTUAL_BACKGROUND_TYPE.BLUR,
  234. enabled: true,
  235. blurValue: 8,
  236. selectedThumbnail: 'slight-blur'
  237. });
  238. logger.info('"Slight-blur" option set for virtual background preview!');
  239. }, []);
  240. const enableSlideBlurKeyPress = useCallback(e => {
  241. if (e.key === ' ' || e.key === 'Enter') {
  242. e.preventDefault();
  243. enableSlideBlur();
  244. }
  245. }, [ enableSlideBlur ]);
  246. const removeBackground = useCallback(async () => {
  247. onOptionsChange({
  248. enabled: false,
  249. selectedThumbnail: 'none'
  250. });
  251. logger.info('"None" option set for virtual background preview!');
  252. }, []);
  253. const removeBackgroundKeyPress = useCallback(e => {
  254. if (e.key === ' ' || e.key === 'Enter') {
  255. e.preventDefault();
  256. removeBackground();
  257. }
  258. }, [ removeBackground ]);
  259. const setUploadedImageBackground = useCallback(async e => {
  260. const imageId = e.currentTarget.getAttribute('data-imageid');
  261. const image = storedImages.find(img => img.id === imageId);
  262. if (image) {
  263. onOptionsChange({
  264. backgroundType: 'image',
  265. enabled: true,
  266. url: image.src,
  267. selectedThumbnail: image.id
  268. });
  269. logger.info('Uploaded image set for virtual background preview!');
  270. }
  271. }, [ storedImages ]);
  272. const setImageBackground = useCallback(async e => {
  273. const imageId = e.currentTarget.getAttribute('data-imageid');
  274. const image = _images.find(img => img.id === imageId);
  275. if (image) {
  276. try {
  277. const url = await toDataURL(image.src);
  278. onOptionsChange({
  279. backgroundType: 'image',
  280. enabled: true,
  281. url,
  282. selectedThumbnail: image.id
  283. });
  284. logger.info('Image set for virtual background preview!');
  285. } catch (err) {
  286. logger.error('Could not fetch virtual background image:', err);
  287. }
  288. setLoading(false);
  289. }
  290. }, []);
  291. const setImageBackgroundKeyPress = useCallback(e => {
  292. if (e.key === ' ' || e.key === 'Enter') {
  293. e.preventDefault();
  294. setImageBackground(e);
  295. }
  296. }, [ setImageBackground ]);
  297. const setUploadedImageBackgroundKeyPress = useCallback(e => {
  298. if (e.key === ' ' || e.key === 'Enter') {
  299. e.preventDefault();
  300. setUploadedImageBackground(e);
  301. }
  302. }, [ setUploadedImageBackground ]);
  303. const loadedPreviewState = useCallback(async loaded => {
  304. await setPreviewIsLoaded(loaded);
  305. }, []);
  306. return (
  307. <>
  308. <VirtualBackgroundPreview
  309. loadedPreview = { loadedPreviewState }
  310. options = { options } />
  311. {loading ? (
  312. <div className = { classes.virtualBackgroundLoading }>
  313. <Spinner
  314. // @ts-ignore
  315. isCompleting = { false }
  316. size = 'medium' />
  317. </div>
  318. ) : (
  319. <div className = { classes.container }>
  320. {_showUploadButton
  321. && <UploadImageButton
  322. setLoading = { setLoading }
  323. setOptions = { onOptionsChange }
  324. setStoredImages = { setStoredImages }
  325. showLabel = { previewIsLoaded }
  326. storedImages = { storedImages } />}
  327. <div
  328. className = { classes.thumbnailContainer }
  329. role = 'radiogroup'
  330. tabIndex = { -1 }>
  331. <Tooltip
  332. content = { t('virtualBackground.removeBackground') }
  333. position = { 'top' }>
  334. <div
  335. aria-checked = { selectedThumbnail === 'none' }
  336. aria-label = { t('virtualBackground.removeBackground') }
  337. className = { cx(classes.thumbnail, classes.noneThumbnail,
  338. selectedThumbnail === 'none' && classes.selectedThumbnail) }
  339. onClick = { removeBackground }
  340. onKeyPress = { removeBackgroundKeyPress }
  341. role = 'radio'
  342. tabIndex = { 0 } >
  343. {t('virtualBackground.none')}
  344. </div>
  345. </Tooltip>
  346. <Tooltip
  347. content = { t('virtualBackground.slightBlur') }
  348. position = { 'top' }>
  349. <div
  350. aria-checked = { selectedThumbnail === 'slight-blur' }
  351. aria-label = { t('virtualBackground.slightBlur') }
  352. className = { cx(classes.thumbnail, classes.slightBlur,
  353. selectedThumbnail === 'slight-blur' && classes.selectedThumbnail) }
  354. onClick = { enableSlideBlur }
  355. onKeyPress = { enableSlideBlurKeyPress }
  356. role = 'radio'
  357. tabIndex = { 0 }>
  358. {t('virtualBackground.slightBlur')}
  359. </div>
  360. </Tooltip>
  361. <Tooltip
  362. content = { t('virtualBackground.blur') }
  363. position = { 'top' }>
  364. <div
  365. aria-checked = { selectedThumbnail === 'blur' }
  366. aria-label = { t('virtualBackground.blur') }
  367. className = { cx(classes.thumbnail, classes.blur,
  368. selectedThumbnail === 'blur' && classes.selectedThumbnail) }
  369. onClick = { enableBlur }
  370. onKeyPress = { enableBlurKeyPress }
  371. role = 'radio'
  372. tabIndex = { 0 }>
  373. {t('virtualBackground.blur')}
  374. </div>
  375. </Tooltip>
  376. {_images.map(image => (
  377. <Tooltip
  378. content = { image.tooltip && t(`virtualBackground.${image.tooltip}`) }
  379. key = { image.id }
  380. position = { 'top' }>
  381. <img
  382. alt = { image.tooltip && t(`virtualBackground.${image.tooltip}`) }
  383. aria-checked = { options?.selectedThumbnail === image.id
  384. || selectedThumbnail === image.id }
  385. className = { cx(classes.thumbnail,
  386. (options?.selectedThumbnail === image.id
  387. || selectedThumbnail === image.id) && classes.selectedThumbnail) }
  388. data-imageid = { image.id }
  389. onClick = { setImageBackground }
  390. onError = { onError }
  391. onKeyPress = { setImageBackgroundKeyPress }
  392. role = 'radio'
  393. src = { image.src }
  394. tabIndex = { 0 } />
  395. </Tooltip>
  396. ))}
  397. {storedImages.map((image, index) => (
  398. <div
  399. className = { classes.storedImageContainer }
  400. key = { image.id }>
  401. <img
  402. alt = { t('virtualBackground.uploadedImage', { index: index + 1 }) }
  403. aria-checked = { selectedThumbnail === image.id }
  404. className = { cx(classes.thumbnail,
  405. selectedThumbnail === image.id && classes.selectedThumbnail) }
  406. data-imageid = { image.id }
  407. onClick = { setUploadedImageBackground }
  408. onError = { onError }
  409. onKeyPress = { setUploadedImageBackgroundKeyPress }
  410. role = 'radio'
  411. src = { image.src }
  412. tabIndex = { 0 } />
  413. <Icon
  414. ariaLabel = { t('virtualBackground.deleteImage') }
  415. className = { cx(classes.deleteImageIcon, 'delete-image-icon') }
  416. data-imageid = { image.id }
  417. onClick = { deleteStoredImage }
  418. onKeyPress = { deleteStoredImageKeyPress }
  419. role = 'button'
  420. size = { 16 }
  421. src = { IconCloseLarge }
  422. tabIndex = { 0 } />
  423. </div>
  424. ))}
  425. </div>
  426. </div>
  427. )}
  428. </>
  429. );
  430. }
  431. /**
  432. * Maps (parts of) the redux state to the associated props for the
  433. * {@code VirtualBackground} component.
  434. *
  435. * @param {Object} state - The Redux state.
  436. * @private
  437. * @returns {{Props}}
  438. */
  439. function _mapStateToProps(state: IReduxState) {
  440. const { localFlipX } = state['features/base/settings'];
  441. const dynamicBrandingImages = state['features/dynamic-branding'].virtualBackgrounds;
  442. const hasBrandingImages = Boolean(dynamicBrandingImages.length);
  443. return {
  444. _localFlipX: Boolean(localFlipX),
  445. _images: (hasBrandingImages && dynamicBrandingImages) || IMAGES,
  446. _virtualBackground: state['features/virtual-background'],
  447. _showUploadButton: !state['features/base/config'].disableAddingBackgroundImages,
  448. _multiStreamModeEnabled: getMultipleVideoSendingSupportFeatureFlag(state)
  449. };
  450. }
  451. export default connect(_mapStateToProps)(translate(VirtualBackgrounds));