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 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:'10px 22px 10px 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. zIndex: 2,
  93. width:'100%',
  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. zIndex:1
  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. zIndex:1
  292. },
  293. });
  294. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  295. interface IMessageRightImage {
  296. url: string,
  297. tongue: boolean,
  298. watched: boolean,
  299. avatarUrl: string,
  300. color: string,
  301. createdAt:string,
  302. fullType: string,
  303. caption: string,
  304. emoji: string,
  305. emojiCompanion: string,
  306. pinned: boolean,
  307. isSomeSelected: boolean,
  308. isSelected:(_id:string) => boolean,
  309. handleSelected: (_id:string) => void,
  310. _id: string,
  311. name: string,
  312. lastName: string,
  313. nightMode: boolean,
  314. handleReply: (_id: string) => void,
  315. handleForward: (_id: string) => void,
  316. }
  317. const MessageRightImage = ({url,tongue,watched,avatarUrl,color,createdAt,fullType,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,name,lastName,nightMode,handleReply,handleForward}:IMessageRightImage) => {
  318. const classes = useStyles();
  319. const [watch, setWatch] = useState<boolean>(false)
  320. const [anchorEl, setAnchorEl] = useState<any>(null);
  321. const [selected, setSelected] = useState<boolean>(false);
  322. const [modal, setModal] = useState<boolean>(false)
  323. const [scale, setScale] = useState<number>(1)
  324. const refView = useRef<null | any>(null)
  325. const open = Boolean(anchorEl);
  326. const checked = isSelected(_id)
  327. const handleIncreaseScale = () => {
  328. if (scale < 5) {
  329. refView.current.style.transform = `scale(${scale+0.25},${scale+0.25})`
  330. refView.current.style.transformOrigin = `${50 / scale + 0.25}px ${50 / scale + 0.25}px`
  331. setScale(scale+0.25)
  332. }
  333. }
  334. const handleDecreaseScale = () => {
  335. if (scale >= 0.5) {
  336. refView.current.style.transform = `scale(${scale-0.25},${scale-0.25})`
  337. refView.current.style.transformOrigin = `${50/scale-0.25}px ${50/scale-0.25}px`
  338. setScale(scale-0.25)
  339. }
  340. }
  341. const handleOpenWatch = () => !watch&&setWatch(true)
  342. const handleCloseWatch = (e:any) => {
  343. const id = e.target.id
  344. if (id === 'overlay' || id === 'close') {
  345. setWatch(false)
  346. refView.current.style.transform = 'scale(1)'
  347. setScale(1)
  348. }
  349. }
  350. const handleClose = (type: string | undefined): void => {
  351. if (type === 'copy') copied('Link')
  352. if (type === 'delete') setModal(true)
  353. setAnchorEl(null)
  354. setSelected(false)
  355. }
  356. const handleDeleteModal = (e: any) => {
  357. const id = e.target.id
  358. if (id === 'overlay' || id === 'cancel') return setModal(false)
  359. if (id === 'delete') {
  360. removeMessageById(_id)
  361. setModal(false)
  362. }
  363. }
  364. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  365. e.preventDefault()
  366. setAnchorEl(e.currentTarget)
  367. setSelected(true)
  368. }
  369. const handleEmojiMenu = ({ target }: any): void => {
  370. const idEmoji = target.id
  371. if (idEmoji === emoji) {updateMessageById(_id,'')
  372. } else updateMessageById(_id,idEmoji)
  373. }
  374. return (watch ?
  375. <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
  376. <div className={classes.topBar}>
  377. <div className={classes.wrapperCredentials}>
  378. <ListItemIcon >
  379. <Avatar alt={name} src={url?url:undefined}
  380. sx={{ background: color, width: 44, height: 44 }}>
  381. {!url&&`${firstLetter(name)}${firstLetter(lastName)}`}
  382. </Avatar>
  383. </ListItemIcon>
  384. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  385. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  386. primaryTypographyProps={{ color: '#ffffff' }}
  387. secondary={timeStampEU(createdAt)} secondaryTypographyProps={{ color: '#ffffff' }} />
  388. </div>
  389. <div className={classes.wrapperIcons}>
  390. <ZoomOutIcon onClick={handleDecreaseScale}
  391. className={classes.magnifying} fontSize='large' />
  392. <ZoomInIcon onClick={handleIncreaseScale}
  393. className={classes.magnifying} fontSize='large' />
  394. <DownloadForOfflineIcon onClick={() => handleDownload(url, fullType)}
  395. className={classes.downloadIcon} fontSize='large' />
  396. <CloseIcon id='close' onClick={handleCloseWatch}
  397. className={classes.iconCloseOverlay} fontSize='large' />
  398. </div>
  399. </div>
  400. <div ref={refView} className={classes.wrapperImage}>
  401. <img className={classes.innerImage}
  402. width='100%' height='auto' alt='imageItem' src={url} />
  403. </div>
  404. </div> :
  405. <div className={classes.container}>
  406. <div onContextMenu={(e) => handleContextMenu(e)} className={classes.wrapper}
  407. style={{backgroundColor:selected?'#ced8d7':'#deffa9',pointerEvents:isSomeSelected?'none':'auto'}}>
  408. <Typography style={{color: "#26afee"}} variant="h6" align="right">
  409. {`${firstLetter(name)}${slicedWord(name, 15, 1)}
  410. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  411. </Typography>
  412. <div className={classes.wrapperInner}>
  413. <ImageIcon onClick={() => handleDownload(url, fullType)}
  414. className={classes.folderIcon} fontSize='large' />
  415. <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
  416. style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
  417. </div>
  418. <ListItemText style={{wordBreak:'break-word'}} secondary={caption} secondaryTypographyProps={{color: "#000000"}}/>
  419. <div className={classes.informationWrapper}>
  420. <div className={classes.time} style={{ color: watched?'#18bd03':'#414141'}}>{timeStampMessage(createdAt)}</div>
  421. {watched&&<DoneAllIcon 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