index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import { styled } from '@mui/material/styles';
  3. import { useState } from "react";
  4. import DownloadForOfflineIcon from '@mui/icons-material/DownloadForOffline';
  5. import { IconButton } from "@material-ui/core";
  6. import ImageIcon from '@mui/icons-material/Image';
  7. import FileDownloadIcon from '@mui/icons-material/FileDownload';
  8. import Button from '@mui/material/Button';
  9. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  10. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  11. import Menu from '@mui/material/Menu';
  12. import MenuItem from '@mui/material/MenuItem';
  13. import Divider from '@mui/material/Divider';
  14. import CheckBoxIcon from '@mui/icons-material/CheckBox';
  15. import Checkbox from '@mui/material/Checkbox';
  16. import PushPinIcon from '@mui/icons-material/PushPin';
  17. import CloseIcon from '@mui/icons-material/Close';
  18. import { CopyToClipboard } from 'react-copy-to-clipboard';
  19. import { removeMessageById,updateMessageById,pinMessageById } from "../../../../../../api-data";
  20. import { timeStampMessage, timeStampEU,handleDownload,copied,emojisArr } from '../../../../../../helpers'
  21. const StyledMenu = styled((props:any) => (
  22. <Menu
  23. elevation={0}
  24. anchorOrigin={{
  25. vertical: 'top',
  26. horizontal: 'right',
  27. }}
  28. transformOrigin={{
  29. vertical: 'bottom',
  30. horizontal: 'right',
  31. }}
  32. {...props}
  33. />
  34. ))(({ theme }:any) => ({
  35. '& .MuiPaper-root': {
  36. borderRadius: 10,
  37. marginTop: theme.spacing(0),
  38. minWidth: 220,
  39. color:
  40. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  41. boxShadow:
  42. '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',
  43. '& .MuiMenu-list': {
  44. padding: '4px 4px',
  45. },
  46. '& .MuiMenuItem-root': {
  47. marginBottom: theme.spacing(1),
  48. '& .MuiSvgIcon-root': {
  49. fontSize: 21,
  50. color: theme.palette.text.secondary,
  51. marginRight: theme.spacing(2),
  52. }
  53. },
  54. },
  55. }));
  56. const useStyles = makeStyles({
  57. container: {
  58. display: "flex",
  59. justifyContent: "flex-end",
  60. borderRadius: 7,
  61. },
  62. wrapper: {
  63. width: 400,
  64. position: 'relative',
  65. display: 'flex',
  66. alignItems: 'center',
  67. alignContent: 'center',
  68. justifyContent: 'space-between',
  69. borderRadius: 7,
  70. padding: '12px 5px 18px 5px',
  71. backgroundColor: '#deffa9',
  72. "&:after": {
  73. content: "''",
  74. position: "absolute",
  75. width: "0",
  76. height: "0",
  77. borderBottom: "15px solid #deffa9",
  78. borderLeft: "15px solid transparent",
  79. borderRight: "15px solid transparent",
  80. bottom: "0",
  81. right: "-15px"
  82. },
  83. "&:before": {
  84. content: "''",
  85. position: "absolute",
  86. width: "0",
  87. height: "0",
  88. borderBottom: "17px solid #deffa9",
  89. borderLeft: "16px solid transparent",
  90. borderRight: "16px solid transparent",
  91. bottom: "0px",
  92. right: "-17px"
  93. }
  94. },
  95. wrapperActive: {
  96. width: 400,
  97. position: 'relative',
  98. display: 'flex',
  99. alignItems: 'center',
  100. alignContent: 'center',
  101. justifyContent: 'space-between',
  102. borderRadius: 7,
  103. padding: '12px 5px 18px 5px',
  104. backgroundColor: '#ced8d7',
  105. "&:after": {
  106. content: "''",
  107. position: "absolute",
  108. width: "0",
  109. height: "0",
  110. borderBottom: "15px solid #ced8d7",
  111. borderLeft: "15px solid transparent",
  112. borderRight: "15px solid transparent",
  113. bottom: "0",
  114. right: "-15px"
  115. },
  116. "&:before": {
  117. content: "''",
  118. position: "absolute",
  119. width: "0",
  120. height: "0",
  121. borderBottom: "17px solid #ced8d7",
  122. borderLeft: "16px solid transparent",
  123. borderRight: "16px solid transparent",
  124. bottom: "0px",
  125. right: "-17px"
  126. }
  127. },
  128. image: {
  129. borderRadius: 7,
  130. width: 300,
  131. maxHeight: 400,
  132. cursor:'pointer',
  133. },
  134. time: {
  135. position: "absolute",
  136. fontSize: ".65em",
  137. fontWeight:600,
  138. bottom: 0,
  139. right: 6,
  140. color: '#414141',
  141. padding: 3,
  142. borderRadius: 5,
  143. },
  144. captionWrapper: {
  145. position: "absolute",
  146. fontSize: ".85em",
  147. color: '#626262',
  148. maxWidth:'76%',
  149. fontWeight:600,
  150. bottom: -25,
  151. right: 40,
  152. borderRadius: 5,
  153. wordBreak:'break-word',
  154. textAlign: "left",
  155. font: "400 .9em 'Open Sans', sans-serif",
  156. backgroundColor: '#ffffff',
  157. padding:10,
  158. "&:after": {
  159. content: "''",
  160. position: "absolute",
  161. width: "0",
  162. height: "0",
  163. borderBottom: "15px solid #ffffff",
  164. borderLeft: "15px solid transparent",
  165. borderRight: "15px solid transparent",
  166. bottom: "0",
  167. right: "-15px"
  168. },
  169. "&:before": {
  170. content: "''",
  171. position: "absolute",
  172. width: "0",
  173. height: "0",
  174. borderBottom: "17px solid #ffffff",
  175. borderLeft: "16px solid transparent",
  176. borderRight: "16px solid transparent",
  177. bottom: "-1px",
  178. right: "-17px"
  179. }
  180. },
  181. bntDownload: {
  182. backgroundColor: 'inherit',
  183. color: '#54b0fc',
  184. width: 30,
  185. height:30,
  186. '&:hover': {
  187. backgroundColor: '#54b0fc',
  188. color:'#ffffff'
  189. }
  190. },
  191. overlay: {
  192. position: 'fixed',
  193. top: 0,
  194. left: 0,
  195. width: '100vw',
  196. height: '100vh',
  197. zIndex: 100,
  198. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  199. overflowY: 'hidden',
  200. boxSizing: 'border-box',
  201. display: 'flex',
  202. justifyContent: 'center',
  203. alignContent: 'center',
  204. alignItems: 'center'
  205. },
  206. wrapperOverlayImg: {
  207. width: '30%',
  208. maxHeight: '80%',
  209. position: 'relative',
  210. display: 'flex',
  211. },
  212. overlayDownloadIcon: {
  213. position: 'absolute',
  214. content: '',
  215. right: 0,
  216. top: -40,
  217. cursor: 'pointer',
  218. color: '#e9e7e7',
  219. padding: 0,
  220. borderRadius: '50%',
  221. '&:hover': {
  222. backgroundColor: '#ffffff',
  223. color: '#b8b7b7',
  224. }
  225. },
  226. overlayTime: {
  227. position: 'absolute',
  228. content: '',
  229. color: '#ffffff',
  230. top: -30,
  231. left: 0,
  232. borderRadius: 10,
  233. padding:'2px 6px 2px 6px',
  234. backgroundColor:'#707070'
  235. },
  236. modalDelete: {
  237. background: '#ffffff',
  238. position: 'absolute',
  239. content:'',
  240. width: '20%',
  241. height:'auto',
  242. left: '40%',
  243. bottom: '48.5%',
  244. borderRadius: 10,
  245. padding: 10,
  246. display: 'flex',
  247. flexDirection:'column'
  248. },
  249. overlayDelete: {
  250. position: 'fixed',
  251. top: 0,
  252. left: 0,
  253. width: '100vw',
  254. height: '100vh',
  255. zIndex: 100,
  256. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  257. overflowY: 'hidden',
  258. },
  259. emojiTitle: {
  260. position: "absolute",
  261. fontSize: "1.7em",
  262. fontWeight:600,
  263. bottom: 0,
  264. left: -40,
  265. },
  266. emojiCompanionTitle: {
  267. position: "absolute",
  268. fontSize: "1.7em",
  269. fontWeight:600,
  270. bottom: '2rem',
  271. left: -40,
  272. },
  273. emoji: {
  274. cursor: 'pointer',
  275. fontSize: '1.7rem',
  276. transition: 'all 0.3s',
  277. '&:hover': {
  278. transform: 'scale(1.5)'
  279. }
  280. },
  281. emojiActive: {
  282. cursor: 'pointer',
  283. fontSize: '1.2rem',
  284. animation: `$emoji 0.6s ease-out`,
  285. animationDirection: 'forwards',
  286. animationIterationCount: 1,
  287. },
  288. '@keyframes emoji': {
  289. '5%': { transform: 'translateY(1rem)'},
  290. '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
  291. '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
  292. '80%': {opacity: 0},
  293. '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  294. },
  295. iconClose: {
  296. '&:hover': {
  297. transform: 'rotate(180deg)',
  298. transition: 'all 250ms ease-out ',
  299. }
  300. },
  301. });
  302. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  303. interface IMessageRightImage {
  304. url:string,
  305. createdAt:string,
  306. color: string,
  307. fullType: string,
  308. caption: string,
  309. emoji: string,
  310. emojiCompanion: string,
  311. pinned: boolean,
  312. isSomeSelected: boolean,
  313. isSelected:(_id:string) => boolean,
  314. handleSelected: (_id:string) => void,
  315. _id:string
  316. }
  317. const MessageRightImage = ({url,createdAt,color,fullType,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id}: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 open = Boolean(anchorEl);
  324. const checked = isSelected(_id)
  325. const handleOpenWatch = () => !watch&&setWatch(true)
  326. const handleCloseWatch = (e:any) => e.target.id === 'overlay'&&watch&&setWatch(false)
  327. const handleClose = (type: string | undefined): void => {
  328. if (type === 'copy') copied('Link')
  329. if (type === 'delete') setModal(true)
  330. setAnchorEl(null)
  331. setSelected(false)
  332. }
  333. const handleDeleteModal = (e: any) => {
  334. const id = e.target.id
  335. if (id === 'overlay' || id === 'cancel') return setModal(false)
  336. if (id === 'delete') {
  337. removeMessageById(_id)
  338. setModal(false)
  339. }
  340. }
  341. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  342. e.preventDefault()
  343. setAnchorEl(e.currentTarget)
  344. setSelected(true)
  345. }
  346. const handleEmojiMenu = ({ target }: any): void => {
  347. const idEmoji = target.id
  348. if (idEmoji === emoji) {updateMessageById(_id,'')
  349. } else updateMessageById(_id,idEmoji)
  350. }
  351. return (watch ?
  352. <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
  353. <div className={classes.wrapperOverlayImg}>
  354. <span className={classes.overlayTime}>{timeStampEU(createdAt)}</span>
  355. <DownloadForOfflineIcon className={classes.overlayDownloadIcon} fontSize='large'
  356. onClick={() => handleDownload(url, fullType)}/>
  357. <img width='100%' height='auto' alt='imageItem' src={url} />
  358. </div>
  359. </div> :
  360. <div className={classes.container} style={{ marginBottom: caption ? 40 : 15}}>
  361. <div onContextMenu={(e) => handleContextMenu(e)}
  362. className={selected ? classes.wrapperActive : classes.wrapper}
  363. style={{pointerEvents:isSomeSelected?'none':'auto'}}>
  364. <ImageIcon fontSize='large' style={{ color: '#bd9a00' }} />
  365. <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
  366. style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
  367. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload}>
  368. <FileDownloadIcon fontSize='medium'/>
  369. </IconButton>
  370. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  371. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  372. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  373. {caption&&<div className={classes.captionWrapper}>{caption}</div>}
  374. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  375. anchorEl={anchorEl} open={open} onClose={handleClose}>
  376. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  377. {emojisArr.map((el:string, i:number) =>
  378. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  379. </MenuItem>
  380. <Divider/>
  381. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  382. <MenuItem>
  383. <ContentCopyIcon />
  384. Copy Image link
  385. </MenuItem>
  386. </CopyToClipboard>
  387. <MenuItem onClick={() => {
  388. pinMessageById(_id, !pinned)
  389. handleClose(undefined)
  390. }}>
  391. {pinned ?
  392. <CloseIcon className={classes.iconClose} /> :
  393. <PushPinIcon />}
  394. {pinned?'Unpin message':'Pin message'}
  395. </MenuItem>
  396. <MenuItem onClick={() => {
  397. handleSelected(_id)
  398. handleClose(undefined)
  399. }}>
  400. <CheckBoxIcon />
  401. Select message
  402. </MenuItem>
  403. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  404. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  405. Delete message
  406. </MenuItem>
  407. </StyledMenu>
  408. {modal &&
  409. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  410. <div className={classes.modalDelete}>
  411. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  412. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  413. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  414. DELETE MESSAGE
  415. </Button>
  416. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  417. CANCEL
  418. </Button>
  419. </div>
  420. </div>}
  421. </div>
  422. {isSomeSelected&&<Checkbox {...label} checked={checked} onClick={() => handleSelected(_id)}/>}
  423. </div>
  424. )};
  425. export default MessageRightImage