index.tsx 14 KB

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