index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import { styled } from '@mui/material/styles';
  3. import { useState } from "react";
  4. import Typography from '@mui/material/Typography';
  5. import ListItemText from '@mui/material/ListItemText';
  6. import Button from '@mui/material/Button';
  7. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  8. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  9. import Menu from '@mui/material/Menu';
  10. import MenuItem from '@mui/material/MenuItem';
  11. import Divider from '@mui/material/Divider';
  12. import CheckBoxIcon from '@mui/icons-material/CheckBox';
  13. import Checkbox from '@mui/material/Checkbox';
  14. import PushPinIcon from '@mui/icons-material/PushPin';
  15. import CloseIcon from '@mui/icons-material/Close';
  16. import ReplyIcon from '@mui/icons-material/Reply';
  17. import { CopyToClipboard } from 'react-copy-to-clipboard';
  18. import { firstLetter, slicedWord, timeStampMessage, copied,emojisArr } from '../../../../../../helpers'
  19. import { removeMessageById,updateMessageById,pinMessageById } from "../../../../../../api-data";
  20. const StyledMenu = styled((props:any) => (
  21. <Menu
  22. elevation={0}
  23. anchorOrigin={{
  24. vertical: 'top',
  25. horizontal: 'right',
  26. }}
  27. transformOrigin={{
  28. vertical: 'bottom',
  29. horizontal: 'right',
  30. }}
  31. {...props}
  32. />
  33. ))(({ theme }:any) => ({
  34. '& .MuiPaper-root': {
  35. borderRadius: 10,
  36. marginTop: theme.spacing(0),
  37. minWidth: 220,
  38. color:
  39. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  40. boxShadow:
  41. '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',
  42. '& .MuiMenu-list': {
  43. padding: '4px 4px',
  44. },
  45. '& .MuiMenuItem-root': {
  46. marginBottom: theme.spacing(1),
  47. '& .MuiSvgIcon-root': {
  48. fontSize: 21,
  49. color: theme.palette.text.secondary,
  50. marginRight: theme.spacing(2),
  51. }
  52. },
  53. },
  54. }));
  55. const useStyles = makeStyles({
  56. container: {
  57. display: "flex",
  58. alignItems: 'flex-end',
  59. alignContent: 'flex-end',
  60. flexDirection:'column',
  61. borderRadius: 7,
  62. position: 'relative',
  63. marginBottom: 5,
  64. padding:'10px 22px 10px 0px'
  65. },
  66. wrapper: {
  67. position: 'relative',
  68. display: 'flex',
  69. alignItems: 'center',
  70. alignContent: 'center',
  71. flexWrap:'wrap',
  72. maxWidth: 450,
  73. minWidth:200,
  74. padding: "10px",
  75. paddingBottom:18,
  76. backgroundColor: "#deffa9",
  77. border: "1px solid #d5ff91",
  78. borderRadius: 7,
  79. },
  80. wrapperInner: {
  81. display: 'flex',
  82. alignItems: 'center',
  83. alignContent: 'center',
  84. width:'100%'
  85. },
  86. message: {
  87. wordBreak:'break-word',
  88. textAlign: "left",
  89. font: "400 .9em 'Open Sans', sans-serif",
  90. "&:after": {
  91. content: "''",
  92. position: "absolute",
  93. width: "0",
  94. height: "0",
  95. borderBottom: "15px solid #deffa9",
  96. borderLeft: "15px solid transparent",
  97. borderRight: "15px solid transparent",
  98. bottom: "0",
  99. right: "-15px"
  100. },
  101. "&:before": {
  102. content: "''",
  103. position: "absolute",
  104. width: "0",
  105. height: "0",
  106. borderBottom: "17px solid #deffa9",
  107. borderLeft: "16px solid transparent",
  108. borderRight: "16px solid transparent",
  109. bottom: "-1px",
  110. right: "-17px"
  111. }
  112. },
  113. messageActive: {
  114. wordBreak:'break-word',
  115. textAlign: "left",
  116. font: "400 .9em 'Open Sans', sans-serif",
  117. "&:after": {
  118. content: "''",
  119. position: "absolute",
  120. width: "0",
  121. height: "0",
  122. borderBottom: "15px solid #ced8d7",
  123. borderLeft: "15px solid transparent",
  124. borderRight: "15px solid transparent",
  125. bottom: "0",
  126. right: "-15px"
  127. },
  128. "&:before": {
  129. content: "''",
  130. position: "absolute",
  131. width: "0",
  132. height: "0",
  133. borderBottom: "17px solid #ced8d7",
  134. borderLeft: "16px solid transparent",
  135. borderRight: "16px solid transparent",
  136. bottom: "0px",
  137. right: "-17px"
  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: -64,
  254. bottom: -10,
  255. pointerEvents: 'auto'
  256. },
  257. folderIcon: {
  258. marginRight: 7,
  259. color: '#54b0fc',
  260. cursor: 'pointer',
  261. '&:hover': {
  262. color: '#016cc3'
  263. },
  264. },
  265. });
  266. const label = { inputProps: { 'aria-label': 'Checkbox demo' } };
  267. interface IMessageRightText {
  268. message:string,
  269. name:string,
  270. lastName:string,
  271. createdAt: string,
  272. caption: string,
  273. emoji: string,
  274. emojiCompanion: string,
  275. pinned: boolean,
  276. isSomeSelected: boolean,
  277. isSelected:(_id:string) => boolean,
  278. handleSelected: (_id:string) => void,
  279. _id: string,
  280. nightMode: boolean,
  281. handleReply: (_id: string) => void,
  282. handleForward: (_id: string) => void,
  283. }
  284. const MessageRightText = ({message,name,lastName,createdAt,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,nightMode,handleReply,handleForward}:IMessageRightText) => {
  285. const classes = useStyles();
  286. const [anchorEl, setAnchorEl] = useState<any>(null);
  287. const [selected, setSelected] = useState<boolean>(false);
  288. const [modal,setModal] = useState<boolean>(false)
  289. const open = Boolean(anchorEl);
  290. const checked = isSelected(_id)
  291. const handleClose = (type: string | undefined): void => {
  292. if (type === 'copy') copied('Message')
  293. if (type === 'delete') setModal(true)
  294. setAnchorEl(null)
  295. setSelected(false)
  296. }
  297. const handleDeleteModal = (e: any) => {
  298. const id = e.target.id
  299. if (id === 'overlay' || id === 'cancel') return setModal(false)
  300. if (id === 'delete') {
  301. removeMessageById(_id)
  302. setModal(false)
  303. }
  304. }
  305. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  306. e.preventDefault()
  307. setAnchorEl(e.currentTarget)
  308. setSelected(true)
  309. }
  310. const handleEmojiMenu = ({ target }: any): void => {
  311. const idEmoji = target.id
  312. if (idEmoji === emoji) {updateMessageById(_id,'')
  313. } else updateMessageById(_id,idEmoji)
  314. }
  315. return (
  316. <div className={classes.container}>
  317. <div onContextMenu={(e) => handleContextMenu(e)} className={classes.wrapper}
  318. style={{backgroundColor:selected?'#ced8d7':undefined,
  319. border:selected?'#ced8d7':undefined,pointerEvents:isSomeSelected?'none':'auto'}}>
  320. <Typography style={{marginLeft:40,color: "#42aee0"}} variant="h6" align="right">
  321. {`${firstLetter(name)}${slicedWord(name, 15, 1)}
  322. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  323. </Typography>
  324. <div className={classes.wrapperInner}>
  325. <CopyToClipboard onCopy={() => copied('Message')} text={message}>
  326. <ContentCopyIcon className={classes.folderIcon} fontSize='large'/>
  327. </CopyToClipboard>
  328. <ListItemText className={selected?classes.messageActive:classes.message}
  329. secondary={message}
  330. secondaryTypographyProps={{ color: "#0e0d0d" }} />
  331. </div>
  332. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  333. {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
  334. {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
  335. {isSomeSelected && <div className={classes.checkboxSelect}><Checkbox {...label} checked={checked} sx={{ color: nightMode ? '#ffffff' : '#00ff48', '&.Mui-checked': { color: nightMode ? '#ffffff' : '#00ff48' } }}
  336. onClick={() => handleSelected(_id)}/></div>}
  337. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  338. anchorEl={anchorEl} open={open} onClose={handleClose}>
  339. <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
  340. {emojisArr.map((el:string, i:number) =>
  341. <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
  342. </MenuItem>
  343. <Divider />
  344. <MenuItem onClick={() => {
  345. handleReply(_id)
  346. handleClose(undefined)
  347. }}>
  348. <ReplyIcon />
  349. Reply
  350. </MenuItem>
  351. <MenuItem onClick={() => {
  352. handleForward(_id)
  353. handleClose(undefined)
  354. }}>
  355. <ReplyIcon style={{transform :'rotateY(180deg)'}} />
  356. Forward
  357. </MenuItem>
  358. <CopyToClipboard onCopy={() => handleClose('copy')} text={message}>
  359. <MenuItem>
  360. <ContentCopyIcon />
  361. Copy Text
  362. </MenuItem>
  363. </CopyToClipboard>
  364. <MenuItem onClick={() => {
  365. pinMessageById(_id, !pinned)
  366. handleClose(undefined)
  367. }}>
  368. {pinned ?
  369. <CloseIcon className={classes.iconClose} /> :
  370. <PushPinIcon />}
  371. {pinned?'Unpin':'Pin'}
  372. </MenuItem>
  373. <MenuItem onClick={() => {
  374. handleSelected(_id)
  375. handleClose(undefined)
  376. }}>
  377. <CheckBoxIcon />
  378. Select
  379. </MenuItem>
  380. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  381. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  382. Delete
  383. </MenuItem>
  384. </StyledMenu>
  385. {modal &&
  386. <div onClick={handleDeleteModal} className={classes.overlay} id='overlay'>
  387. <div className={classes.modalDelete}>
  388. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  389. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  390. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  391. DELETE MESSAGE
  392. </Button>
  393. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  394. CANCEL
  395. </Button>
  396. </div>
  397. </div>}
  398. </div>
  399. {caption&&<div className={classes.captionWrapper}>{caption}</div>}
  400. </div>
  401. )};
  402. export default MessageRightText