index.tsx 14 KB

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