index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-end",
  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: '#deffa9',
  66. borderRadius: 7,
  67. "&:after": {
  68. content: "''",
  69. position: "absolute",
  70. width: "0",
  71. height: "0",
  72. borderBottom: "15px solid #deffa9",
  73. borderLeft: "15px solid transparent",
  74. borderRight: "15px solid transparent",
  75. bottom: "0",
  76. right: "-15px"
  77. },
  78. "&:before": {
  79. content: "''",
  80. position: "absolute",
  81. width: "0",
  82. height: "0",
  83. borderBottom: "17px solid #deffa9",
  84. borderLeft: "16px solid transparent",
  85. borderRight: "16px solid transparent",
  86. bottom: "0px",
  87. right: "-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: '#ced8d7',
  99. borderRadius: 7,
  100. "&:after": {
  101. content: "''",
  102. position: "absolute",
  103. width: "0",
  104. height: "0",
  105. borderBottom: "15px solid #ced8d7",
  106. borderLeft: "15px solid transparent",
  107. borderRight: "15px solid transparent",
  108. bottom: "0",
  109. right: "-15px"
  110. },
  111. "&:before": {
  112. content: "''",
  113. position: "absolute",
  114. width: "0",
  115. height: "0",
  116. borderBottom: "17px solid #ced8d7",
  117. borderLeft: "16px solid transparent",
  118. borderRight: "16px solid transparent",
  119. bottom: "0px",
  120. right: "-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. caption: {
  152. position: "absolute",
  153. fontSize: ".65em",
  154. fontWeight:600,
  155. bottom: 0,
  156. left: 6,
  157. color: '#000000',
  158. padding: 3,
  159. borderRadius: 5,
  160. },
  161. overlay: {
  162. position: 'absolute',
  163. top: 0,
  164. left: 0,
  165. width: '100%',
  166. height:'100%',
  167. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  168. border:'solid 2px #0084ff',
  169. cursor: 'pointer',
  170. zIndex:100
  171. },
  172. modalDelete: {
  173. background: '#ffffff',
  174. position: 'absolute',
  175. content:'',
  176. width: '20%',
  177. height:'auto',
  178. left: '40%',
  179. bottom: '48.5%',
  180. borderRadius: 10,
  181. padding: 10,
  182. display: 'flex',
  183. flexDirection:'column'
  184. },
  185. overlayDelete: {
  186. position: 'fixed',
  187. top: 0,
  188. left: 0,
  189. width: '100vw',
  190. height: '100vh',
  191. zIndex: 100,
  192. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  193. overflowY: 'hidden',
  194. },
  195. });
  196. interface IMessageRightFile {
  197. url:string,
  198. createdAt: string,
  199. type: string,
  200. caption: string,
  201. _id:string
  202. }
  203. const MessageRightFile = ({ url,createdAt,type,caption,_id }:IMessageRightFile) => {
  204. const classes = useStyles();
  205. const [read, setRead] = useState<boolean>(false)
  206. const [anchorEl, setAnchorEl] = useState<any>(null);
  207. const [selected, setSelected] = useState<boolean>(false);
  208. const [modal,setModal] = useState<boolean>(false)
  209. const open = Boolean(anchorEl);
  210. const handleOpenRead = () => !read&&setRead(true)
  211. const handleCloseRead = (e: any) => {
  212. console.log(e)
  213. read && setRead(false)
  214. }
  215. const handleClose = (type: string | undefined): void => {
  216. if (type === 'copy') copied('Link')
  217. if (type === 'delete') setModal(true)
  218. setAnchorEl(null)
  219. setSelected(false)
  220. }
  221. const handleDeleteModal = (e: any) => {
  222. const id = e.target.id
  223. if (id === 'overlay' || id === 'cancel') return setModal(false)
  224. if (id === 'delete') {
  225. removeMessageById(_id)
  226. setModal(false)
  227. }
  228. }
  229. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  230. e.preventDefault()
  231. setAnchorEl(e.currentTarget)
  232. setSelected(true)
  233. }
  234. return (
  235. <div className={classes.container}>
  236. {read&&<div className={classes.overlay} id='overlay' onClick={handleCloseRead}>
  237. <FileViewer
  238. allowFullScreen={true}
  239. fileType={type}
  240. filePath={url}
  241. onError={handleCloseRead}
  242. />
  243. </div>}
  244. <div onContextMenu={(e) => handleContextMenu(e)}
  245. className={selected? classes.wrapperActive:classes.wrapper}>
  246. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  247. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  248. <a href={url} target="_blank" rel="noreferrer" download>
  249. <IconButton className={classes.bntDownload}>
  250. <FileDownloadIcon fontSize='medium'/>
  251. </IconButton>
  252. </a>
  253. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  254. <div className={classes.caption}>{caption}</div>
  255. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  256. anchorEl={anchorEl} open={open} onClose={handleClose}>
  257. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  258. <MenuItem>
  259. <ContentCopyIcon />
  260. Copy File link
  261. </MenuItem>
  262. </CopyToClipboard>
  263. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  264. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  265. Delete message
  266. </MenuItem>
  267. </StyledMenu>
  268. {modal &&
  269. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  270. <div className={classes.modalDelete}>
  271. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  272. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  273. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  274. DELETE MESSAGE
  275. </Button>
  276. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  277. CANCEL
  278. </Button>
  279. </div>
  280. </div>}
  281. </div>
  282. </div>
  283. )};
  284. export default MessageRightFile