index.tsx 11 KB

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