index.tsx 9.4 KB

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