index.tsx 15 KB

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