index.tsx 14 KB

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