index.tsx 17 KB

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