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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * The redux {@code dispatch} function.
  62. */
  63. dispatch: Function,
  64. /**
  65. * Invoked to obtain translated strings.
  66. */
  67. t: Function
  68. };
  69. /**
  70. * Renders virtual background dialog.
  71. *
  72. * @returns {ReactElement}
  73. */
  74. function VirtualBackground({ _jitsiTrack, _selectedThumbnail, dispatch, t }: Props) {
  75. const [ options, setOptions ] = useState({});
  76. const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
  77. const [ storedImages, setStoredImages ] = useState((localImages && JSON.parse(localImages)) || []);
  78. const [ loading, isloading ] = useState(false);
  79. const deleteStoredImage = image => {
  80. setStoredImages(storedImages.filter(item => item !== image));
  81. };
  82. /**
  83. * Updates stored images on local storage.
  84. */
  85. useEffect(() => {
  86. try {
  87. jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
  88. } catch (err) {
  89. // Preventing localStorage QUOTA_EXCEEDED_ERR
  90. err && deleteStoredImage(storedImages[0]);
  91. }
  92. if (storedImages.length === backgroundsLimit) {
  93. deleteStoredImage(storedImages[0]);
  94. }
  95. }, [ storedImages ]);
  96. const enableBlur = async (blurValue, selection) => {
  97. setOptions({
  98. backgroundType: 'blur',
  99. enabled: true,
  100. blurValue,
  101. selectedThumbnail: selection
  102. });
  103. };
  104. const removeBackground = async () => {
  105. setOptions({
  106. enabled: false,
  107. selectedThumbnail: 'none'
  108. });
  109. };
  110. const shareDesktop = async selection => {
  111. const url = await createLocalTrack('desktop', '');
  112. setOptions({
  113. backgroundType: 'desktop-share',
  114. enabled: true,
  115. selectedThumbnail: selection,
  116. url
  117. });
  118. };
  119. const setUploadedImageBackground = async image => {
  120. setOptions({
  121. backgroundType: 'image',
  122. enabled: true,
  123. url: image.src,
  124. selectedThumbnail: image.id
  125. });
  126. };
  127. const setImageBackground = async image => {
  128. const url = await toDataURL(image.src);
  129. setOptions({
  130. backgroundType: 'image',
  131. enabled: true,
  132. url,
  133. selectedThumbnail: image.id
  134. });
  135. };
  136. const uploadImage = async imageFile => {
  137. const reader = new FileReader();
  138. reader.readAsDataURL(imageFile[0]);
  139. reader.onload = async () => {
  140. const url = await resizeImage(reader.result);
  141. const uuId = uuid.v4();
  142. setStoredImages([
  143. ...storedImages,
  144. {
  145. id: uuId,
  146. src: url
  147. }
  148. ]);
  149. setOptions({
  150. backgroundType: 'image',
  151. enabled: true,
  152. url,
  153. selectedThumbnail: uuId
  154. });
  155. };
  156. reader.onerror = () => {
  157. isloading(false);
  158. logger.error('Failed to upload virtual image!');
  159. };
  160. };
  161. const applyVirtualBackground = async () => {
  162. isloading(true);
  163. await dispatch(toggleBackgroundEffect(options, _jitsiTrack));
  164. await isloading(false);
  165. dispatch(hideDialog());
  166. };
  167. return (
  168. <Dialog
  169. hideCancelButton = { false }
  170. okKey = { 'virtualBackground.apply' }
  171. onSubmit = { applyVirtualBackground }
  172. submitDisabled = { !options || loading }
  173. titleKey = { 'virtualBackground.title' } >
  174. <VirtualBackgroundPreview options = { options } />
  175. {loading ? (
  176. <div className = 'virtual-background-loading'>
  177. <Spinner
  178. isCompleting = { false }
  179. size = 'medium' />
  180. </div>
  181. ) : (
  182. <div>
  183. <label
  184. className = 'file-upload-label'
  185. htmlFor = 'file-upload'>
  186. <Icon
  187. className = { 'add-background' }
  188. size = { 20 }
  189. src = { IconPlusCircle } />
  190. {t('virtualBackground.addBackground')}
  191. </label>
  192. <input
  193. accept = 'image/*'
  194. className = 'file-upload-btn'
  195. id = 'file-upload'
  196. onChange = { e => uploadImage(e.target.files) }
  197. type = 'file' />
  198. <div className = 'virtual-background-dialog'>
  199. <div
  200. className = { _selectedThumbnail === 'none' ? 'none-selected' : 'virtual-background-none' }
  201. onClick = { removeBackground }>
  202. {t('virtualBackground.none')}
  203. </div>
  204. <div
  205. className = { _selectedThumbnail === 'slight-blur'
  206. ? 'slight-blur-selected' : 'slight-blur' }
  207. onClick = { () => enableBlur(8, 'slight-blur') }>
  208. {t('virtualBackground.slightBlur')}
  209. </div>
  210. <div
  211. className = { _selectedThumbnail === 'blur' ? 'blur-selected' : 'blur' }
  212. onClick = { () => enableBlur(25, 'blur') }>
  213. {t('virtualBackground.blur')}
  214. </div>
  215. <div
  216. className = { _selectedThumbnail === 'desktop-share'
  217. ? 'desktop-share-selected'
  218. : 'desktop-share' }
  219. onClick = { () => shareDesktop('desktop-share') }>
  220. <Icon
  221. className = 'share-desktop-icon'
  222. size = { 30 }
  223. src = { IconShareDesktop } />
  224. </div>
  225. {images.map((image, index) => (
  226. <img
  227. className = {
  228. options.selectedThumbnail === image.id || _selectedThumbnail === image.id
  229. ? 'thumbnail-selected'
  230. : 'thumbnail'
  231. }
  232. key = { index }
  233. onClick = { () => setImageBackground(image) }
  234. onError = { event => event.target.style.display = 'none' }
  235. src = { image.src } />
  236. ))}
  237. {storedImages.map((image, index) => (
  238. <div
  239. className = { 'thumbnail-container' }
  240. key = { index }>
  241. <img
  242. className = { _selectedThumbnail === image.id ? 'thumbnail-selected' : 'thumbnail' }
  243. onClick = { () => setUploadedImageBackground(image) }
  244. onError = { event => event.target.style.display = 'none' }
  245. src = { image.src } />
  246. <Icon
  247. className = { 'delete-image-icon' }
  248. onClick = { () => deleteStoredImage(image) }
  249. size = { 15 }
  250. src = { IconCloseSmall } />
  251. </div>
  252. ))}
  253. </div>
  254. </div>
  255. )}
  256. </Dialog>
  257. );
  258. }
  259. /**
  260. * Maps (parts of) the redux state to the associated props for the
  261. * {@code VirtualBackground} component.
  262. *
  263. * @param {Object} state - The Redux state.
  264. * @private
  265. * @returns {{Props}}
  266. */
  267. function _mapStateToProps(state): Object {
  268. return {
  269. _selectedThumbnail: state['features/virtual-background'].selectedThumbnail,
  270. _jitsiTrack: getLocalVideoTrack(state['features/base/tracks'])?.jitsiTrack
  271. };
  272. }
  273. export default translate(connect(_mapStateToProps)(VirtualBackground));