index.tsx 12 KB

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