index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 18px 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 18px 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. caption: {
  140. position: "absolute",
  141. fontSize: ".65em",
  142. fontWeight:600,
  143. bottom: 0,
  144. left: 6,
  145. color: '#000000',
  146. padding: 3,
  147. borderRadius: 5,
  148. },
  149. bntDownload: {
  150. backgroundColor: 'inherit',
  151. color: '#54b0fc',
  152. width: 30,
  153. height:30,
  154. '&:hover': {
  155. backgroundColor: '#54b0fc',
  156. color:'#ffffff'
  157. }
  158. },
  159. overlay: {
  160. position: 'fixed',
  161. top: 0,
  162. left: 0,
  163. width: '100vw',
  164. height: '100vh',
  165. zIndex: 100,
  166. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  167. overflowY: 'hidden',
  168. boxSizing: 'border-box',
  169. display: 'flex',
  170. justifyContent: 'center',
  171. alignContent: 'center',
  172. alignItems: 'center'
  173. },
  174. wrapperOverlayImg: {
  175. width: '30%',
  176. maxHeight: '80%',
  177. position: 'relative',
  178. display: 'flex',
  179. },
  180. overlayDownloadIcon: {
  181. position: 'absolute',
  182. content: '',
  183. right: 0,
  184. top: -40,
  185. cursor: 'pointer',
  186. color: '#e9e7e7',
  187. padding: 0,
  188. borderRadius: '50%',
  189. '&:hover': {
  190. backgroundColor: '#ffffff',
  191. color: '#b8b7b7',
  192. }
  193. },
  194. overlayTime: {
  195. position: 'absolute',
  196. content: '',
  197. color: '#ffffff',
  198. top: -30,
  199. left: 0,
  200. borderRadius: 10,
  201. padding:'2px 6px 2px 6px',
  202. backgroundColor:'#707070'
  203. },
  204. modalDelete: {
  205. background: '#ffffff',
  206. position: 'absolute',
  207. content:'',
  208. width: '20%',
  209. height:'auto',
  210. left: '40%',
  211. bottom: '48.5%',
  212. borderRadius: 10,
  213. padding: 10,
  214. display: 'flex',
  215. flexDirection:'column'
  216. },
  217. overlayDelete: {
  218. position: 'fixed',
  219. top: 0,
  220. left: 0,
  221. width: '100vw',
  222. height: '100vh',
  223. zIndex: 100,
  224. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  225. overflowY: 'hidden',
  226. },
  227. });
  228. interface IMessagesLeftImage {
  229. url:string,
  230. createdAt:string,
  231. color: string,
  232. message: string,
  233. messages: any,
  234. fullType: string,
  235. caption :string,
  236. _id:string
  237. }
  238. const MessagesLeftImage = ({url,createdAt,color,message,messages,fullType,caption,_id}:IMessagesLeftImage) => {
  239. const classes = useStyles();
  240. const [watch, setWatch] = useState<boolean>(false)
  241. const [anchorEl, setAnchorEl] = useState<any>(null);
  242. const [selected, setSelected] = useState<boolean>(false);
  243. const [modal,setModal] = useState<boolean>(false)
  244. const open = Boolean(anchorEl);
  245. const handleOpenWatch = () => !watch&&setWatch(true)
  246. const handleCloseWatch = (e:any) => e.target.id === 'overlay'&&watch&&setWatch(false)
  247. const handleClose = (type: string | undefined): void => {
  248. if (type === 'copy') copied('Link')
  249. if (type === 'delete') setModal(true)
  250. setAnchorEl(null)
  251. setSelected(false)
  252. }
  253. const handleDeleteModal = (e: any) => {
  254. const id = e.target.id
  255. if (id === 'overlay' || id === 'cancel') return setModal(false)
  256. if (id === 'delete') {
  257. removeMessageById(_id)
  258. setModal(false)
  259. }
  260. }
  261. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  262. e.preventDefault()
  263. setAnchorEl(e.currentTarget)
  264. setSelected(true)
  265. }
  266. return (watch ?
  267. <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
  268. <div className={classes.wrapperOverlayImg}>
  269. <span className={classes.overlayTime}>{timeStampEU(createdAt)}</span>
  270. <DownloadForOfflineIcon className={classes.overlayDownloadIcon} fontSize='large'
  271. onClick={() => handleDownload(url, fullType)}/>
  272. <img width='100%' height='auto' alt='imageItem' src={url} />
  273. </div>
  274. </div> :
  275. <div className={classes.container}>
  276. <div onContextMenu={(e) => handleContextMenu(e)}
  277. className={selected? classes.wrapperActive:classes.wrapper}>
  278. <ImageIcon fontSize='large' style={{ color: '#bd9a00' }} />
  279. <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
  280. style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
  281. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload} >
  282. <FileDownloadIcon fontSize='medium'/>
  283. </IconButton>
  284. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  285. <div className={classes.caption}>{caption}</div>
  286. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  287. anchorEl={anchorEl} open={open} onClose={handleClose}>
  288. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  289. <MenuItem>
  290. <ContentCopyIcon />
  291. Copy Image link
  292. </MenuItem>
  293. </CopyToClipboard>
  294. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  295. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  296. Delete message
  297. </MenuItem>
  298. </StyledMenu>
  299. {modal &&
  300. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  301. <div className={classes.modalDelete}>
  302. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  303. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  304. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  305. DELETE MESSAGE
  306. </Button>
  307. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  308. CANCEL
  309. </Button>
  310. </div>
  311. </div>}
  312. </div>
  313. </div>
  314. )};
  315. export default MessagesLeftImage