|
@@ -1,8 +1,9 @@
|
|
|
-import React, {useState, useEffect, useRef} from 'react'
|
|
|
+import React, {useState, useEffect, useRef, createContext } from 'react'
|
|
|
import './App.scss'
|
|
|
import {Provider, connect} from 'react-redux'
|
|
|
import {Router, Route, Link, Redirect, Switch} from 'react-router-dom'
|
|
|
import createHistory from "history/createBrowserHistory"
|
|
|
+
|
|
|
import {
|
|
|
store,
|
|
|
socket
|
|
@@ -14,20 +15,63 @@ import {
|
|
|
Main,
|
|
|
AsidePage,
|
|
|
CChatsPage,
|
|
|
- MsgPage
|
|
|
+ CMsgPage,
|
|
|
+ PageNoChat
|
|
|
} from "./pages"
|
|
|
|
|
|
import Grid from '@mui/material/Grid'
|
|
|
+import { useTheme } from '@mui/material/styles'
|
|
|
+import useMediaQuery from '@mui/material/useMediaQuery'
|
|
|
+
|
|
|
|
|
|
+export const history = createHistory()
|
|
|
|
|
|
|
|
|
-const PageNoChat = () => (
|
|
|
- <div style={{ height: "100vh", width: "100%", backgroundColor: "#eee" }}>
|
|
|
- </div>
|
|
|
-)
|
|
|
+export const AdaptiveContext = createContext(null)
|
|
|
|
|
|
-const AuthSwitch = ({ token }) => {
|
|
|
+const AdaptiveGrid = ({ }) => {
|
|
|
+
|
|
|
+ // const [,route, histId] = history.location.pathname.split('/')
|
|
|
+
|
|
|
+ const theme = useTheme()
|
|
|
+ const matches = useMediaQuery(theme.breakpoints.up('sm'))
|
|
|
+
|
|
|
+ const [aside, setAside] = useState(true)
|
|
|
+
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <AdaptiveContext.Provider value={ {setAside} }>
|
|
|
+
|
|
|
+ { (matches || aside) &&
|
|
|
+ <Grid item xs={12} sm={4}>
|
|
|
+ <AsidePage>
|
|
|
+ <Switch>
|
|
|
+ <Route path="/main/:_id" component={CChatsPage} />
|
|
|
+ <Route path="/find" component={PageNoChat} />
|
|
|
+ <Route path="*" component={CChatsPage} />
|
|
|
+ </Switch>
|
|
|
+ </AsidePage>
|
|
|
+ </Grid>
|
|
|
+ }
|
|
|
|
|
|
+ { (matches || !aside) &&
|
|
|
+ <Grid item xs={12} sm={8}>
|
|
|
+ <Switch>
|
|
|
+ <Route path="/main" component={PageNoChat} exact/>
|
|
|
+ <Route path="/main/:_id" component={CMsgPage} exact/>
|
|
|
+ <Route path="*" component={PageNoChat} />
|
|
|
+ </Switch>
|
|
|
+ </Grid>
|
|
|
+ }
|
|
|
+
|
|
|
+ </AdaptiveContext.Provider>
|
|
|
+ </>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const AuthSwitch = ({ token }) => {
|
|
|
+
|
|
|
if (token) {
|
|
|
console.log('подключение сокета')
|
|
|
socket.emit('jwt', token)
|
|
@@ -36,44 +80,23 @@ const AuthSwitch = ({ token }) => {
|
|
|
return (
|
|
|
<>
|
|
|
{token ?
|
|
|
- <>
|
|
|
<Main>
|
|
|
|
|
|
- <Grid item xs={12} sm={4}>
|
|
|
- <AsidePage>
|
|
|
- <Switch>
|
|
|
- <Route path="/main/:_id" component={CChatsPage} />
|
|
|
- <Route path="/find" component={PageNoChat} />
|
|
|
- <Route path="*" component={CChatsPage} />
|
|
|
- </Switch>
|
|
|
- </AsidePage>
|
|
|
- </Grid>
|
|
|
-
|
|
|
- <Grid item xs={12} sm={8}>
|
|
|
- <Switch>
|
|
|
- <Route path="/main" component={PageNoChat} exact/>
|
|
|
- <Route path="/main/:_id" component={MsgPage} exact/>
|
|
|
- <Route path="*" component={PageNoChat} />
|
|
|
- </Switch>
|
|
|
- </Grid>
|
|
|
+ <AdaptiveGrid />
|
|
|
|
|
|
</Main>
|
|
|
- </>
|
|
|
- :
|
|
|
- <>
|
|
|
+ :
|
|
|
<Switch>
|
|
|
<Route path="/reg" component={Register} />
|
|
|
<Route path="/login" component={Login} />
|
|
|
<Route path="*" component={Login} />
|
|
|
</Switch>
|
|
|
- </>
|
|
|
}
|
|
|
</>
|
|
|
)
|
|
|
}
|
|
|
const CAuthSwitch = connect(state => ({ token: state.auth.token || null }))(AuthSwitch)
|
|
|
|
|
|
-export const history = createHistory()
|
|
|
|
|
|
function App() {
|
|
|
|
|
@@ -84,7 +107,6 @@ function App() {
|
|
|
<Provider store={store}>
|
|
|
<div className="App">
|
|
|
|
|
|
-
|
|
|
<CAuthSwitch />
|
|
|
|
|
|
</div>
|