index.tsx 16 KB

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