index.tsx 14 KB

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