index.tsx 14 KB

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