index.tsx 11 KB

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