index.tsx 11 KB

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