index.tsx 16 KB

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