index.tsx 12 KB

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