index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 Divider from '@mui/material/Divider';
  13. import { CopyToClipboard } from 'react-copy-to-clipboard';
  14. import { removeMessageById,updateMessageById } from "../../../../../../api-data";
  15. import { timeStampMessage,copied,emojisArr } from '../../../../../../helpers'
  16. const FileViewer = require('react-file-viewer')
  17. const StyledMenu = styled((props:any) => (
  18. <Menu
  19. elevation={0}
  20. anchorOrigin={{
  21. vertical: 'top',
  22. horizontal: 'right',
  23. }}
  24. transformOrigin={{
  25. vertical: 'bottom',
  26. horizontal: 'right',
  27. }}
  28. {...props}
  29. />
  30. ))(({ theme }:any) => ({
  31. '& .MuiPaper-root': {
  32. borderRadius: 10,
  33. marginTop: theme.spacing(0),
  34. minWidth: 220,
  35. color:
  36. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  37. boxShadow:
  38. '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',
  39. '& .MuiMenu-list': {
  40. padding: '4px 4px',
  41. },
  42. '& .MuiMenuItem-root': {
  43. marginBottom: theme.spacing(1),
  44. '& .MuiSvgIcon-root': {
  45. fontSize: 21,
  46. color: theme.palette.text.secondary,
  47. marginRight: theme.spacing(2),
  48. }
  49. },
  50. },
  51. }));
  52. const useStyles = makeStyles({
  53. container: {
  54. display: "flex",
  55. justifyContent: "flex-start",
  56. marginBottom:15
  57. },
  58. wrapper: {
  59. position: 'relative',
  60. display: 'flex',
  61. justifyContent: 'space-between',
  62. alignContent: 'center',
  63. alignItems: 'center',
  64. width: 200,
  65. padding: '5px 5px 12px 5px',
  66. backgroundColor: '#ffffff',
  67. borderRadius: 7,
  68. "&:after": {
  69. content: "''",
  70. position: "absolute",
  71. width: "0",
  72. height: "0",
  73. borderBottom: "15px solid #ffffff",
  74. borderLeft: "15px solid transparent",
  75. borderRight: "15px solid transparent",
  76. bottom: '0px',
  77. left: "-15px"
  78. },
  79. "&:before": {
  80. content: "''",
  81. position: "absolute",
  82. width: "0",
  83. height: "0",
  84. borderBottom: "17px solid #ffffff",
  85. borderLeft: "16px solid transparent",
  86. borderRight: "16px solid transparent",
  87. bottom: "0px",
  88. left: "-17px"
  89. }
  90. },
  91. wrapperActive: {
  92. position: 'relative',
  93. display: 'flex',
  94. justifyContent: 'space-between',
  95. alignContent: 'center',
  96. alignItems: 'center',
  97. width: 200,
  98. padding: '5px 5px 12px 5px',
  99. backgroundColor: '#babdbc',
  100. borderRadius: 7,
  101. "&:after": {
  102. content: "''",
  103. position: "absolute",
  104. width: "0",
  105. height: "0",
  106. borderBottom: "15px solid #babdbc",
  107. borderLeft: "15px solid transparent",
  108. borderRight: "15px solid transparent",
  109. bottom: '0px',
  110. left: "-15px"
  111. },
  112. "&:before": {
  113. content: "''",
  114. position: "absolute",
  115. width: "0",
  116. height: "0",
  117. borderBottom: "17px solid #babdbc",
  118. borderLeft: "16px solid transparent",
  119. borderRight: "16px solid transparent",
  120. bottom: "0px",
  121. left: "-17px"
  122. }
  123. },
  124. bntDownload: {
  125. backgroundColor: 'inherit',
  126. color: '#54b0fc',
  127. width: 30,
  128. height:30,
  129. '&:hover': {
  130. backgroundColor: '#54b0fc',
  131. color:'#ffffff'
  132. }
  133. },
  134. title: {
  135. margin: '0 30px 0 5px',
  136. color: '#0e0d0d',
  137. cursor: 'pointer',
  138. '&:hover': {
  139. color: '#54b0fc',
  140. }
  141. },
  142. time: {
  143. position: "absolute",
  144. fontSize: ".65em",
  145. fontWeight:600,
  146. bottom: 0,
  147. right: 6,
  148. color: '#414141',
  149. padding: 3,
  150. borderRadius: 5,
  151. },
  152. caption: {
  153. position: "absolute",
  154. fontSize: ".65em",
  155. fontWeight:600,
  156. bottom: 0,
  157. left: 6,
  158. color: '#000000',
  159. padding: 3,
  160. borderRadius: 5,
  161. },
  162. overlay: {
  163. position: 'absolute',
  164. top: 0,
  165. left: 0,
  166. width: '100%',
  167. height:'100%',
  168. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  169. border:'solid 2px #0084ff',
  170. cursor: 'pointer',
  171. zIndex:100
  172. },
  173. modalDelete: {
  174. background: '#ffffff',
  175. position: 'absolute',
  176. content:'',
  177. width: '20%',
  178. height:'auto',
  179. left: '40%',
  180. bottom: '48.5%',
  181. borderRadius: 10,
  182. padding: 10,
  183. display: 'flex',
  184. flexDirection:'column'
  185. },
  186. overlayDelete: {
  187. position: 'fixed',
  188. top: 0,
  189. left: 0,
  190. width: '100vw',
  191. height: '100vh',
  192. zIndex: 100,
  193. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  194. overflowY: 'hidden',
  195. },
  196. emojiTitle: {
  197. position: "absolute",
  198. fontSize: "1.7em",
  199. fontWeight:600,
  200. bottom: 0,
  201. right: -40,
  202. },
  203. emojiCompanionTitle: {
  204. position: "absolute",
  205. fontSize: "1.7em",
  206. fontWeight:600,
  207. bottom: '2rem',
  208. right: -40,
  209. },
  210. emoji: {
  211. cursor: 'pointer',
  212. fontSize: '1.7rem',
  213. transition: 'all 0.3s',
  214. '&:hover': {
  215. transform: 'scale(1.5)'
  216. }
  217. },
  218. emojiActive: {
  219. cursor: 'pointer',
  220. fontSize: '1.2rem',
  221. animation: `$emoji 0.6s ease-out`,
  222. animationDirection: 'forwards',
  223. animationIterationCount: 1,
  224. },
  225. '@keyframes emoji': {
  226. '5%': { transform: 'translateY(1rem)'},
  227. '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
  228. '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
  229. '80%': {opacity: 0},
  230. '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  231. },
  232. });
  233. interface IMessageLeftFile {
  234. url:string,
  235. createdAt: string,
  236. type: string,
  237. caption: string,
  238. emoji: string,
  239. emojiCompanion: string,
  240. _id:string
  241. }
  242. const MessageLeftFile = ({ url,createdAt,type,caption,emoji,emojiCompanion,_id }:IMessageLeftFile) => {
  243. const classes = useStyles();
  244. const [read, setRead] = useState<boolean>(false)
  245. const [anchorEl, setAnchorEl] = useState<any>(null);
  246. const [selected, setSelected] = useState<boolean>(false);
  247. const [emojiIndex, setEmojiIndex] = useState<string | null>(null);
  248. const [modal,setModal] = useState<boolean>(false)
  249. const open = Boolean(anchorEl);
  250. const handleOpenRead = () => !read&&setRead(true)
  251. const handleCloseRead = () => read && setRead(false)
  252. const handleClose = (type: string | undefined): void => {
  253. if (type === 'copy') copied('Link')
  254. if (type === 'delete') setModal(true)
  255. setAnchorEl(null)
  256. setSelected(false)
  257. }
  258. const handleDeleteModal = (e: any) => {
  259. const id = e.target.id
  260. if (id === 'overlay' || id === 'cancel') return setModal(false)
  261. if (id === 'delete') {
  262. removeMessageById(_id)
  263. setModal(false)
  264. }
  265. }
  266. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  267. e.preventDefault()
  268. setAnchorEl(e.currentTarget)
  269. setSelected(true)
  270. }
  271. const handleEmojiMenu = ({ target }: any): void => {
  272. const idEmoji = target.id
  273. if (idEmoji === emoji) {
  274. updateMessageById(_id,'')
  275. setEmojiIndex(null)
  276. } else {
  277. updateMessageById(_id,idEmoji)
  278. setEmojiIndex(idEmoji)
  279. }
  280. }
  281. return (
  282. <div className={classes.container}>
  283. {read&&<div className={classes.overlay} id='overlay' onClick={handleCloseRead}>
  284. <FileViewer
  285. allowFullScreen={true}
  286. fileType={type}
  287. filePath={url}
  288. onError={handleCloseRead}
  289. />
  290. </div>}
  291. <div onContextMenu={(e) => handleContextMenu(e)}
  292. className={selected? classes.wrapperActive:classes.wrapper}>
  293. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  294. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  295. <a href={url} target="_blank" rel="noreferrer" download>
  296. <IconButton className={classes.bntDownload} >
  297. <FileDownloadIcon fontSize='medium'/>
  298. </IconButton>
  299. </a>
  300. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  301. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  302. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  303. <div className={classes.caption}>{caption}</div>
  304. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  305. anchorEl={anchorEl} open={open} onClose={handleClose}>
  306. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  307. {emojisArr.map((el:string, i:number) =>
  308. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  309. </MenuItem>
  310. <Divider/>
  311. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  312. <MenuItem>
  313. <ContentCopyIcon />
  314. Copy File link
  315. </MenuItem>
  316. </CopyToClipboard>
  317. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  318. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  319. Delete message
  320. </MenuItem>
  321. </StyledMenu>
  322. {modal &&
  323. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  324. <div className={classes.modalDelete}>
  325. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  326. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  327. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  328. DELETE MESSAGE
  329. </Button>
  330. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  331. CANCEL
  332. </Button>
  333. </div>
  334. </div>}
  335. </div>
  336. </div>
  337. )};
  338. export default MessageLeftFile