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