index.tsx 16 KB

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