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 10.0KB

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