index.tsx 16 KB

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