index.tsx 9.3 KB

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