index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import { styled } from '@mui/material/styles';
  3. import { useState,useRef } from "react";
  4. import { IconButton } from "@material-ui/core";
  5. import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile';
  6. import FileDownloadIcon from '@mui/icons-material/FileDownload';
  7. import Button from '@mui/material/Button';
  8. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  9. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  10. import Menu from '@mui/material/Menu';
  11. import MenuItem from '@mui/material/MenuItem';
  12. import Divider from '@mui/material/Divider';
  13. import CheckBoxIcon from '@mui/icons-material/CheckBox';
  14. import Checkbox from '@mui/material/Checkbox';
  15. import PushPinIcon from '@mui/icons-material/PushPin';
  16. import CloseIcon from '@mui/icons-material/Close';
  17. import ZoomOutIcon from '@mui/icons-material/ZoomOut';
  18. import ZoomInIcon from '@mui/icons-material/ZoomIn';
  19. import ReplyIcon from '@mui/icons-material/Reply';
  20. import { CopyToClipboard } from 'react-copy-to-clipboard';
  21. import { removeMessageById,updateMessageById,pinMessageById } from "../../../../../../api-data";
  22. import { timeStampMessage,copied,emojisArr,slicedWord } from '../../../../../../helpers'
  23. const FileViewer = require('react-file-viewer')
  24. const StyledMenu = styled((props:any) => (
  25. <Menu
  26. elevation={0}
  27. anchorOrigin={{
  28. vertical: 'top',
  29. horizontal: 'right',
  30. }}
  31. transformOrigin={{
  32. vertical: 'bottom',
  33. horizontal: 'right',
  34. }}
  35. {...props}
  36. />
  37. ))(({ theme }:any) => ({
  38. '& .MuiPaper-root': {
  39. borderRadius: 10,
  40. marginTop: theme.spacing(0),
  41. minWidth: 220,
  42. color:
  43. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  44. boxShadow:
  45. 'rgb(255, 255, 255) 0px 0px 0px 0px, rgba(0, 0, 0, 0.05) 0px 0px 0px 1px, rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px',
  46. '& .MuiMenu-list': {
  47. padding: '4px 4px',
  48. },
  49. '& .MuiMenuItem-root': {
  50. marginBottom: theme.spacing(1),
  51. '& .MuiSvgIcon-root': {
  52. fontSize: 21,
  53. color: theme.palette.text.secondary,
  54. marginRight: theme.spacing(2),
  55. }
  56. },
  57. },
  58. }));
  59. const useStyles = makeStyles({
  60. container: {
  61. display: "flex",
  62. justifyContent: "flex-end",
  63. borderRadius: 7,
  64. },
  65. wrapper: {
  66. position: 'relative',
  67. display: 'flex',
  68. justifyContent: 'space-between',
  69. alignContent: 'center',
  70. alignItems: 'center',
  71. width: 200,
  72. padding: '5px 5px 22px 5px',
  73. backgroundColor: '#deffa9',
  74. borderRadius: 7,
  75. "&:after": {
  76. content: "''",
  77. position: "absolute",
  78. width: "0",
  79. height: "0",
  80. borderBottom: "15px solid #deffa9",
  81. borderLeft: "15px solid transparent",
  82. borderRight: "15px solid transparent",
  83. bottom: "0",
  84. right: "-15px"
  85. },
  86. "&:before": {
  87. content: "''",
  88. position: "absolute",
  89. width: "0",
  90. height: "0",
  91. borderBottom: "17px solid #deffa9",
  92. borderLeft: "16px solid transparent",
  93. borderRight: "16px solid transparent",
  94. bottom: "0px",
  95. right: "-17px"
  96. }
  97. },
  98. wrapperActive: {
  99. position: 'relative',
  100. display: 'flex',
  101. justifyContent: 'space-between',
  102. alignContent: 'center',
  103. alignItems: 'center',
  104. width: 200,
  105. padding: '5px 5px 22px 5px',
  106. backgroundColor: '#ced8d7',
  107. borderRadius: 7,
  108. "&:after": {
  109. content: "''",
  110. position: "absolute",
  111. width: "0",
  112. height: "0",
  113. borderBottom: "15px solid #ced8d7",
  114. borderLeft: "15px solid transparent",
  115. borderRight: "15px solid transparent",
  116. bottom: "0",
  117. right: "-15px"
  118. },
  119. "&:before": {
  120. content: "''",
  121. position: "absolute",
  122. width: "0",
  123. height: "0",
  124. borderBottom: "17px solid #ced8d7",
  125. borderLeft: "16px solid transparent",
  126. borderRight: "16px solid transparent",
  127. bottom: "0px",
  128. right: "-17px"
  129. }
  130. },
  131. bntDownload: {
  132. backgroundColor: 'inherit',
  133. color: '#54b0fc',
  134. width: 30,
  135. height:30,
  136. '&:hover': {
  137. backgroundColor: '#54b0fc',
  138. color:'#ffffff'
  139. }
  140. },
  141. title: {
  142. margin: '0 30px 0 5px',
  143. color: '#0e0d0d',
  144. cursor: 'pointer',
  145. '&:hover': {
  146. color: '#54b0fc',
  147. }
  148. },
  149. time: {
  150. position: "absolute",
  151. fontSize: ".65em",
  152. fontWeight:600,
  153. bottom: 0,
  154. right: 6,
  155. color: '#414141',
  156. padding: 3,
  157. borderRadius: 5,
  158. },
  159. captionWrapper: {
  160. position: "absolute",
  161. fontSize: ".85em",
  162. color: '#626262',
  163. maxWidth:'76%',
  164. fontWeight:600,
  165. bottom: -28,
  166. right: 40,
  167. borderRadius: 5,
  168. wordBreak:'break-word',
  169. textAlign: "left",
  170. font: "400 .9em 'Open Sans', sans-serif",
  171. backgroundColor: '#ffffff',
  172. padding:10,
  173. "&:after": {
  174. content: "''",
  175. position: "absolute",
  176. width: "0",
  177. height: "0",
  178. borderBottom: "15px solid #ffffff",
  179. borderLeft: "15px solid transparent",
  180. borderRight: "15px solid transparent",
  181. bottom: "0",
  182. right: "-15px"
  183. },
  184. "&:before": {
  185. content: "''",
  186. position: "absolute",
  187. width: "0",
  188. height: "0",
  189. borderBottom: "17px solid #ffffff",
  190. borderLeft: "16px solid transparent",
  191. borderRight: "16px solid transparent",
  192. bottom: "-1px",
  193. right: "-17px"
  194. }
  195. },
  196. overlay: {
  197. position: 'absolute',
  198. top: 0,
  199. left: 0,
  200. width: '100%',
  201. height:'100%',
  202. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  203. border:'solid 2px #0084ff',
  204. cursor: 'auto',
  205. zIndex: 100,
  206. overflow: 'auto',
  207. },
  208. wrapperIcons: {
  209. position: 'absolute',
  210. top: 14,
  211. right: 30,
  212. display: 'flex',
  213. alignContent: 'center',
  214. alignItems: 'center',
  215. padding: '2px 10px',
  216. borderRadius: 10,
  217. backgroundColor: 'rgba(65, 65, 65, 0.9)',
  218. zIndex:150
  219. },
  220. magnifying : {
  221. cursor: 'pointer',
  222. color: '#e9e7e7',
  223. padding: 0,
  224. marginRight:10,
  225. '&:hover': {
  226. color: '#ffffff',
  227. transform: 'scale(1.1)'
  228. }
  229. },
  230. iconCloseOverlay: {
  231. marginLeft:5,
  232. cursor: 'pointer',
  233. color: '#e9e7e7',
  234. padding: 0,
  235. '&:hover': {
  236. color: '#ffffff',
  237. transform: 'rotate(180deg)',
  238. transition: 'all 250ms ease-out ',
  239. }
  240. },
  241. modalDelete: {
  242. background: '#ffffff',
  243. position: 'absolute',
  244. content:'',
  245. width: '20%',
  246. height:'auto',
  247. left: '40%',
  248. bottom: '48.5%',
  249. borderRadius: 10,
  250. padding: 10,
  251. display: 'flex',
  252. flexDirection:'column'
  253. },
  254. overlayDelete: {
  255. position: 'fixed',
  256. top: 0,
  257. left: 0,
  258. width: '100vw',
  259. height: '100vh',
  260. zIndex: 100,
  261. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  262. overflowY: 'hidden',
  263. },
  264. emojiTitle: {
  265. position: "absolute",
  266. fontSize: "1.7em",
  267. fontWeight:600,
  268. bottom: 0,
  269. left: -40,
  270. },
  271. emojiCompanionTitle: {
  272. position: "absolute",
  273. fontSize: "1.7em",
  274. fontWeight:600,
  275. bottom: '2rem',
  276. left: -40,
  277. },
  278. emoji: {
  279. cursor: 'pointer',
  280. fontSize: '1.7rem',
  281. transition: 'all 0.3s',
  282. '&:hover': {
  283. transform: 'scale(1.5)'
  284. }
  285. },
  286. emojiActive: {
  287. cursor: 'pointer',
  288. fontSize: '1.2rem',
  289. animation: `$emoji 0.6s ease-out`,
  290. animationDirection: 'forwards',
  291. animationIterationCount: 1,
  292. },
  293. '@keyframes emoji': {
  294. '5%': { transform: 'translateY(1rem)'},
  295. '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
  296. '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
  297. '80%': {opacity: 0},
  298. '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  299. },
  300. iconClose: {
  301. '&:hover': {
  302. transform: 'rotate(180deg)',
  303. transition: 'all 250ms ease-out ',
  304. }
  305. },
  306. });
  307. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  308. interface IMessageRightFile {
  309. url:string,
  310. createdAt: string,
  311. type: string,
  312. caption: string,
  313. emoji: string,
  314. emojiCompanion: string,
  315. pinned: boolean,
  316. isSomeSelected: boolean,
  317. isSelected:(_id:string) => boolean,
  318. handleSelected: (_id:string) => void,
  319. _id: string,
  320. nightMode: boolean,
  321. handleReply: (_id: string) => void
  322. }
  323. const MessageRightFile = ({ url,createdAt,type,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,nightMode,handleReply }:IMessageRightFile) => {
  324. const classes = useStyles();
  325. const [read, setRead] = useState<boolean>(false)
  326. const [anchorEl, setAnchorEl] = useState<any>(null);
  327. const [selected, setSelected] = useState<boolean>(false);
  328. const [scale, setScale] = useState<number>(1)
  329. const [modal,setModal] = useState<boolean>(false)
  330. const open = Boolean(anchorEl);
  331. const checked = isSelected(_id)
  332. const refView = useRef<null | any>(null)
  333. const handleIncreaseScale = () => {
  334. if (scale < 5) {
  335. refView.current.style.transform = `scale(${scale+0.25},${scale+0.25})`
  336. refView.current.style.transformOrigin = `${50 / scale + 0.25}px ${50 / scale + 0.25}px`
  337. setScale(scale+0.25)
  338. }
  339. }
  340. const handleDecreaseScale = () => {
  341. if (scale >= 0.5) {
  342. refView.current.style.transform = `scale(${scale-0.25},${scale-0.25})`
  343. refView.current.style.transformOrigin = `${50/scale-0.25}px ${50/scale-0.25}px`
  344. setScale(scale-0.25)
  345. }
  346. }
  347. const handleOpenRead = () => !read&&setRead(true)
  348. const handleCloseRead = (e:any) => {
  349. const id = e.target.id
  350. if (id === 'overlay' || id === 'close') {
  351. setRead(false)
  352. setScale(1)
  353. }
  354. }
  355. const handleClose = (type: string | undefined): void => {
  356. if (type === 'copy') copied('Link')
  357. if (type === 'delete') setModal(true)
  358. setAnchorEl(null)
  359. setSelected(false)
  360. }
  361. const handleDeleteModal = (e: any) => {
  362. const id = e.target.id
  363. if (id === 'overlay' || id === 'cancel') return setModal(false)
  364. if (id === 'delete') {
  365. removeMessageById(_id)
  366. setModal(false)
  367. }
  368. }
  369. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  370. e.preventDefault()
  371. setAnchorEl(e.currentTarget)
  372. setSelected(true)
  373. }
  374. const handleEmojiMenu = ({ target }: any): void => {
  375. const idEmoji = target.id
  376. if (idEmoji === emoji) {updateMessageById(_id,'')
  377. } else updateMessageById(_id,idEmoji)
  378. }
  379. return (
  380. <div className={classes.container} style={{ marginBottom: caption ? 43 : 15 }}>
  381. {read && <div className={classes.wrapperIcons}>
  382. <ZoomOutIcon onClick={handleDecreaseScale} className={classes.magnifying} fontSize='large' />
  383. <ZoomInIcon onClick={handleIncreaseScale} className={classes.magnifying} fontSize='large' />
  384. <CloseIcon id='close' onClick={handleCloseRead} className={classes.iconCloseOverlay} fontSize='large' />
  385. </div>}
  386. {read && <div className={classes.overlay} id='overlay'
  387. onClick={handleCloseRead}>
  388. <div ref={refView}>
  389. <FileViewer
  390. allowFullScreen={true}
  391. fileType={type}
  392. filePath={url}
  393. onError={handleCloseRead}
  394. />
  395. </div>
  396. </div>}
  397. <div onContextMenu={(e) => handleContextMenu(e)}
  398. className={selected ? classes.wrapperActive : classes.wrapper}
  399. style={{pointerEvents:isSomeSelected?'none':'auto'}}>
  400. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  401. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  402. <a href={url} target="_blank" rel="noreferrer" download>
  403. <IconButton className={classes.bntDownload}>
  404. <FileDownloadIcon fontSize='medium'/>
  405. </IconButton>
  406. </a>
  407. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  408. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  409. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  410. {caption&&<div className={classes.captionWrapper}>{slicedWord(caption, 20, 0)}</div>}
  411. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  412. anchorEl={anchorEl} open={open} onClose={handleClose}>
  413. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  414. {emojisArr.map((el:string, i:number) =>
  415. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  416. </MenuItem>
  417. <Divider />
  418. <MenuItem onClick={() => {
  419. handleReply(_id)
  420. handleClose(undefined)
  421. }}>
  422. <ReplyIcon />
  423. Reply
  424. </MenuItem>
  425. <MenuItem>
  426. <ReplyIcon style={{transform :'rotateY(180deg)'}} />
  427. Forward
  428. </MenuItem>
  429. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  430. <MenuItem>
  431. <ContentCopyIcon />
  432. Copy Link
  433. </MenuItem>
  434. </CopyToClipboard>
  435. <MenuItem onClick={() => {
  436. pinMessageById(_id, !pinned)
  437. handleClose(undefined)
  438. }}>
  439. {pinned ?
  440. <CloseIcon className={classes.iconClose} /> :
  441. <PushPinIcon />}
  442. {pinned?'Unpin':'Pin'}
  443. </MenuItem>
  444. <MenuItem onClick={() => {
  445. handleSelected(_id)
  446. handleClose(undefined)
  447. }}>
  448. <CheckBoxIcon />
  449. Select
  450. </MenuItem>
  451. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  452. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  453. Delete
  454. </MenuItem>
  455. </StyledMenu>
  456. {modal &&
  457. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  458. <div className={classes.modalDelete}>
  459. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  460. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  461. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  462. DELETE MESSAGE
  463. </Button>
  464. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  465. CANCEL
  466. </Button>
  467. </div>
  468. </div>}
  469. </div>
  470. {isSomeSelected&&<Checkbox {...label} checked={checked} sx={{color:nightMode?'#ffffff':'#00ff48','&.Mui-checked': {color: nightMode?'#ffffff':'#00ff48'}}} onClick={() => handleSelected(_id)}/>}
  471. </div>
  472. )};
  473. export default MessageRightFile