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