index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 { CopyToClipboard } from 'react-copy-to-clipboard';
  14. import { removeMessageById } from "../../../../../../api-data";
  15. import { timeStampMessage, timeStampEU,handleDownload,copied } from '../../../../../../helpers'
  16. const StyledMenu = styled((props:any) => (
  17. <Menu
  18. elevation={0}
  19. anchorOrigin={{
  20. vertical: 'top',
  21. horizontal: 'right',
  22. }}
  23. transformOrigin={{
  24. vertical: 'bottom',
  25. horizontal: 'right',
  26. }}
  27. {...props}
  28. />
  29. ))(({ theme }:any) => ({
  30. '& .MuiPaper-root': {
  31. borderRadius: 10,
  32. marginTop: theme.spacing(0),
  33. minWidth: 220,
  34. color:
  35. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  36. boxShadow:
  37. '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',
  38. '& .MuiMenu-list': {
  39. padding: '4px 4px',
  40. },
  41. '& .MuiMenuItem-root': {
  42. marginBottom: theme.spacing(1),
  43. '& .MuiSvgIcon-root': {
  44. fontSize: 21,
  45. color: theme.palette.text.secondary,
  46. marginRight: theme.spacing(2),
  47. }
  48. },
  49. },
  50. }));
  51. const useStyles = makeStyles({
  52. container: {
  53. display: "flex",
  54. justifyContent: "flex-start",
  55. marginBottom:15
  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 12px 5px',
  66. backgroundColor: '#ffffff',
  67. "&:after": {
  68. content: "''",
  69. position: "absolute",
  70. width: "0",
  71. height: "0",
  72. borderBottom: "15px solid #ffffff",
  73. borderLeft: "15px solid transparent",
  74. borderRight: "15px solid transparent",
  75. bottom: '0px',
  76. left: "-15px"
  77. },
  78. "&:before": {
  79. content: "''",
  80. position: "absolute",
  81. width: "0",
  82. height: "0",
  83. borderBottom: "17px solid #ffffff",
  84. borderLeft: "16px solid transparent",
  85. borderRight: "16px solid transparent",
  86. bottom: "0px",
  87. left: "-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 12px 5px',
  99. backgroundColor: '#babdbc',
  100. "&:after": {
  101. content: "''",
  102. position: "absolute",
  103. width: "0",
  104. height: "0",
  105. borderBottom: "15px solid #babdbc",
  106. borderLeft: "15px solid transparent",
  107. borderRight: "15px solid transparent",
  108. bottom: '0px',
  109. left: "-15px"
  110. },
  111. "&:before": {
  112. content: "''",
  113. position: "absolute",
  114. width: "0",
  115. height: "0",
  116. borderBottom: "17px solid #babdbc",
  117. borderLeft: "16px solid transparent",
  118. borderRight: "16px solid transparent",
  119. bottom: "0px",
  120. left: "-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. bntDownload: {
  140. backgroundColor: 'inherit',
  141. color: '#54b0fc',
  142. width: 30,
  143. height:30,
  144. '&:hover': {
  145. backgroundColor: '#54b0fc',
  146. color:'#ffffff'
  147. }
  148. },
  149. overlay: {
  150. position: 'fixed',
  151. top: 0,
  152. left: 0,
  153. width: '100vw',
  154. height: '100vh',
  155. zIndex: 100,
  156. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  157. overflowY: 'hidden',
  158. boxSizing: 'border-box',
  159. display: 'flex',
  160. justifyContent: 'center',
  161. alignContent: 'center',
  162. alignItems: 'center'
  163. },
  164. wrapperOverlayImg: {
  165. width: '30%',
  166. maxHeight: '80%',
  167. position: 'relative',
  168. display: 'flex',
  169. },
  170. overlayDownloadIcon: {
  171. position: 'absolute',
  172. content: '',
  173. right: 0,
  174. top: -40,
  175. cursor: 'pointer',
  176. color: '#e9e7e7',
  177. padding: 0,
  178. borderRadius: '50%',
  179. '&:hover': {
  180. backgroundColor: '#ffffff',
  181. color: '#b8b7b7',
  182. }
  183. },
  184. overlayTime: {
  185. position: 'absolute',
  186. content: '',
  187. color: '#ffffff',
  188. top: -30,
  189. left: 0,
  190. borderRadius: 10,
  191. padding:'2px 6px 2px 6px',
  192. backgroundColor:'#707070'
  193. },
  194. modalDelete: {
  195. background: '#ffffff',
  196. position: 'absolute',
  197. content:'',
  198. width: '20%',
  199. height:'auto',
  200. left: '40%',
  201. bottom: '48.5%',
  202. borderRadius: 10,
  203. padding: 10,
  204. display: 'flex',
  205. flexDirection:'column'
  206. },
  207. overlayDelete: {
  208. position: 'fixed',
  209. top: 0,
  210. left: 0,
  211. width: '100vw',
  212. height: '100vh',
  213. zIndex: 100,
  214. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  215. overflowY: 'hidden',
  216. },
  217. });
  218. interface IMessagesLeftImage {
  219. url:string,
  220. createdAt:string,
  221. color: string,
  222. message: string,
  223. messages: any,
  224. fullType: string,
  225. _id:string
  226. }
  227. const MessagesLeftImage = ({url,createdAt,color,message,messages,fullType,_id}:IMessagesLeftImage) => {
  228. const classes = useStyles();
  229. const [watch, setWatch] = useState<boolean>(false)
  230. const [anchorEl, setAnchorEl] = useState<any>(null);
  231. const [selected, setSelected] = useState<boolean>(false);
  232. const [modal,setModal] = useState<boolean>(false)
  233. const open = Boolean(anchorEl);
  234. const handleOpenWatch = () => !watch&&setWatch(true)
  235. const handleCloseWatch = (e:any) => e.target.id === 'overlay'&&watch&&setWatch(false)
  236. const handleClose = (type: string | undefined): void => {
  237. if (type === 'copy') copied('Link')
  238. if (type === 'delete') setModal(true)
  239. setAnchorEl(null)
  240. setSelected(false)
  241. }
  242. const handleDeleteModal = (e: any) => {
  243. const id = e.target.id
  244. if (id === 'overlay' || id === 'cancel') return setModal(false)
  245. if (id === 'delete') {
  246. removeMessageById(_id)
  247. setModal(false)
  248. }
  249. }
  250. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  251. e.preventDefault()
  252. setAnchorEl(e.currentTarget)
  253. setSelected(true)
  254. }
  255. return (watch ?
  256. <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
  257. <div className={classes.wrapperOverlayImg}>
  258. <span className={classes.overlayTime}>{timeStampEU(createdAt)}</span>
  259. <DownloadForOfflineIcon className={classes.overlayDownloadIcon} fontSize='large'
  260. onClick={() => handleDownload(url, fullType)}/>
  261. <img width='100%' height='auto' alt='imageItem' src={url} />
  262. </div>
  263. </div> :
  264. <div className={classes.container}>
  265. <div onContextMenu={(e) => handleContextMenu(e)}
  266. className={selected? classes.wrapperActive:classes.wrapper}>
  267. <ImageIcon fontSize='large' style={{ color: '#bd9a00' }} />
  268. <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
  269. style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
  270. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload} >
  271. <FileDownloadIcon fontSize='medium'/>
  272. </IconButton>
  273. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  274. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  275. anchorEl={anchorEl} open={open} onClose={handleClose}>
  276. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  277. <MenuItem>
  278. <ContentCopyIcon />
  279. Copy Image link
  280. </MenuItem>
  281. </CopyToClipboard>
  282. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  283. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  284. Delete message
  285. </MenuItem>
  286. </StyledMenu>
  287. {modal &&
  288. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  289. <div className={classes.modalDelete}>
  290. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  291. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  292. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  293. DELETE MESSAGE
  294. </Button>
  295. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  296. CANCEL
  297. </Button>
  298. </div>
  299. </div>}
  300. </div>
  301. </div>
  302. )};
  303. export default MessagesLeftImage