index.tsx 12 KB

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