index.tsx 15 KB

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