index.tsx 16 KB

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