index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 { CopyToClipboard } from 'react-copy-to-clipboard';
  11. import { removeMessageById } from "../../../../../../api-data";
  12. import { firstLetter,slicedWord,timeStampMessage,copied } from '../../../../../../helpers'
  13. const StyledMenu = styled((props:any) => (
  14. <Menu
  15. elevation={0}
  16. anchorOrigin={{
  17. vertical: 'top',
  18. horizontal: 'right',
  19. }}
  20. transformOrigin={{
  21. vertical: 'bottom',
  22. horizontal: 'right',
  23. }}
  24. {...props}
  25. />
  26. ))(({ theme }:any) => ({
  27. '& .MuiPaper-root': {
  28. borderRadius: 10,
  29. marginTop: theme.spacing(0),
  30. minWidth: 220,
  31. color:
  32. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  33. boxShadow:
  34. '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',
  35. '& .MuiMenu-list': {
  36. padding: '4px 4px',
  37. },
  38. '& .MuiMenuItem-root': {
  39. marginBottom: theme.spacing(1),
  40. '& .MuiSvgIcon-root': {
  41. fontSize: 21,
  42. color: theme.palette.text.secondary,
  43. marginRight: theme.spacing(2),
  44. }
  45. },
  46. },
  47. }));
  48. const useStyles = makeStyles({
  49. container: {
  50. display: "flex",
  51. justifyContent: "flex-start",
  52. marginBottom:15
  53. },
  54. wrapper: {
  55. position: 'relative',
  56. display: 'flex',
  57. alignItems: 'center',
  58. alignContent: 'center',
  59. maxWidth: 450,
  60. minWidth:200,
  61. padding: "10px",
  62. paddingBottom:18,
  63. backgroundColor: "#ffffff",
  64. border: "1px solid #f0f0f0",
  65. borderRadius: 7,
  66. },
  67. message: {
  68. wordBreak:'break-word',
  69. textAlign: "left",
  70. font: "400 .9em 'Open Sans', sans-serif",
  71. "&:after": {
  72. content: "''",
  73. position: "absolute",
  74. width: "0",
  75. height: "0",
  76. borderBottom: "15px solid #ffffff",
  77. borderLeft: "15px solid transparent",
  78. borderRight: "15px solid transparent",
  79. bottom: '0px',
  80. left: "-15px"
  81. },
  82. "&:before": {
  83. content: "''",
  84. position: "absolute",
  85. width: "0",
  86. height: "0",
  87. borderBottom: "17px solid #ffffff",
  88. borderLeft: "16px solid transparent",
  89. borderRight: "16px solid transparent",
  90. bottom: "-1px",
  91. left: "-17px"
  92. }
  93. },
  94. messageActive: {
  95. wordBreak:'break-word',
  96. textAlign: "left",
  97. font: "400 .9em 'Open Sans', sans-serif",
  98. "&:after": {
  99. content: "''",
  100. position: "absolute",
  101. width: "0",
  102. height: "0",
  103. borderBottom: "15px solid #babdbc",
  104. borderLeft: "15px solid transparent",
  105. borderRight: "15px solid transparent",
  106. bottom: '0px',
  107. left: "-15px"
  108. },
  109. "&:before": {
  110. content: "''",
  111. position: "absolute",
  112. width: "0",
  113. height: "0",
  114. borderBottom: "17px solid #babdbc",
  115. borderLeft: "16px solid transparent",
  116. borderRight: "16px solid transparent",
  117. bottom: "-1px",
  118. left: "-17px"
  119. }
  120. },
  121. copyIcon: {
  122. color: '#b56ff7',
  123. cursor: 'pointer',
  124. marginRight: 7,
  125. '&:hover': {
  126. color: '#9c3bf8',
  127. },
  128. },
  129. time: {
  130. position: "absolute",
  131. fontSize: ".65em",
  132. fontWeight:600,
  133. bottom: 6,
  134. right: 6,
  135. color: '#414141',
  136. padding: 3,
  137. borderRadius: 5,
  138. },
  139. caption: {
  140. position: "absolute",
  141. fontSize: ".65em",
  142. fontWeight:600,
  143. bottom: 6,
  144. left: 6,
  145. color: '#000000',
  146. padding: 3,
  147. borderRadius: 5,
  148. },
  149. modalDelete: {
  150. background: '#ffffff',
  151. position: 'absolute',
  152. content:'',
  153. width: '20%',
  154. height:'auto',
  155. left: '40%',
  156. bottom: '48.5%',
  157. borderRadius: 10,
  158. padding: 10,
  159. display: 'flex',
  160. flexDirection:'column'
  161. },
  162. overlay: {
  163. position: 'fixed',
  164. top: 0,
  165. left: 0,
  166. width: '100vw',
  167. height: '100vh',
  168. zIndex: 100,
  169. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  170. overflowY: 'hidden',
  171. },
  172. });
  173. interface IMessageLeftText {
  174. message:string,
  175. name:string,
  176. lastName:string,
  177. createdAt: string,
  178. caption :string,
  179. _id:string
  180. }
  181. const MessageLeftText = ({message,name,lastName,createdAt,caption,_id}:IMessageLeftText) => {
  182. const classes = useStyles();
  183. const [anchorEl, setAnchorEl] = useState<any>(null);
  184. const [selected, setSelected] = useState<boolean>(false);
  185. const [modal,setModal] = useState<boolean>(false)
  186. const open = Boolean(anchorEl);
  187. const handleClose = (type: string | undefined): void => {
  188. if (type === 'copy') copied('Message')
  189. if (type === 'delete') setModal(true)
  190. setAnchorEl(null)
  191. setSelected(false)
  192. }
  193. const handleDeleteModal = (e: any) => {
  194. const id = e.target.id
  195. if (id === 'overlay' || id === 'cancel') return setModal(false)
  196. if (id === 'delete') {
  197. removeMessageById(_id)
  198. setModal(false)
  199. }
  200. }
  201. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  202. e.preventDefault()
  203. setAnchorEl(e.currentTarget)
  204. setSelected(true)
  205. }
  206. return (
  207. <div className={classes.container}>
  208. <div onContextMenu={(e) => handleContextMenu(e)} className={classes.wrapper}
  209. style={{backgroundColor:selected?'#babdbc':undefined,
  210. border:selected?'#babdbc':undefined}}>
  211. <CopyToClipboard onCopy={() => copied('Message')} text={message}>
  212. <ContentCopyIcon className={classes.copyIcon} fontSize='large'/>
  213. </CopyToClipboard>
  214. <ListItemText className={selected?classes.messageActive:classes.message}
  215. primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  216. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  217. primaryTypographyProps={{color: "#0379af"}}
  218. secondary={message}
  219. secondaryTypographyProps={{ color: "#0e0d0d" }} />
  220. <div className={classes.time}>{timeStampMessage(createdAt)}</div>
  221. <div className={classes.caption}>{caption}</div>
  222. <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
  223. anchorEl={anchorEl} open={open} onClose={handleClose}>
  224. <CopyToClipboard onCopy={() => handleClose('copy')} text={message}>
  225. <MenuItem>
  226. <ContentCopyIcon />
  227. Copy message
  228. </MenuItem>
  229. </CopyToClipboard>
  230. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  231. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  232. Delete message
  233. </MenuItem>
  234. </StyledMenu>
  235. {modal &&
  236. <div onClick={handleDeleteModal} className={classes.overlay} id='overlay'>
  237. <div className={classes.modalDelete}>
  238. <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
  239. <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
  240. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  241. DELETE MESSAGE
  242. </Button>
  243. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  244. CANCEL
  245. </Button>
  246. </div>
  247. </div>}
  248. </div>
  249. </div>
  250. )};
  251. export default MessageLeftText