index.tsx 12 KB

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