index.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import { styled } from '@mui/material/styles';
  3. import { useState } from "react";
  4. import { IconButton } from "@material-ui/core";
  5. import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile';
  6. import FileDownloadIcon from '@mui/icons-material/FileDownload';
  7. import Button from '@mui/material/Button';
  8. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  9. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  10. import Menu from '@mui/material/Menu';
  11. import MenuItem from '@mui/material/MenuItem';
  12. import { CopyToClipboard } from 'react-copy-to-clipboard';
  13. import { removeMessageById } from "../../../../../../api-data";
  14. import { timeStampMessage,copied } from '../../../../../../helpers'
  15. const FileViewer = require('react-file-viewer')
  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. position: 'relative',
  59. display: 'flex',
  60. justifyContent: 'space-between',
  61. alignContent: 'center',
  62. alignItems: 'center',
  63. width: 200,
  64. padding: '5px 5px 12px 5px',
  65. backgroundColor: '#ffffff',
  66. borderRadius: 7,
  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. position: 'relative',
  92. display: 'flex',
  93. justifyContent: 'space-between',
  94. alignContent: 'center',
  95. alignItems: 'center',
  96. width: 200,
  97. padding: '5px 5px 12px 5px',
  98. backgroundColor: '#babdbc',
  99. borderRadius: 7,
  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. bntDownload: {
  124. backgroundColor: 'inherit',
  125. color: '#54b0fc',
  126. width: 30,
  127. height:30,
  128. '&:hover': {
  129. backgroundColor: '#54b0fc',
  130. color:'#ffffff'
  131. }
  132. },
  133. title: {
  134. margin: '0 30px 0 5px',
  135. color: '#0e0d0d',
  136. cursor: 'pointer',
  137. '&:hover': {
  138. color: '#54b0fc',
  139. }
  140. },
  141. time: {
  142. position: "absolute",
  143. fontSize: ".65em",
  144. fontWeight:600,
  145. bottom: 0,
  146. right: 6,
  147. color: '#414141',
  148. padding: 3,
  149. borderRadius: 5,
  150. },
  151. overlay: {
  152. position: 'sticky',
  153. top: 0,
  154. left: 0,
  155. width: '100%',
  156. minHeight:'100%',
  157. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  158. border: 'solid 1px rgba(179, 179, 179, 0.6)',
  159. overflow: 'hidden',
  160. cursor:'pointer'
  161. },
  162. modalDelete: {
  163. background: '#ffffff',
  164. position: 'absolute',
  165. content:'',
  166. width: '20%',
  167. height:'auto',
  168. left: '40%',
  169. bottom: '48.5%',
  170. borderRadius: 10,
  171. padding: 10,
  172. display: 'flex',
  173. flexDirection:'column'
  174. },
  175. overlayDelete: {
  176. position: 'fixed',
  177. top: 0,
  178. left: 0,
  179. width: '100vw',
  180. height: '100vh',
  181. zIndex: 100,
  182. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  183. overflowY: 'hidden',
  184. },
  185. });
  186. interface IMessageLeftFile {
  187. url:string,
  188. createdAt: string,
  189. type: string,
  190. _id:string
  191. }
  192. const MessageLeftFile = ({ url,createdAt,type,_id }:IMessageLeftFile) => {
  193. const classes = useStyles();
  194. const [read, setRead] = useState<boolean>(false)
  195. const [anchorEl, setAnchorEl] = useState<any>(null);
  196. const [selected, setSelected] = useState<boolean>(false);
  197. const [modal,setModal] = useState<boolean>(false)
  198. const open = Boolean(anchorEl);
  199. const handleOpenRead = () => !read&&setRead(true)
  200. const handleCloseRead = () => read && setRead(false)
  201. const handleClose = (type: string | undefined): void => {
  202. if (type === 'copy') copied('Link')
  203. if (type === 'delete') setModal(true)
  204. setAnchorEl(null)
  205. setSelected(false)
  206. }
  207. const handleDeleteModal = (e: any) => {
  208. const id = e.target.id
  209. if (id === 'overlay' || id === 'cancel') return setModal(false)
  210. if (id === 'delete') {
  211. removeMessageById(_id)
  212. setModal(false)
  213. }
  214. }
  215. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  216. e.preventDefault()
  217. setAnchorEl(e.currentTarget)
  218. setSelected(true)
  219. }
  220. return (
  221. <div className={classes.container}>
  222. {read&&<div className={classes.overlay} id='overlay' onClick={handleCloseRead}>
  223. <FileViewer
  224. allowFullScreen={true}
  225. fileType={type}
  226. filePath={url}
  227. onError={handleCloseRead}
  228. />
  229. </div>}
  230. <div onContextMenu={(e) => handleContextMenu(e)}
  231. className={selected? classes.wrapperActive:classes.wrapper}>
  232. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  233. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  234. <a href={url} target="_blank" rel="noreferrer" download>
  235. <IconButton className={classes.bntDownload} >
  236. <FileDownloadIcon fontSize='medium'/>
  237. </IconButton>
  238. </a>
  239. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  240. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  241. anchorEl={anchorEl} open={open} onClose={handleClose}>
  242. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  243. <MenuItem>
  244. <ContentCopyIcon />
  245. Copy File link
  246. </MenuItem>
  247. </CopyToClipboard>
  248. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  249. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  250. Delete message
  251. </MenuItem>
  252. </StyledMenu>
  253. {modal &&
  254. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  255. <div className={classes.modalDelete}>
  256. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  257. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  258. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  259. DELETE MESSAGE
  260. </Button>
  261. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  262. CANCEL
  263. </Button>
  264. </div>
  265. </div>}
  266. </div>
  267. </div>
  268. )};
  269. export default MessageLeftFile