index.tsx 11 KB

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