index.tsx 12 KB

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