index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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-end",
  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: '#deffa9',
  67. borderRadius: 7,
  68. "&:after": {
  69. content: "''",
  70. position: "absolute",
  71. width: "0",
  72. height: "0",
  73. borderBottom: "15px solid #deffa9",
  74. borderLeft: "15px solid transparent",
  75. borderRight: "15px solid transparent",
  76. bottom: "0",
  77. right: "-15px"
  78. },
  79. "&:before": {
  80. content: "''",
  81. position: "absolute",
  82. width: "0",
  83. height: "0",
  84. borderBottom: "17px solid #deffa9",
  85. borderLeft: "16px solid transparent",
  86. borderRight: "16px solid transparent",
  87. bottom: "0px",
  88. right: "-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: '#ced8d7',
  100. borderRadius: 7,
  101. "&:after": {
  102. content: "''",
  103. position: "absolute",
  104. width: "0",
  105. height: "0",
  106. borderBottom: "15px solid #ced8d7",
  107. borderLeft: "15px solid transparent",
  108. borderRight: "15px solid transparent",
  109. bottom: "0",
  110. right: "-15px"
  111. },
  112. "&:before": {
  113. content: "''",
  114. position: "absolute",
  115. width: "0",
  116. height: "0",
  117. borderBottom: "17px solid #ced8d7",
  118. borderLeft: "16px solid transparent",
  119. borderRight: "16px solid transparent",
  120. bottom: "0px",
  121. right: "-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. left: -40,
  202. },
  203. emojiCompanionTitle: {
  204. position: "absolute",
  205. fontSize: "1.7em",
  206. fontWeight:600,
  207. bottom: '2rem',
  208. left: -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 IMessageRightFile {
  234. url:string,
  235. createdAt: string,
  236. type: string,
  237. caption: string,
  238. emoji: string,
  239. emojiCompanion: string,
  240. _id:string
  241. }
  242. const MessageRightFile = ({ url,createdAt,type,caption,emoji,emojiCompanion,_id }:IMessageRightFile) => {
  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 = (e: any) => {
  252. console.log(e)
  253. read && setRead(false)
  254. }
  255. const handleClose = (type: string | undefined): void => {
  256. if (type === 'copy') copied('Link')
  257. if (type === 'delete') setModal(true)
  258. setAnchorEl(null)
  259. setSelected(false)
  260. }
  261. const handleDeleteModal = (e: any) => {
  262. const id = e.target.id
  263. if (id === 'overlay' || id === 'cancel') return setModal(false)
  264. if (id === 'delete') {
  265. removeMessageById(_id)
  266. setModal(false)
  267. }
  268. }
  269. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  270. e.preventDefault()
  271. setAnchorEl(e.currentTarget)
  272. setSelected(true)
  273. }
  274. const handleEmojiMenu = ({ target }: any): void => {
  275. const idEmoji = target.id
  276. if (idEmoji === emoji) {
  277. updateMessageById(_id,'')
  278. setEmojiIndex(null)
  279. } else {
  280. updateMessageById(_id,idEmoji)
  281. setEmojiIndex(idEmoji)
  282. }
  283. }
  284. return (
  285. <div className={classes.container}>
  286. {read&&<div className={classes.overlay} id='overlay' onClick={handleCloseRead}>
  287. <FileViewer
  288. allowFullScreen={true}
  289. fileType={type}
  290. filePath={url}
  291. onError={handleCloseRead}
  292. />
  293. </div>}
  294. <div onContextMenu={(e) => handleContextMenu(e)}
  295. className={selected? classes.wrapperActive:classes.wrapper}>
  296. <InsertDriveFileIcon fontSize='large' style={{ color: '#99b401' }} />
  297. {!read && <span className={classes.title} onClick={handleOpenRead}>Read File</span>}
  298. <a href={url} target="_blank" rel="noreferrer" download>
  299. <IconButton className={classes.bntDownload}>
  300. <FileDownloadIcon fontSize='medium'/>
  301. </IconButton>
  302. </a>
  303. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  304. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  305. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  306. <div className={classes.caption}>{caption}</div>
  307. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  308. anchorEl={anchorEl} open={open} onClose={handleClose}>
  309. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  310. {emojisArr.map((el:string, i:number) =>
  311. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  312. </MenuItem>
  313. <Divider/>
  314. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  315. <MenuItem>
  316. <ContentCopyIcon />
  317. Copy File link
  318. </MenuItem>
  319. </CopyToClipboard>
  320. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  321. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  322. Delete message
  323. </MenuItem>
  324. </StyledMenu>
  325. {modal &&
  326. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  327. <div className={classes.modalDelete}>
  328. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  329. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  330. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  331. DELETE MESSAGE
  332. </Button>
  333. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  334. CANCEL
  335. </Button>
  336. </div>
  337. </div>}
  338. </div>
  339. </div>
  340. )};
  341. export default MessageRightFile