index.tsx 13 KB

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