index.tsx 9.9 KB

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