index.tsx 10 KB

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