index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. },
  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 22px 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 22px 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. captionWrapper: {
  152. position: "absolute",
  153. fontSize: ".85em",
  154. color: '#414141',
  155. maxWidth:'76%',
  156. fontWeight:600,
  157. bottom: -28,
  158. left: 12,
  159. borderRadius: 5,
  160. wordBreak:'break-word',
  161. textAlign: "left",
  162. font: "400 .9em 'Open Sans', sans-serif",
  163. backgroundColor: '#deffa9',
  164. padding:10,
  165. "&:after": {
  166. content: "''",
  167. position: "absolute",
  168. width: "0",
  169. height: "0",
  170. borderBottom: "15px solid #deffa9",
  171. borderLeft: "15px solid transparent",
  172. borderRight: "15px solid transparent",
  173. bottom: '0px',
  174. left: "-15px"
  175. },
  176. "&:before": {
  177. content: "''",
  178. position: "absolute",
  179. width: "0",
  180. height: "0",
  181. borderBottom: "17px solid #deffa9",
  182. borderLeft: "16px solid transparent",
  183. borderRight: "16px solid transparent",
  184. bottom: "-1px",
  185. left: "-17px"
  186. }
  187. },
  188. overlay: {
  189. position: 'absolute',
  190. top: 0,
  191. left: 0,
  192. width: '100%',
  193. height:'100%',
  194. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  195. border:'solid 2px #0084ff',
  196. cursor: 'pointer',
  197. zIndex:100
  198. },
  199. modalDelete: {
  200. background: '#ffffff',
  201. position: 'absolute',
  202. content:'',
  203. width: '20%',
  204. height:'auto',
  205. left: '40%',
  206. bottom: '48.5%',
  207. borderRadius: 10,
  208. padding: 10,
  209. display: 'flex',
  210. flexDirection:'column'
  211. },
  212. overlayDelete: {
  213. position: 'fixed',
  214. top: 0,
  215. left: 0,
  216. width: '100vw',
  217. height: '100vh',
  218. zIndex: 100,
  219. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  220. overflowY: 'hidden',
  221. },
  222. emojiTitle: {
  223. position: "absolute",
  224. fontSize: "1.7em",
  225. fontWeight:600,
  226. bottom: 0,
  227. right: -40,
  228. },
  229. emojiCompanionTitle: {
  230. position: "absolute",
  231. fontSize: "1.7em",
  232. fontWeight:600,
  233. bottom: '2rem',
  234. right: -40,
  235. },
  236. emoji: {
  237. cursor: 'pointer',
  238. fontSize: '1.7rem',
  239. transition: 'all 0.3s',
  240. '&:hover': {
  241. transform: 'scale(1.5)'
  242. }
  243. },
  244. emojiActive: {
  245. cursor: 'pointer',
  246. fontSize: '1.2rem',
  247. animation: `$emoji 0.6s ease-out`,
  248. animationDirection: 'forwards',
  249. animationIterationCount: 1,
  250. },
  251. '@keyframes emoji': {
  252. '5%': { transform: 'translateY(1rem)'},
  253. '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
  254. '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
  255. '80%': {opacity: 0},
  256. '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  257. },
  258. });
  259. interface IMessageLeftFile {
  260. url:string,
  261. createdAt: string,
  262. type: string,
  263. caption: string,
  264. emoji: string,
  265. emojiCompanion: string,
  266. _id:string
  267. }
  268. const MessageLeftFile = ({ url,createdAt,type,caption,emoji,emojiCompanion,_id }:IMessageLeftFile) => {
  269. const classes = useStyles();
  270. const [read, setRead] = useState<boolean>(false)
  271. const [anchorEl, setAnchorEl] = useState<any>(null);
  272. const [selected, setSelected] = useState<boolean>(false);
  273. const [modal,setModal] = useState<boolean>(false)
  274. const open = Boolean(anchorEl);
  275. const handleOpenRead = () => !read&&setRead(true)
  276. const handleCloseRead = () => read && setRead(false)
  277. const handleClose = (type: string | undefined): void => {
  278. if (type === 'copy') copied('Link')
  279. if (type === 'delete') setModal(true)
  280. setAnchorEl(null)
  281. setSelected(false)
  282. }
  283. const handleDeleteModal = (e: any) => {
  284. const id = e.target.id
  285. if (id === 'overlay' || id === 'cancel') return setModal(false)
  286. if (id === 'delete') {
  287. removeMessageById(_id)
  288. setModal(false)
  289. }
  290. }
  291. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  292. e.preventDefault()
  293. setAnchorEl(e.currentTarget)
  294. setSelected(true)
  295. }
  296. const handleEmojiMenu = ({ target }: any): void => {
  297. const idEmoji = target.id
  298. if (idEmoji === emoji) {updateMessageById(_id,'')
  299. } else updateMessageById(_id,idEmoji)
  300. }
  301. return (
  302. <div className={classes.container} style={{marginBottom:caption?43:15}}>
  303. {read&&<div className={classes.overlay} id='overlay' onClick={handleCloseRead}>
  304. <FileViewer
  305. allowFullScreen={true}
  306. fileType={type}
  307. filePath={url}
  308. onError={handleCloseRead}
  309. />
  310. </div>}
  311. <div onContextMenu={(e) => handleContextMenu(e)}
  312. className={selected? classes.wrapperActive:classes.wrapper}>
  313. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  314. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  315. <a href={url} target="_blank" rel="noreferrer" download>
  316. <IconButton className={classes.bntDownload} >
  317. <FileDownloadIcon fontSize='medium'/>
  318. </IconButton>
  319. </a>
  320. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  321. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  322. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  323. {caption&&<div className={classes.captionWrapper}>{caption}</div>}
  324. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  325. anchorEl={anchorEl} open={open} onClose={handleClose}>
  326. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  327. {emojisArr.map((el:string, i:number) =>
  328. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  329. </MenuItem>
  330. <Divider/>
  331. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  332. <MenuItem>
  333. <ContentCopyIcon />
  334. Copy File link
  335. </MenuItem>
  336. </CopyToClipboard>
  337. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  338. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  339. Delete message
  340. </MenuItem>
  341. </StyledMenu>
  342. {modal &&
  343. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  344. <div className={classes.modalDelete}>
  345. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  346. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  347. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  348. DELETE MESSAGE
  349. </Button>
  350. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  351. CANCEL
  352. </Button>
  353. </div>
  354. </div>}
  355. </div>
  356. </div>
  357. )};
  358. export default MessageLeftFile