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.

VirtualBackgroundDialog.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // @flow
  2. /* eslint-disable react/jsx-no-bind, no-return-assign */
  3. import Spinner from '@atlaskit/spinner';
  4. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  5. import React, { useState, useEffect } from 'react';
  6. import uuid from 'uuid';
  7. import { Dialog, hideDialog } from '../../base/dialog';
  8. import { translate } from '../../base/i18n';
  9. import { Icon, IconCloseSmall, IconPlusCircle, IconShareDesktop } from '../../base/icons';
  10. import { createLocalTrack } from '../../base/lib-jitsi-meet/functions';
  11. import { connect } from '../../base/redux';
  12. import { getLocalVideoTrack } from '../../base/tracks';
  13. import { toggleBackgroundEffect } from '../actions';
  14. import { resizeImage, toDataURL } from '../functions';
  15. import logger from '../logger';
  16. import VirtualBackgroundPreview from './VirtualBackgroundPreview';
  17. // The limit of virtual background uploads is 24. When the number
  18. // of uploads is 25 we trigger the deleteStoredImage function to delete
  19. // the first/oldest uploaded background.
  20. const backgroundsLimit = 25;
  21. const images = [
  22. {
  23. id: '1',
  24. src: 'images/virtual-background/background-1.jpg'
  25. },
  26. {
  27. id: '2',
  28. src: 'images/virtual-background/background-2.jpg'
  29. },
  30. {
  31. id: '3',
  32. src: 'images/virtual-background/background-3.jpg'
  33. },
  34. {
  35. id: '4',
  36. src: 'images/virtual-background/background-4.jpg'
  37. },
  38. {
  39. id: '5',
  40. src: 'images/virtual-background/background-5.jpg'
  41. },
  42. {
  43. id: '6',
  44. src: 'images/virtual-background/background-6.jpg'
  45. },
  46. {
  47. id: '7',
  48. src: 'images/virtual-background/background-7.jpg'
  49. }
  50. ];
  51. type Props = {
  52. /**
  53. * Returns the jitsi track that will have backgraund effect applied.
  54. */
  55. _jitsiTrack: Object,
  56. /**
  57. * Returns the selected thumbnail identifier.
  58. */
  59. _selectedThumbnail: string,
  60. /**
  61. * Returns the selected virtual source object.
  62. */
  63. _virtualSource: Object,
  64. /**
  65. * The redux {@code dispatch} function.
  66. */
  67. dispatch: Function,
  68. /**
  69. * Invoked to obtain translated strings.
  70. */
  71. t: Function
  72. };
  73. /**
  74. * Renders virtual background dialog.
  75. *
  76. * @returns {ReactElement}
  77. */
  78. function VirtualBackground({ _jitsiTrack, _selectedThumbnail, _virtualSource, dispatch, t }: Props) {
  79. const [ options, setOptions ] = useState({});
  80. const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
  81. const [ storedImages, setStoredImages ] = useState((localImages && JSON.parse(localImages)) || []);
  82. const [ loading, isloading ] = useState(false);
  83. const [ activeDesktopVideo ] = useState(_virtualSource?.videoType === 'desktop' ? _virtualSource : null);
  84. const deleteStoredImage = image => {
  85. setStoredImages(storedImages.filter(item => item !== image));
  86. };
  87. /**
  88. * Updates stored images on local storage.
  89. */
  90. useEffect(() => {
  91. try {
  92. jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
  93. } catch (err) {
  94. // Preventing localStorage QUOTA_EXCEEDED_ERR
  95. err && deleteStoredImage(storedImages[0]);
  96. }
  97. if (storedImages.length === backgroundsLimit) {
  98. deleteStoredImage(storedImages[0]);
  99. }
  100. }, [ storedImages ]);
  101. const enableBlur = async (blurValue, selection) => {
  102. setOptions({
  103. backgroundType: 'blur',
  104. enabled: true,
  105. blurValue,
  106. selectedThumbnail: selection
  107. });
  108. };
  109. const removeBackground = async () => {
  110. setOptions({
  111. enabled: false,
  112. selectedThumbnail: 'none'
  113. });
  114. };
  115. const shareDesktop = async selection => {
  116. const url = await createLocalTrack('desktop', '');
  117. setOptions({
  118. backgroundType: 'desktop-share',
  119. enabled: true,
  120. selectedThumbnail: selection,
  121. url
  122. });
  123. };
  124. const setUploadedImageBackground = async image => {
  125. setOptions({
  126. backgroundType: 'image',
  127. enabled: true,
  128. url: image.src,
  129. selectedThumbnail: image.id
  130. });
  131. };
  132. const setImageBackground = async image => {
  133. const url = await toDataURL(image.src);
  134. setOptions({
  135. backgroundType: 'image',
  136. enabled: true,
  137. url,
  138. selectedThumbnail: image.id
  139. });
  140. };
  141. const uploadImage = async imageFile => {
  142. const reader = new FileReader();
  143. reader.readAsDataURL(imageFile[0]);
  144. reader.onload = async () => {
  145. const url = await resizeImage(reader.result);
  146. const uuId = uuid.v4();
  147. setStoredImages([
  148. ...storedImages,
  149. {
  150. id: uuId,
  151. src: url
  152. }
  153. ]);
  154. setOptions({
  155. backgroundType: 'image',
  156. enabled: true,
  157. url,
  158. selectedThumbnail: uuId
  159. });
  160. };
  161. reader.onerror = () => {
  162. isloading(false);
  163. logger.error('Failed to upload virtual image!');
  164. };
  165. };
  166. const applyVirtualBackground = async () => {
  167. if (activeDesktopVideo) {
  168. await activeDesktopVideo.dispose();
  169. }
  170. isloading(true);
  171. await dispatch(toggleBackgroundEffect(options, _jitsiTrack));
  172. await isloading(false);
  173. dispatch(hideDialog());
  174. };
  175. return (
  176. <Dialog
  177. hideCancelButton = { false }
  178. okKey = { 'virtualBackground.apply' }
  179. onSubmit = { applyVirtualBackground }
  180. submitDisabled = { !options || loading }
  181. titleKey = { 'virtualBackground.title' } >
  182. <VirtualBackgroundPreview options = { options } />
  183. {loading ? (
  184. <div className = 'virtual-background-loading'>
  185. <Spinner
  186. isCompleting = { false }
  187. size = 'medium' />
  188. </div>
  189. ) : (
  190. <div>
  191. <label
  192. className = 'file-upload-label'
  193. htmlFor = 'file-upload'>
  194. <Icon
  195. className = { 'add-background' }
  196. size = { 20 }
  197. src = { IconPlusCircle } />
  198. {t('virtualBackground.addBackground')}
  199. </label>
  200. <input
  201. accept = 'image/*'
  202. className = 'file-upload-btn'
  203. id = 'file-upload'
  204. onChange = { e => uploadImage(e.target.files) }
  205. type = 'file' />
  206. <div className = 'virtual-background-dialog'>
  207. <div
  208. className = { _selectedThumbnail === 'none' ? 'none-selected' : 'virtual-background-none' }
  209. onClick = { removeBackground }>
  210. {t('virtualBackground.none')}
  211. </div>
  212. <div
  213. className = { _selectedThumbnail === 'slight-blur'
  214. ? 'slight-blur-selected' : 'slight-blur' }
  215. onClick = { () => enableBlur(8, 'slight-blur') }>
  216. {t('virtualBackground.slightBlur')}
  217. </div>
  218. <div
  219. className = { _selectedThumbnail === 'blur' ? 'blur-selected' : 'blur' }
  220. onClick = { () => enableBlur(25, 'blur') }>
  221. {t('virtualBackground.blur')}
  222. </div>
  223. <div
  224. className = { _selectedThumbnail === 'desktop-share'
  225. ? 'desktop-share-selected'
  226. : 'desktop-share' }
  227. onClick = { () => shareDesktop('desktop-share') }>
  228. <Icon
  229. className = 'share-desktop-icon'
  230. size = { 30 }
  231. src = { IconShareDesktop } />
  232. </div>
  233. {images.map((image, index) => (
  234. <img
  235. className = {
  236. options.selectedThumbnail === image.id || _selectedThumbnail === image.id
  237. ? 'thumbnail-selected'
  238. : 'thumbnail'
  239. }
  240. key = { index }
  241. onClick = { () => setImageBackground(image) }
  242. onError = { event => event.target.style.display = 'none' }
  243. src = { image.src } />
  244. ))}
  245. {storedImages.map((image, index) => (
  246. <div
  247. className = { 'thumbnail-container' }
  248. key = { index }>
  249. <img
  250. className = { _selectedThumbnail === image.id ? 'thumbnail-selected' : 'thumbnail' }
  251. onClick = { () => setUploadedImageBackground(image) }
  252. onError = { event => event.target.style.display = 'none' }
  253. src = { image.src } />
  254. <Icon
  255. className = { 'delete-image-icon' }
  256. onClick = { () => deleteStoredImage(image) }
  257. size = { 15 }
  258. src = { IconCloseSmall } />
  259. </div>
  260. ))}
  261. </div>
  262. </div>
  263. )}
  264. </Dialog>
  265. );
  266. }
  267. /**
  268. * Maps (parts of) the redux state to the associated props for the
  269. * {@code VirtualBackground} component.
  270. *
  271. * @param {Object} state - The Redux state.
  272. * @private
  273. * @returns {{Props}}
  274. */
  275. function _mapStateToProps(state): Object {
  276. return {
  277. _virtualSource: state['features/virtual-background'].virtualSource,
  278. _selectedThumbnail: state['features/virtual-background'].selectedThumbnail,
  279. _jitsiTrack: getLocalVideoTrack(state['features/base/tracks'])?.jitsiTrack
  280. };
  281. }
  282. export default translate(connect(_mapStateToProps)(VirtualBackground));