index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 { CopyToClipboard } from 'react-copy-to-clipboard';
  14. import { removeMessageById } from "../../../../../../api-data";
  15. import { timeStampMessage,handleDownload,copied } from '../../../../../../helpers'
  16. const StyledMenu = styled((props:any) => (
  17. <Menu
  18. elevation={0}
  19. anchorOrigin={{
  20. vertical: 'top',
  21. horizontal: 'right',
  22. }}
  23. transformOrigin={{
  24. vertical: 'bottom',
  25. horizontal: 'right',
  26. }}
  27. {...props}
  28. />
  29. ))(({ theme }:any) => ({
  30. '& .MuiPaper-root': {
  31. borderRadius: 10,
  32. marginTop: theme.spacing(0),
  33. minWidth: 220,
  34. color:
  35. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  36. boxShadow:
  37. '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',
  38. '& .MuiMenu-list': {
  39. padding: '4px 4px',
  40. },
  41. '& .MuiMenuItem-root': {
  42. marginBottom: theme.spacing(1),
  43. '& .MuiSvgIcon-root': {
  44. fontSize: 21,
  45. color: theme.palette.text.secondary,
  46. marginRight: theme.spacing(2),
  47. }
  48. },
  49. },
  50. }));
  51. const useStyles = makeStyles({
  52. container: {
  53. display: "flex",
  54. justifyContent: "flex-start",
  55. marginBottom:15
  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: '#ffffff',
  65. borderRadius: 7,
  66. "&:after": {
  67. content: "''",
  68. position: "absolute",
  69. width: "0",
  70. height: "0",
  71. borderBottom: "15px solid #ffffff",
  72. borderLeft: "15px solid transparent",
  73. borderRight: "15px solid transparent",
  74. bottom: '0px',
  75. left: "-15px"
  76. },
  77. "&:before": {
  78. content: "''",
  79. position: "absolute",
  80. width: "0",
  81. height: "0",
  82. borderBottom: "17px solid #ffffff",
  83. borderLeft: "16px solid transparent",
  84. borderRight: "16px solid transparent",
  85. bottom: "0px",
  86. left: "-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: '#babdbc',
  97. borderRadius: 7,
  98. "&:after": {
  99. content: "''",
  100. position: "absolute",
  101. width: "0",
  102. height: "0",
  103. borderBottom: "15px solid #babdbc",
  104. borderLeft: "15px solid transparent",
  105. borderRight: "15px solid transparent",
  106. bottom: '0px',
  107. left: "-15px"
  108. },
  109. "&:before": {
  110. content: "''",
  111. position: "absolute",
  112. width: "0",
  113. height: "0",
  114. borderBottom: "17px solid #babdbc",
  115. borderLeft: "16px solid transparent",
  116. borderRight: "16px solid transparent",
  117. bottom: "0px",
  118. left: "-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: 0,
  140. right: 6,
  141. color: '#414141',
  142. padding: 3,
  143. borderRadius: 5,
  144. },
  145. caption: {
  146. position: "absolute",
  147. fontSize: ".65em",
  148. fontWeight:600,
  149. bottom: 0,
  150. left: 6,
  151. color: '#000000',
  152. padding: 3,
  153. borderRadius: 5,
  154. },
  155. modalDelete: {
  156. background: '#ffffff',
  157. position: 'absolute',
  158. content:'',
  159. width: '20%',
  160. height:'auto',
  161. left: '40%',
  162. bottom: '48.5%',
  163. borderRadius: 10,
  164. padding: 10,
  165. display: 'flex',
  166. flexDirection:'column'
  167. },
  168. overlay: {
  169. position: 'fixed',
  170. top: 0,
  171. left: 0,
  172. width: '100vw',
  173. height: '100vh',
  174. zIndex: 100,
  175. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  176. overflowY: 'hidden',
  177. },
  178. });
  179. interface IMessageLeftAudio {
  180. url:string,
  181. createdAt: string,
  182. fullType: string,
  183. caption :string,
  184. _id:string
  185. }
  186. const MessageLeftAudio = ({ url,createdAt,fullType,caption,_id }:IMessageLeftAudio) => {
  187. const classes = useStyles();
  188. const [anchorEl, setAnchorEl] = useState<any>(null);
  189. const [selected, setSelected] = useState<boolean>(false);
  190. const [modal,setModal] = useState<boolean>(false)
  191. const open = Boolean(anchorEl);
  192. const handleClose = (type: string | undefined): void => {
  193. if (type === 'copy') copied('Link')
  194. if (type === 'delete') setModal(true)
  195. setAnchorEl(null)
  196. setSelected(false)
  197. }
  198. const handleDeleteModal = (e: any) => {
  199. const id = e.target.id
  200. if (id === 'overlay' || id === 'cancel') return setModal(false)
  201. if (id === 'delete') {
  202. removeMessageById(_id)
  203. setModal(false)
  204. }
  205. }
  206. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  207. e.preventDefault()
  208. setAnchorEl(e.currentTarget)
  209. setSelected(true)
  210. }
  211. return (
  212. <div className={classes.container}>
  213. <div onContextMenu={(e) => handleContextMenu(e)}
  214. className={selected? classes.wrapperActive:classes.wrapper}>
  215. <AudioFileIcon fontSize='large' style={{ color:'#0294c0'}}/>
  216. <ReactAudioPlayer className={classes.player}
  217. src={url}
  218. controls
  219. />
  220. <IconButton onClick={() => handleDownload(url, fullType)} className={classes.bntDownload} >
  221. <FileDownloadIcon fontSize='medium'/>
  222. </IconButton>
  223. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  224. <div className={classes.caption}>{caption}</div>
  225. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  226. anchorEl={anchorEl} open={open} onClose={handleClose}>
  227. <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
  228. <MenuItem>
  229. <ContentCopyIcon />
  230. Copy Audio link
  231. </MenuItem>
  232. </CopyToClipboard>
  233. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  234. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  235. Delete message
  236. </MenuItem>
  237. </StyledMenu>
  238. {modal &&
  239. <div onClick={handleDeleteModal} className={classes.overlay} id='overlay'>
  240. <div className={classes.modalDelete}>
  241. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  242. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  243. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  244. DELETE MESSAGE
  245. </Button>
  246. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  247. CANCEL
  248. </Button>
  249. </div>
  250. </div>}
  251. </div>
  252. </div>
  253. )};
  254. export default MessageLeftAudio