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.

FileSharing.tsx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import React, { useCallback, useState, useRef } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector, useStore } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState } from '../../../app/types';
  6. import Avatar from '../../../base/avatar/components/Avatar';
  7. import { IconCloudUpload, IconDownload, IconTrash } from '../../../base/icons/svg';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import { BUTTON_TYPES } from '../../../base/ui/constants.web';
  10. import Icon from '../../../base/icons/components/Icon';
  11. import { isLocalParticipantModerator } from '../../../base/participants/functions';
  12. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  13. import BaseTheme from '../../../base/ui/components/BaseTheme.web';
  14. import { downloadFile, removeFile } from '../../actions';
  15. import { formatFileSize, formatTimestamp, getFileIcon, processFiles } from '../../functions.any';
  16. const useStyles = makeStyles()(theme => {
  17. return {
  18. buttonContainer: {
  19. alignItems: 'center',
  20. display: 'flex',
  21. justifyContent: 'end',
  22. gap: theme.spacing(2),
  23. position: 'absolute',
  24. top: 0,
  25. right: theme.spacing(3),
  26. bottom: 0,
  27. left: 0
  28. },
  29. container: {
  30. boxSizing: 'border-box',
  31. display: 'flex',
  32. flexDirection: 'column',
  33. height: '100%',
  34. margin: '0 auto',
  35. maxWidth: '600px',
  36. padding: theme.spacing(3),
  37. position: 'relative',
  38. width: '100%'
  39. },
  40. dropZone: {
  41. backgroundColor: theme.palette.ui02,
  42. border: `2px dashed ${theme.palette.ui03}`,
  43. borderRadius: theme.shape.borderRadius,
  44. bottom: 0,
  45. left: 0,
  46. opacity: 0,
  47. position: 'absolute',
  48. right: 0,
  49. top: 0,
  50. zIndex: 0,
  51. '&.dragging': {
  52. backgroundColor: theme.palette.ui03,
  53. borderColor: theme.palette.action01,
  54. opacity: 0.8,
  55. zIndex: 2
  56. }
  57. },
  58. fileIconContainer: {
  59. display: 'flex',
  60. margin: 'auto'
  61. },
  62. fileItem: {
  63. backgroundColor: theme.palette.ui02,
  64. borderRadius: theme.shape.borderRadius,
  65. display: 'flex',
  66. flexDirection: 'row',
  67. gap: theme.spacing(3),
  68. justifyContent: 'space-between',
  69. padding: theme.spacing(3),
  70. position: 'relative',
  71. '&:hover': {
  72. backgroundColor: theme.palette.ui03,
  73. borderRadius: theme.shape.borderRadius,
  74. '& .actionIconVisibility': {
  75. visibility: 'visible'
  76. },
  77. '& .timestampVisibility': {
  78. visibility: 'hidden'
  79. }
  80. }
  81. },
  82. fileItemDetails: {
  83. display: 'flex',
  84. flexDirection: 'column',
  85. flexGrow: 2,
  86. gap: theme.spacing(1),
  87. justifyContent: 'center',
  88. minWidth: 0
  89. },
  90. fileList: {
  91. display: 'flex',
  92. flex: 1,
  93. flexDirection: 'column',
  94. gap: theme.spacing(2),
  95. gridTemplateColumns: 'repeat(auto-fill, minmax(200px, 1fr))',
  96. marginBottom: theme.spacing(8),
  97. overflowY: 'auto',
  98. zIndex: 1
  99. },
  100. fileName: {
  101. ...theme.typography.labelBold,
  102. gap: theme.spacing(1),
  103. overflow: 'hidden',
  104. textOverflow: 'ellipsis',
  105. whiteSpace: 'nowrap'
  106. },
  107. fileAuthorParticipant: {
  108. alignItems: 'center',
  109. display: 'inline-flex',
  110. gap: theme.spacing(1)
  111. },
  112. fileAuthorParticipantName: {
  113. ...theme.typography.labelBold,
  114. overflow: 'hidden',
  115. textOverflow: 'ellipsis',
  116. whiteSpace: 'nowrap'
  117. },
  118. fileSize: {
  119. ...theme.typography.labelRegular
  120. },
  121. fileTimestamp: {
  122. ...theme.typography.labelRegular,
  123. display: 'flex',
  124. lineHeight: '1.2rem',
  125. marginTop: theme.spacing(1),
  126. textAlign: 'center',
  127. },
  128. hiddenInput: {
  129. visibility: 'hidden'
  130. },
  131. noFilesContainer: {
  132. display: 'flex',
  133. flexDirection: 'column',
  134. height: '88%',
  135. justifyContent: 'center',
  136. textAlign: 'center'
  137. },
  138. noFilesText: {
  139. ...withPixelLineHeight(theme.typography.bodyLongBold),
  140. color: theme.palette.text02,
  141. padding: '0 24px',
  142. textAlign: 'center'
  143. },
  144. progressBar: {
  145. backgroundColor: theme.palette.ui03,
  146. borderRadius: theme.shape.borderRadius,
  147. height: 4,
  148. overflow: 'hidden',
  149. width: '100%'
  150. },
  151. progressFill: {
  152. backgroundColor: theme.palette.action01,
  153. height: '100%',
  154. transition: 'width 0.3s ease'
  155. },
  156. uploadButton: {
  157. bottom: theme.spacing(4),
  158. cursor: 'pointer',
  159. left: '50%',
  160. position: 'absolute',
  161. transform: 'translateX(-50%)',
  162. width: '85%',
  163. zIndex: 1
  164. },
  165. uploadIcon: {
  166. margin: '0 auto'
  167. },
  168. actionIcon: {
  169. cursor: 'pointer',
  170. padding: theme.spacing(1),
  171. visibility: 'hidden'
  172. }
  173. };
  174. });
  175. const FileSharing = () => {
  176. const { classes } = useStyles();
  177. const [ isDragging, setIsDragging ] = useState(false);
  178. const fileInputRef = useRef<HTMLInputElement>(null);
  179. const { t } = useTranslation();
  180. const dispatch = useDispatch();
  181. const store = useStore();
  182. const { files } = useSelector((state: IReduxState) => state['features/file-sharing']);
  183. const sortedFiles = Array.from(files.values()).sort((a, b) => a.fileName.localeCompare(b.fileName));
  184. const isModerator = useSelector(isLocalParticipantModerator);
  185. const handleDragEnter = useCallback((e: React.DragEvent) => {
  186. e.preventDefault();
  187. e.stopPropagation();
  188. setIsDragging(true);
  189. }, []);
  190. const handleDragLeave = useCallback((e: React.DragEvent) => {
  191. e.preventDefault();
  192. e.stopPropagation();
  193. setIsDragging(false);
  194. }, []);
  195. const handleDragOver = useCallback((e: React.DragEvent) => {
  196. e.preventDefault();
  197. e.stopPropagation();
  198. }, []);
  199. const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  200. if (e.target.files) {
  201. processFiles(e.target.files as FileList, store);
  202. }
  203. }, [ processFiles ]);
  204. const handleDrop = useCallback((e: React.DragEvent) => {
  205. e.preventDefault();
  206. e.stopPropagation();
  207. setIsDragging(false);
  208. if (e.dataTransfer.files?.length > 0) {
  209. processFiles(e.dataTransfer.files as FileList, store);
  210. }
  211. }, [ processFiles ]);
  212. const handleClick = useCallback(() => {
  213. fileInputRef.current?.click();
  214. }, []);
  215. const handleKeyPress = useCallback((e: React.KeyboardEvent) => {
  216. if (e.key === 'Enter' || e.key === ' ') {
  217. fileInputRef.current?.click();
  218. }
  219. }, []);
  220. return (
  221. <div className = { classes.container }>
  222. {
  223. isModerator && (
  224. <>
  225. <div
  226. className = { `${classes.dropZone} ${
  227. isDragging ? 'dragging' : ''
  228. }` }
  229. onDragEnter = { handleDragEnter }
  230. onDragLeave = { handleDragLeave }
  231. onDragOver = { handleDragOver }
  232. onDrop = { handleDrop }
  233. role = 'button'
  234. tabIndex = { 0 }>
  235. <input
  236. className = { classes.hiddenInput }
  237. multiple = { true }
  238. onChange = { handleFileSelect }
  239. ref = { fileInputRef }
  240. type = 'file' />
  241. </div>
  242. {
  243. sortedFiles.length === 0 && (
  244. <div className = { classes.noFilesContainer }>
  245. <Icon
  246. className = { classes.uploadIcon }
  247. color = { BaseTheme.palette.icon03 }
  248. size = { 160 }
  249. src = { IconCloudUpload } />
  250. <span className = { classes.noFilesText }>
  251. { t('fileSharing.dragAndDrop') }
  252. </span>
  253. </div>
  254. )
  255. }
  256. </>
  257. )
  258. }
  259. {
  260. sortedFiles.length > 0 && (
  261. <div className = { classes.fileList }>
  262. {
  263. sortedFiles.map(file => (
  264. <div
  265. className = { classes.fileItem }
  266. key = { file.fileId }
  267. title = { file.fileName }>
  268. {
  269. (file.progress ?? 100) === 100 && (
  270. <>
  271. <div className = { classes.fileIconContainer }>
  272. <Icon
  273. color = { BaseTheme.palette.icon01 }
  274. size = { 64 }
  275. src = { getFileIcon(file.fileType) } />
  276. </div>
  277. <div className = { classes.fileItemDetails }>
  278. <div className = { classes.fileName }>
  279. { file.fileName }
  280. </div>
  281. <div className = { classes.fileSize }>
  282. { formatFileSize(file.fileSize) }
  283. </div>
  284. <div className = { classes.fileAuthorParticipant }>
  285. <Avatar
  286. displayName = { file.authorParticipantName }
  287. participantId = { file.authorParticipantId }
  288. size = { 16 } />
  289. <div className = { classes.fileAuthorParticipantName }>
  290. { file.authorParticipantName }
  291. </div>
  292. </div>
  293. </div>
  294. <div className = { `${classes.fileTimestamp} timestampVisibility` }>
  295. <pre>
  296. { formatTimestamp(file.timestamp) }
  297. </pre>
  298. </div>
  299. <div className = { classes.buttonContainer }>
  300. <Icon
  301. className = { `${classes.actionIcon} actionIconVisibility` }
  302. color = { BaseTheme.palette.icon01 }
  303. // eslint-disable-next-line react/jsx-no-bind
  304. onClick = { () => dispatch(downloadFile(file.fileId)) }
  305. size = { 24 }
  306. src = { IconDownload } />
  307. {
  308. isModerator && (
  309. <Icon
  310. className = { `${classes.actionIcon} actionIconVisibility` }
  311. color = { BaseTheme.palette.icon01 }
  312. // eslint-disable-next-line react/jsx-no-bind
  313. onClick = { () => dispatch(removeFile(file.fileId)) }
  314. size = { 24 }
  315. src = { IconTrash } />
  316. )
  317. }
  318. </div>
  319. </>
  320. )
  321. }
  322. {
  323. (file.progress ?? 100) < 100 && (
  324. <div className = { classes.progressBar }>
  325. <div
  326. className = { classes.progressFill }
  327. style = {{ width: `${file.progress}%` }} />
  328. </div>
  329. )
  330. }
  331. </div>
  332. ))
  333. }
  334. </div>
  335. )
  336. }
  337. {
  338. isModerator && (
  339. <Button
  340. accessibilityLabel = { t('fileSharing.uploadFile') }
  341. className = { classes.uploadButton }
  342. labelKey = { 'fileSharing.uploadFile' }
  343. onClick = { handleClick }
  344. onKeyPress = { handleKeyPress }
  345. type = { BUTTON_TYPES.PRIMARY } />
  346. )
  347. }
  348. </div>
  349. );
  350. };
  351. export default FileSharing;