index.tsx 15 KB

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