index.tsx 16 KB

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