index.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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-start',
  66. alignContent: 'flex-start',
  67. flexDirection:'column',
  68. borderRadius: 7,
  69. position: 'relative',
  70. marginBottom: 5,
  71. padding:'10px 0px 10px 22px'
  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: '#ffffff',
  83. "&:after": {
  84. content: "''",
  85. position: "absolute",
  86. width: "0",
  87. height: "0",
  88. borderBottom: "15px solid #ffffff",
  89. borderLeft: "15px solid transparent",
  90. borderRight: "15px solid transparent",
  91. bottom: '0px',
  92. left: "-15px"
  93. },
  94. "&:before": {
  95. content: "''",
  96. position: "absolute",
  97. width: "0",
  98. height: "0",
  99. borderBottom: "17px solid #ffffff",
  100. borderLeft: "16px solid transparent",
  101. borderRight: "16px solid transparent",
  102. bottom: "0px",
  103. left: "-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: '#babdbc',
  116. "&:after": {
  117. content: "''",
  118. position: "absolute",
  119. width: "0",
  120. height: "0",
  121. borderBottom: "15px solid #babdbc",
  122. borderLeft: "15px solid transparent",
  123. borderRight: "15px solid transparent",
  124. bottom: '0px',
  125. left: "-15px"
  126. },
  127. "&:before": {
  128. content: "''",
  129. position: "absolute",
  130. width: "0",
  131. height: "0",
  132. borderBottom: "17px solid #babdbc",
  133. borderLeft: "16px solid transparent",
  134. borderRight: "16px solid transparent",
  135. bottom: "0px",
  136. left: "-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: '#414141',
  159. maxWidth: 430,
  160. fontWeight:600,
  161. borderRadius: 5,
  162. marginLeft:10,
  163. wordBreak:'break-word',
  164. textAlign: "left",
  165. font: "400 .9em 'Open Sans', sans-serif",
  166. backgroundColor: '#deffa9',
  167. padding:10,
  168. "&:after": {
  169. content: "''",
  170. position: "absolute",
  171. width: "0",
  172. height: "0",
  173. borderBottom: "15px solid #deffa9",
  174. borderLeft: "15px solid transparent",
  175. borderRight: "15px solid transparent",
  176. bottom: "0px",
  177. left: "-15px"
  178. },
  179. "&:before": {
  180. content: "''",
  181. position: "absolute",
  182. width: "0",
  183. height: "0",
  184. borderBottom: "17px solid #deffa9",
  185. borderLeft: "16px solid transparent",
  186. borderRight: "16px solid transparent",
  187. bottom: "0px",
  188. left: "-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. right: -40,
  311. },
  312. emojiCompanionTitle: {
  313. position: "absolute",
  314. fontSize: "1.7em",
  315. fontWeight:600,
  316. bottom: '2.2rem',
  317. right: -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. left: -64,
  350. bottom: -10,
  351. pointerEvents: 'auto'
  352. }
  353. });
  354. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  355. interface IMessagesLeftImage {
  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. handleForward: (_id: string) => void,
  373. }
  374. const MessagesLeftImage = ({url,createdAt,color,fullType,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,name,lastName,nightMode,handleReply,handleForward}:IMessagesLeftImage) => {
  375. const classes = useStyles();
  376. const [watch, setWatch] = useState<boolean>(false)
  377. const [anchorEl, setAnchorEl] = useState<any>(null);
  378. const [selected, setSelected] = useState<boolean>(false);
  379. const [scale, setScale] = useState<number>(1)
  380. const refView = useRef<null | any>(null)
  381. const [modal,setModal] = useState<boolean>(false)
  382. const open = Boolean(anchorEl);
  383. const checked = isSelected(_id)
  384. const handleIncreaseScale = () => {
  385. if (scale < 5) {
  386. refView.current.style.transform = `scale(${scale+0.25},${scale+0.25})`
  387. refView.current.style.transformOrigin = `${50 / scale + 0.25}px ${50 / scale + 0.25}px`
  388. setScale(scale+0.25)
  389. }
  390. }
  391. const handleDecreaseScale = () => {
  392. if (scale >= 0.5) {
  393. refView.current.style.transform = `scale(${scale-0.25},${scale-0.25})`
  394. refView.current.style.transformOrigin = `${50/scale-0.25}px ${50/scale-0.25}px`
  395. setScale(scale-0.25)
  396. }
  397. }
  398. const handleOpenWatch = () => !watch&&setWatch(true)
  399. const handleCloseWatch = (e:any) => {
  400. const id = e.target.id
  401. if (id === 'overlay' || id === 'close') {
  402. setWatch(false)
  403. refView.current.style.transform = 'scale(1)'
  404. setScale(1)
  405. }
  406. }
  407. const handleClose = (type: string | undefined): void => {
  408. if (type === 'copy') copied('Link')
  409. if (type === 'delete') setModal(true)
  410. setAnchorEl(null)
  411. setSelected(false)
  412. }
  413. const handleDeleteModal = (e: any) => {
  414. const id = e.target.id
  415. if (id === 'overlay' || id === 'cancel') return setModal(false)
  416. if (id === 'delete') {
  417. removeMessageById(_id)
  418. setModal(false)
  419. }
  420. }
  421. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  422. e.preventDefault()
  423. setAnchorEl(e.currentTarget)
  424. setSelected(true)
  425. }
  426. const handleEmojiMenu = ({ target }: any): void => {
  427. const idEmoji = target.id
  428. if (idEmoji === emoji) {updateMessageById(_id,'')
  429. } else updateMessageById(_id,idEmoji)
  430. }
  431. return (watch ?
  432. <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
  433. <div className={classes.topBar}>
  434. <div className={classes.wrapperCredentials}>
  435. <ListItemIcon >
  436. <Avatar alt={name} src={url?url:undefined}
  437. sx={{ background: color, width: 44, height: 44 }}>
  438. {!url&&`${firstLetter(name)}${firstLetter(lastName)}`}
  439. </Avatar>
  440. </ListItemIcon>
  441. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  442. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  443. primaryTypographyProps={{ color: '#ffffff' }}
  444. secondary={timeStampEU(createdAt)} secondaryTypographyProps={{ color: '#ffffff' }} />
  445. </div>
  446. <div className={classes.wrapperIcons}>
  447. <ZoomOutIcon onClick={handleDecreaseScale}
  448. className={classes.magnifying} fontSize='large' />
  449. <ZoomInIcon onClick={handleIncreaseScale}
  450. className={classes.magnifying} fontSize='large' />
  451. <DownloadForOfflineIcon onClick={() => handleDownload(url, fullType)}
  452. className={classes.downloadIcon} fontSize='large' />
  453. <CloseIcon id='close' onClick={handleCloseWatch}
  454. className={classes.iconCloseOverlay} fontSize='large' />
  455. </div>
  456. </div>
  457. <div ref={refView} className={classes.wrapperImage}>
  458. <img className={classes.innerImage}
  459. width='100%' height='auto' alt='imageItem' src={url} />
  460. </div>
  461. </div> :
  462. <div className={classes.container}>
  463. <div onContextMenu={(e) => handleContextMenu(e)}
  464. className={selected ? classes.wrapperActive : classes.wrapper}
  465. style={{pointerEvents:isSomeSelected?'none':'auto'}}>
  466. <ImageIcon fontSize='large' style={{ color: '#bd9a00' }} />
  467. <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
  468. style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
  469. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload} >
  470. <FileDownloadIcon fontSize='medium'/>
  471. </IconButton>
  472. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  473. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  474. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  475. {isSomeSelected && <div className={classes.checkboxSelect}><Checkbox {...label} checked={checked} sx={{ color: nightMode ? '#ffffff' : '#00ff48', '&.Mui-checked': { color: nightMode ? '#ffffff' : '#00ff48' } }}
  476. onClick={() => handleSelected(_id)}/></div>}
  477. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  478. anchorEl={anchorEl} open={open} onClose={handleClose}>
  479. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  480. {emojisArr.map((el:string, i:number) =>
  481. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  482. </MenuItem>
  483. <Divider />
  484. <MenuItem onClick={() => {
  485. handleReply(_id)
  486. handleClose(undefined)
  487. }}>
  488. <ReplyIcon />
  489. Reply
  490. </MenuItem>
  491. <MenuItem onClick={() => {
  492. handleForward(_id)
  493. handleClose(undefined)
  494. }}>
  495. <ReplyIcon style={{transform :'rotateY(180deg)'}} />
  496. Forward
  497. </MenuItem>
  498. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  499. <MenuItem>
  500. <ContentCopyIcon />
  501. Copy Link
  502. </MenuItem>
  503. </CopyToClipboard>
  504. <MenuItem onClick={() => {
  505. pinMessageById(_id, !pinned)
  506. handleClose(undefined)
  507. }}>
  508. {pinned ?
  509. <CloseIcon className={classes.iconClose} /> :
  510. <PushPinIcon />}
  511. {pinned?'Unpin':'Pin'}
  512. </MenuItem>
  513. <MenuItem onClick={() => {
  514. handleSelected(_id)
  515. handleClose(undefined)
  516. }}>
  517. <CheckBoxIcon />
  518. Select
  519. </MenuItem>
  520. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  521. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  522. Delete
  523. </MenuItem>
  524. </StyledMenu>
  525. {modal &&
  526. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  527. <div className={classes.modalDelete}>
  528. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  529. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  530. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  531. DELETE MESSAGE
  532. </Button>
  533. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  534. CANCEL
  535. </Button>
  536. </div>
  537. </div>}
  538. </div>
  539. {caption&&<div className={classes.captionWrapper}>{caption}</div>}
  540. </div>)
  541. };
  542. export default MessagesLeftImage