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