index.tsx 16 KB

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