index.tsx 14 KB

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