index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import { styled } from '@mui/material/styles';
  3. import { useState } from "react";
  4. import { IconButton } from "@material-ui/core";
  5. import AudioFileIcon from '@mui/icons-material/AudioFile';
  6. import FileDownloadIcon from '@mui/icons-material/FileDownload';
  7. import ReactAudioPlayer from 'react-audio-player';
  8. import Button from '@mui/material/Button';
  9. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  10. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  11. import Menu from '@mui/material/Menu';
  12. import MenuItem from '@mui/material/MenuItem';
  13. import Divider from '@mui/material/Divider';
  14. import CheckBoxIcon from '@mui/icons-material/CheckBox';
  15. import Checkbox from '@mui/material/Checkbox';
  16. import PushPinIcon from '@mui/icons-material/PushPin';
  17. import CloseIcon from '@mui/icons-material/Close';
  18. import ReplyIcon from '@mui/icons-material/Reply';
  19. import { CopyToClipboard } from 'react-copy-to-clipboard';
  20. import { removeMessageById,updateMessageById,pinMessageById } from "../../../../../../api-data";
  21. import { timeStampMessage,handleDownload,copied,emojisArr,slicedWord } from '../../../../../../helpers'
  22. const StyledMenu = styled((props:any) => (
  23. <Menu
  24. elevation={0}
  25. anchorOrigin={{
  26. vertical: 'top',
  27. horizontal: 'right',
  28. }}
  29. transformOrigin={{
  30. vertical: 'bottom',
  31. horizontal: 'right',
  32. }}
  33. {...props}
  34. />
  35. ))(({ theme }:any) => ({
  36. '& .MuiPaper-root': {
  37. borderRadius: 10,
  38. marginTop: theme.spacing(0),
  39. minWidth: 220,
  40. color:
  41. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  42. boxShadow:
  43. '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',
  44. '& .MuiMenu-list': {
  45. padding: '4px 4px',
  46. },
  47. '& .MuiMenuItem-root': {
  48. marginBottom: theme.spacing(1),
  49. '& .MuiSvgIcon-root': {
  50. fontSize: 21,
  51. color: theme.palette.text.secondary,
  52. marginRight: theme.spacing(2),
  53. }
  54. },
  55. },
  56. }));
  57. const useStyles = makeStyles({
  58. container: {
  59. display: "flex",
  60. alignItems: 'flex-start',
  61. alignContent: 'flex-start',
  62. flexDirection:'column',
  63. borderRadius: 7,
  64. position: 'relative',
  65. marginBottom: 5,
  66. padding:'10px 0px 10px 22px'
  67. },
  68. wrapper: {
  69. position: 'relative',
  70. display: 'flex',
  71. justifyContent: 'space-between',
  72. alignContent: 'center',
  73. alignItems: 'center',
  74. padding: '12px 5px 15px 5px',
  75. backgroundColor: '#ffffff',
  76. borderRadius: 7,
  77. "&:after": {
  78. content: "''",
  79. position: "absolute",
  80. width: "0",
  81. height: "0",
  82. borderBottom: "15px solid #ffffff",
  83. borderLeft: "15px solid transparent",
  84. borderRight: "15px solid transparent",
  85. bottom: '0px',
  86. left: "-15px"
  87. },
  88. "&:before": {
  89. content: "''",
  90. position: "absolute",
  91. width: "0",
  92. height: "0",
  93. borderBottom: "17px solid #ffffff",
  94. borderLeft: "16px solid transparent",
  95. borderRight: "16px solid transparent",
  96. bottom: "0px",
  97. left: "-17px"
  98. }
  99. },
  100. wrapperActive: {
  101. position: 'relative',
  102. display: 'flex',
  103. justifyContent: 'space-between',
  104. alignContent: 'center',
  105. alignItems: 'center',
  106. padding: '12px 5px 15px 5px',
  107. backgroundColor: '#babdbc',
  108. borderRadius: 7,
  109. "&:after": {
  110. content: "''",
  111. position: "absolute",
  112. width: "0",
  113. height: "0",
  114. borderBottom: "15px solid #babdbc",
  115. borderLeft: "15px solid transparent",
  116. borderRight: "15px solid transparent",
  117. bottom: '0px',
  118. left: "-15px"
  119. },
  120. "&:before": {
  121. content: "''",
  122. position: "absolute",
  123. width: "0",
  124. height: "0",
  125. borderBottom: "17px solid #babdbc",
  126. borderLeft: "16px solid transparent",
  127. borderRight: "16px solid transparent",
  128. bottom: "0px",
  129. left: "-17px"
  130. }
  131. },
  132. bntDownload: {
  133. backgroundColor: 'inherit',
  134. color: '#54b0fc',
  135. width: 30,
  136. height:30,
  137. '&:hover': {
  138. backgroundColor: '#54b0fc',
  139. color:'#ffffff'
  140. }
  141. },
  142. player: {
  143. margin: '0 5px 0 5px',
  144. borderRadius:7
  145. },
  146. time: {
  147. position: "absolute",
  148. fontSize: ".65em",
  149. fontWeight:600,
  150. bottom: 0,
  151. right: 6,
  152. color: '#414141',
  153. padding: 3,
  154. borderRadius: 5,
  155. },
  156. captionWrapper: {
  157. position: 'relative',
  158. fontSize: ".85em",
  159. color: '#414141',
  160. maxWidth: 410,
  161. fontWeight:600,
  162. borderRadius: 5,
  163. marginLeft:10,
  164. wordBreak:'break-word',
  165. textAlign: "left",
  166. font: "400 .9em 'Open Sans', sans-serif",
  167. backgroundColor: '#deffa9',
  168. padding:10,
  169. "&:after": {
  170. content: "''",
  171. position: "absolute",
  172. width: "0",
  173. height: "0",
  174. borderBottom: "15px solid #deffa9",
  175. borderLeft: "15px solid transparent",
  176. borderRight: "15px solid transparent",
  177. bottom: "0px",
  178. left: "-15px"
  179. },
  180. "&:before": {
  181. content: "''",
  182. position: "absolute",
  183. width: "0",
  184. height: "0",
  185. borderBottom: "17px solid #deffa9",
  186. borderLeft: "16px solid transparent",
  187. borderRight: "16px solid transparent",
  188. bottom: "0px",
  189. left: "-17px"
  190. }
  191. },
  192. modalDelete: {
  193. background: '#ffffff',
  194. position: 'absolute',
  195. content:'',
  196. width: '20%',
  197. height:'auto',
  198. left: '40%',
  199. bottom: '48.5%',
  200. borderRadius: 10,
  201. padding: 10,
  202. display: 'flex',
  203. flexDirection:'column'
  204. },
  205. overlay: {
  206. position: 'fixed',
  207. top: 0,
  208. left: 0,
  209. width: '100vw',
  210. height: '100vh',
  211. zIndex: 100,
  212. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  213. overflowY: 'hidden',
  214. },
  215. emojiTitle: {
  216. position: "absolute",
  217. fontSize: "1.7em",
  218. fontWeight:600,
  219. bottom: '0.2rem',
  220. right: -40,
  221. },
  222. emojiCompanionTitle: {
  223. position: "absolute",
  224. fontSize: "1.7em",
  225. fontWeight:600,
  226. bottom: '2.2rem',
  227. right: -40,
  228. },
  229. emoji: {
  230. cursor: 'pointer',
  231. fontSize: '1.7rem',
  232. transition: 'all 0.3s',
  233. '&:hover': {
  234. transform: 'scale(1.5)'
  235. }
  236. },
  237. emojiActive: {
  238. cursor: 'pointer',
  239. fontSize: '1.2rem',
  240. animation: `$emoji 0.6s ease-out`,
  241. animationDirection: 'forwards',
  242. animationIterationCount: 1,
  243. },
  244. '@keyframes emoji': {
  245. '5%': { transform: 'translateY(1rem)'},
  246. '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
  247. '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
  248. '80%': {opacity: 0},
  249. '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  250. },
  251. iconClose: {
  252. '&:hover': {
  253. transform: 'rotate(180deg)',
  254. transition: 'all 250ms ease-out ',
  255. }
  256. },
  257. checkboxSelect: {
  258. position: 'absolute',
  259. left: -64,
  260. bottom: -10,
  261. pointerEvents: 'auto'
  262. }
  263. });
  264. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  265. interface IMessageLeftAudio {
  266. url:string,
  267. createdAt: string,
  268. fullType: 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. }
  281. const MessageLeftAudio = ({ url,createdAt,fullType,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,nightMode,handleReply,handleForward }:IMessageLeftAudio) => {
  282. const classes = useStyles();
  283. const [anchorEl, setAnchorEl] = useState<any>(null);
  284. const [selected, setSelected] = useState<boolean>(false);
  285. const [modal,setModal] = useState<boolean>(false)
  286. const open = Boolean(anchorEl);
  287. const checked = isSelected(_id)
  288. const handleClose = (type: string | undefined): void => {
  289. if (type === 'copy') copied('Link')
  290. if (type === 'delete') setModal(true)
  291. setAnchorEl(null)
  292. setSelected(false)
  293. }
  294. const handleDeleteModal = (e: any) => {
  295. const id = e.target.id
  296. if (id === 'overlay' || id === 'cancel') return setModal(false)
  297. if (id === 'delete') {
  298. removeMessageById(_id)
  299. setModal(false)
  300. }
  301. }
  302. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  303. e.preventDefault()
  304. setAnchorEl(e.currentTarget)
  305. setSelected(true)
  306. }
  307. const handleEmojiMenu = ({ target }: any): void => {
  308. const idEmoji = target.id
  309. if (idEmoji === emoji) {
  310. updateMessageById(_id, '')
  311. } else updateMessageById(_id,idEmoji)
  312. }
  313. return (
  314. <div className={classes.container}>
  315. <div onContextMenu={(e) => handleContextMenu(e)}
  316. className={selected ? classes.wrapperActive : classes.wrapper}
  317. style={{ pointerEvents: isSomeSelected ? 'none' : 'auto' }}>
  318. <AudioFileIcon fontSize='large' style={{ color:'#0294c0'}}/>
  319. <ReactAudioPlayer className={classes.player}
  320. src={url} controls
  321. />
  322. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload} >
  323. <FileDownloadIcon fontSize='medium'/>
  324. </IconButton>
  325. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  326. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  327. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  328. {isSomeSelected && <div className={classes.checkboxSelect}><Checkbox {...label} checked={checked} sx={{ color: nightMode ? '#ffffff' : '#00ff48', '&.Mui-checked': { color: nightMode ? '#ffffff' : '#00ff48' } }}
  329. onClick={() => handleSelected(_id)}/></div>}
  330. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  331. anchorEl={anchorEl} open={open} onClose={handleClose}>
  332. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  333. {emojisArr.map((el:string, i:number) =>
  334. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  335. </MenuItem>
  336. <Divider />
  337. <MenuItem onClick={() => {
  338. handleReply(_id)
  339. handleClose(undefined)
  340. }}>
  341. <ReplyIcon />
  342. Reply
  343. </MenuItem>
  344. <MenuItem onClick={() => {
  345. handleForward(_id)
  346. handleClose(undefined)
  347. }}>
  348. <ReplyIcon style={{transform :'rotateY(180deg)'}} />
  349. Forward
  350. </MenuItem>
  351. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  352. <MenuItem>
  353. <ContentCopyIcon />
  354. Copy Link
  355. </MenuItem>
  356. </CopyToClipboard>
  357. <MenuItem onClick={() => {
  358. pinMessageById(_id, !pinned)
  359. handleClose(undefined)
  360. }}>
  361. {pinned ?
  362. <CloseIcon className={classes.iconClose} /> :
  363. <PushPinIcon />}
  364. {pinned?'Unpin':'Pin'}
  365. </MenuItem>
  366. <MenuItem onClick={() => {
  367. handleSelected(_id)
  368. handleClose(undefined)
  369. }}>
  370. <CheckBoxIcon />
  371. Select
  372. </MenuItem>
  373. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  374. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  375. Delete
  376. </MenuItem>
  377. </StyledMenu>
  378. {modal &&
  379. <div onClick={handleDeleteModal} className={classes.overlay} id='overlay'>
  380. <div className={classes.modalDelete}>
  381. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  382. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  383. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  384. DELETE MESSAGE
  385. </Button>
  386. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  387. CANCEL
  388. </Button>
  389. </div>
  390. </div>}
  391. </div>
  392. {caption&&<div className={classes.captionWrapper}>{caption}</div>}
  393. </div>
  394. )};
  395. export default MessageLeftAudio