index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. informationWrapper: {
  85. display: 'flex',
  86. alignItems: 'center',
  87. alignContent: 'center',
  88. justifyContent: 'flex-end',
  89. width: '100%',
  90. paddingRight:3,
  91. },
  92. time: {
  93. fontSize: ".65em",
  94. fontWeight:600,
  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. top: -10,
  210. pointerEvents: 'auto'
  211. },
  212. avatarIcon: {
  213. position: 'absolute',
  214. right: -54,
  215. bottom: 0,
  216. },
  217. tongueOne: {
  218. content: "''",
  219. position: "absolute",
  220. width: "0",
  221. height: "0",
  222. borderRight: "15px solid transparent",
  223. borderLeft: "15px solid transparent",
  224. bottom: '0px',
  225. right: "-15px",
  226. },
  227. tongueTwo: {
  228. content: "''",
  229. position: "absolute",
  230. width: "0",
  231. height: "0",
  232. borderRight: "16px solid transparent",
  233. borderLeft: "16px solid transparent",
  234. bottom: "0px",
  235. right: "-17px",
  236. },
  237. });
  238. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  239. interface IMessageRightFile {
  240. url: string,
  241. tongue: boolean,
  242. watched: boolean,
  243. edited: boolean,
  244. avatarUrl: string,
  245. color: string,
  246. name: string,
  247. lastName: string,
  248. createdAt: string,
  249. type: string,
  250. caption: string,
  251. emoji: string,
  252. emojiCompanion: string,
  253. pinned: boolean,
  254. isSomeSelected: boolean,
  255. isSelected:(_id:string) => boolean,
  256. handleSelected: (_id:string) => void,
  257. _id: string,
  258. nightMode: boolean,
  259. handleReply: (_id: string) => void,
  260. handleForward: (_id: string) => void,
  261. handleEdit:(_id: string) => void,
  262. }
  263. 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) => {
  264. const classes = useStyles();
  265. const [read, setRead] = useState<boolean>(false)
  266. const [anchorEl, setAnchorEl] = useState<any>(null);
  267. const [selected, setSelected] = useState<boolean>(false);
  268. const [scale, setScale] = useState<number>(1)
  269. const [modal,setModal] = useState<boolean>(false)
  270. const open = Boolean(anchorEl);
  271. const checked = isSelected(_id)
  272. const refView = useRef<null | any>(null)
  273. const handleIncreaseScale = () => {
  274. if (scale < 5) {
  275. refView.current.style.transform = `scale(${scale+0.25},${scale+0.25})`
  276. refView.current.style.transformOrigin = `${50 / scale + 0.25}px ${50 / scale + 0.25}px`
  277. setScale(scale+0.25)
  278. }
  279. }
  280. const handleDecreaseScale = () => {
  281. if (scale >= 0.5) {
  282. refView.current.style.transform = `scale(${scale-0.25},${scale-0.25})`
  283. refView.current.style.transformOrigin = `${50/scale-0.25}px ${50/scale-0.25}px`
  284. setScale(scale-0.25)
  285. }
  286. }
  287. const handleOpenRead = () => !read&&setRead(true)
  288. const handleDownloadFile = (e: any) => {
  289. e.stopPropagation()
  290. handleDownload(url, type)
  291. }
  292. const handleCloseRead = (e:any) => {
  293. const id = e.target.id
  294. if (id === 'overlay' || id === 'close') {
  295. setRead(false)
  296. setScale(1)
  297. }
  298. }
  299. const handleClose = (type: string | undefined): void => {
  300. if (type === 'copy') copied('Text')
  301. if (type === 'delete') setModal(true)
  302. setAnchorEl(null)
  303. setSelected(false)
  304. }
  305. const handleDeleteModal = (e: any) => {
  306. const id = e.target.id
  307. if (id === 'overlay' || id === 'cancel') return setModal(false)
  308. if (id === 'delete') {
  309. removeMessageById(_id)
  310. setModal(false)
  311. }
  312. }
  313. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  314. e.preventDefault()
  315. setAnchorEl(e.currentTarget)
  316. setSelected(true)
  317. }
  318. const handleEmojiMenu = ({ target }: any): void => {
  319. const idEmoji = target.id
  320. if (idEmoji === emoji) {updateMessageById(_id,'')
  321. } else updateMessageById(_id,idEmoji)
  322. }
  323. return (
  324. <div className={classes.container} style={{marginBottom:tongue?12:0}}>
  325. {read && <div className={classes.wrapperIcons}>
  326. <ZoomOutIcon onClick={handleDecreaseScale} className={classes.magnifying} fontSize='large' />
  327. <ZoomInIcon onClick={handleIncreaseScale} className={classes.magnifying} fontSize='large' />
  328. <CloseIcon id='close' onClick={handleCloseRead} className={classes.iconCloseOverlay} fontSize='large' />
  329. </div>}
  330. {read && <div className={classes.overlay} id='overlay'
  331. onClick={handleCloseRead}>
  332. <div ref={refView}>
  333. <FileViewer
  334. allowFullScreen={true}
  335. fileType={type}
  336. filePath={url}
  337. onError={handleCloseRead}
  338. />
  339. </div>
  340. </div>}
  341. <div onContextMenu={(e) => handleContextMenu(e)} className={classes.wrapper}
  342. style={{backgroundColor:selected?'#ced8d7':"#deffa9",pointerEvents:isSomeSelected?'none':'auto'}}>
  343. <Typography style={{color: "#26afee",marginBottom:10}} variant="h6" align="right">
  344. {`${firstLetter(name)}${slicedWord(name, 15, 1)}
  345. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  346. </Typography>
  347. {!read &&<Button fullWidth onClick={handleOpenRead} variant="contained" color='warning' style={{fontWeight:500,fontSize:18}}>
  348. Read File
  349. </Button>}
  350. <ListItemText style={{wordBreak:'break-word'}} secondary={caption} secondaryTypographyProps={{color: "#000000"}}/>
  351. <div className={classes.informationWrapper}>
  352. <div className={classes.time} style={{ color: '#18bd03'}}>{`${edited?'edited ':''}${timeStampMessage(createdAt)}`}</div>
  353. {watched ? <DoneAllIcon style={{ color: '#18bd03', marginLeft: 5 }} fontSize='small' /> :
  354. <DoneIcon style={{ color: '#18bd03',marginLeft:5}} fontSize='small' />}
  355. </div>
  356. {tongue&&<div className={classes.avatarIcon}>
  357. <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
  358. sx={{ background: color, width: 40, height: 40 }}>
  359. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  360. </Avatar>
  361. </div>}
  362. {tongue&&<span className={classes.tongueOne} style={{borderBottom: `15px solid ${selected?'#ced8d7':'#deffa9'}`}}></span>}
  363. {tongue&&<span className={classes.tongueTwo} style={{borderBottom: `17px solid ${selected?'#ced8d7':'#deffa9'}`}}></span>}
  364. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  365. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  366. {isSomeSelected && <div className={classes.checkboxSelect}><Checkbox {...label} checked={checked} sx={{ color: nightMode ? '#ffffff' : '#00ff48', '&.Mui-checked': { color: nightMode ? '#ffffff' : '#00ff48' } }}
  367. onClick={() => handleSelected(_id)}/></div>}
  368. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  369. anchorEl={anchorEl} open={open} onClose={handleClose}>
  370. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  371. {emojisArr.map((el:string, i:number) =>
  372. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  373. </MenuItem>
  374. <Divider />
  375. <MenuItem onClick={() => {
  376. handleReply(_id)
  377. handleClose(undefined)
  378. }}>
  379. <ReplyIcon />
  380. Reply
  381. </MenuItem>
  382. <MenuItem onClick={() => {
  383. handleForward(_id)
  384. handleClose(undefined)
  385. }}>
  386. <ReplyIcon style={{transform :'rotateY(180deg)'}} />
  387. Forward
  388. </MenuItem>
  389. <MenuItem onClick={() => {
  390. handleEdit(_id)
  391. handleClose(undefined)
  392. }}>
  393. <EditIcon/>
  394. Edit
  395. </MenuItem>
  396. <MenuItem onClick={handleDownloadFile}>
  397. <DownloadIcon/>
  398. Download
  399. </MenuItem>
  400. {caption&&<CopyToClipboard onCopy={() => handleClose('copy')} text={caption}>
  401. <MenuItem>
  402. <ContentCopyIcon />
  403. Copy Text
  404. </MenuItem>
  405. </CopyToClipboard>}
  406. <MenuItem onClick={() => {
  407. pinMessageById(_id, !pinned)
  408. handleClose(undefined)
  409. }}>
  410. {pinned ?
  411. <CloseIcon className={classes.iconClose} /> :
  412. <PushPinIcon />}
  413. {pinned?'Unpin':'Pin'}
  414. </MenuItem>
  415. <MenuItem onClick={() => {
  416. handleSelected(_id)
  417. handleClose(undefined)
  418. }}>
  419. <CheckBoxIcon />
  420. Select
  421. </MenuItem>
  422. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  423. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  424. Delete
  425. </MenuItem>
  426. </StyledMenu>
  427. {modal &&
  428. <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
  429. <div className={classes.modalDelete}>
  430. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  431. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  432. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  433. DELETE MESSAGE
  434. </Button>
  435. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  436. CANCEL
  437. </Button>
  438. </div>
  439. </div>}
  440. </div>
  441. </div>
  442. )};
  443. export default MessageRightFile