index.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Grid from '@mui/material/Grid'
  2. import { makeStyles } from '@material-ui/core'
  3. import { useSelector } from 'react-redux'
  4. import { useRef,useEffect } from 'react'
  5. import io from 'socket.io-client';
  6. import LeftBar from './LeftBar'
  7. import CentralBar from './CentralBar'
  8. import RightBar from './RightBar'
  9. import { getRightIsOpen } from '../../redux/control/selector'
  10. import { getChatMemo } from '../../redux/chat/selector'
  11. import { getNightMode } from '../../redux/authorization/selector'
  12. import wallpaper from '../../img/wallpaper.jpg'
  13. import wallpaperNight from '../../img/wallpaperNight.jpg'
  14. import { prodSocketURL } from '../../helpers';
  15. import { socketIdChat } from '../../api-data';
  16. const socket = io(prodSocketURL)
  17. const useStyles = makeStyles({
  18. container: {
  19. minHeight: '100vh',
  20. maxHeight: '100vh',
  21. },
  22. centralAndRight: {
  23. display:'flex'
  24. },
  25. })
  26. const HomePage = () => {
  27. const classes = useStyles()
  28. const rightIsOpen = useSelector(getRightIsOpen)
  29. const chatDivRef = useRef<any | null>(null)
  30. const nightMode = useSelector(getNightMode)
  31. const { companionId } = useSelector(getChatMemo)
  32. const backgroundImage = `url(${nightMode ? wallpaperNight : wallpaper})`
  33. useEffect(() => {
  34. socket.on("connect", () => {
  35. const socketId = socket.id
  36. socketIdChat(socketId)
  37. });
  38. socket.on('disconnect', () => {
  39. socketIdChat('')
  40. });
  41. return () => {
  42. socket.off('connect');
  43. socket.off('disconnect');
  44. };
  45. }, [])
  46. return (
  47. <Grid className={classes.container} container spacing={0} >
  48. <LeftBar chatDivRef={chatDivRef} />
  49. {companionId ?
  50. <Grid item lg={9} className={classes.centralAndRight}>
  51. <CentralBar rightIsOpen={rightIsOpen} chatDivRef={chatDivRef}
  52. companionId={companionId} backgroundImage={backgroundImage}
  53. socket={socket}/>
  54. <RightBar rightIsOpen={rightIsOpen} chatDivRef={chatDivRef} />
  55. </Grid> :
  56. <Grid item lg={9} style={{backgroundImage}}/>}
  57. </Grid>
  58. )
  59. }
  60. export default HomePage