index.tsx 12 KB

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