bbgo_origin/frontend/pages/_app.tsx

125 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-01-24 06:32:52 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
2021-01-24 10:12:26 +00:00
2021-02-01 09:13:27 +00:00
import {ThemeProvider} from '@material-ui/core/styles';
2021-01-24 06:32:52 +00:00
2021-02-21 10:08:22 +00:00
import Dialog from '@material-ui/core/Dialog';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
2021-02-21 10:21:05 +00:00
import LinearProgress from '@material-ui/core/LinearProgress';
import Box from '@material-ui/core/Box';
2021-02-21 10:08:22 +00:00
2021-01-24 10:12:26 +00:00
import CssBaseline from '@material-ui/core/CssBaseline';
2021-01-24 06:32:52 +00:00
import theme from '../src/theme';
2021-01-24 06:07:44 +00:00
import '../styles/globals.css'
2021-02-21 11:36:03 +00:00
import {querySessions, querySyncStatus} from "../api/bbgo";
2021-01-24 06:07:44 +00:00
2021-01-24 06:32:52 +00:00
export default function MyApp(props) {
2021-01-24 10:12:26 +00:00
const {Component, pageProps} = props;
2021-02-21 11:36:03 +00:00
const [loading, setLoading] = React.useState(true)
const [syncing, setSyncing] = React.useState(false)
2021-02-21 10:08:22 +00:00
2021-01-24 10:12:26 +00:00
React.useEffect(() => {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
2021-02-21 10:08:22 +00:00
2021-02-21 11:36:03 +00:00
querySessions((sessions) => {
if (sessions.length > 0) {
setSyncing(true)
// session is configured, check if we're syncing data
let poller = null
const pollSyncStatus = () => {
querySyncStatus((status) => {
setSyncing(status)
if (!status) {
2021-02-21 12:01:47 +00:00
// mark it as done
2021-02-21 11:36:03 +00:00
setLoading(false)
clearInterval(poller)
}
}).catch((err) => {
console.error(err)
})
2021-02-21 10:08:22 +00:00
}
2021-02-21 11:36:03 +00:00
poller = setInterval(pollSyncStatus, 1000)
} else {
setLoading(false)
}
}).catch((err) => {
console.error(err)
})
2021-02-21 10:08:22 +00:00
2021-01-24 10:12:26 +00:00
}, []);
2021-02-21 12:01:47 +00:00
const handleClose = (e) => {
}
2021-02-21 10:08:22 +00:00
2021-01-24 10:12:26 +00:00
return (
<React.Fragment>
<Head>
<title>BBGO</title>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width"/>
</Head>
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline/>
2021-02-21 10:08:22 +00:00
{
2021-02-21 12:01:47 +00:00
loading ? (syncing ? (
2021-02-21 11:36:03 +00:00
<React.Fragment>
<Dialog
2021-02-21 12:01:47 +00:00
open={syncing}
2021-02-21 11:36:03 +00:00
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
2021-02-21 12:01:47 +00:00
<DialogTitle id="alert-dialog-title">{"Syncing Trades"}</DialogTitle>
2021-02-21 11:36:03 +00:00
<DialogContent>
<DialogContentText id="alert-dialog-description">
2021-02-21 12:01:47 +00:00
The environment is syncing trades from the exchange sessions.
Please wait a moment...
2021-02-21 11:36:03 +00:00
</DialogContentText>
<Box m={2}>
2021-02-21 12:01:47 +00:00
<LinearProgress/>
2021-02-21 11:36:03 +00:00
</Box>
</DialogContent>
</Dialog>
</React.Fragment>
2021-02-21 12:01:47 +00:00
) : (
2021-02-21 10:08:22 +00:00
<React.Fragment>
<Dialog
2021-02-21 12:01:47 +00:00
open={loading}
2021-02-21 10:08:22 +00:00
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
2021-02-21 12:01:47 +00:00
<DialogTitle id="alert-dialog-title">{"Loading"}</DialogTitle>
2021-02-21 10:08:22 +00:00
<DialogContent>
<DialogContentText id="alert-dialog-description">
2021-02-21 12:01:47 +00:00
Loading...
2021-02-21 10:08:22 +00:00
</DialogContentText>
2021-02-21 10:21:05 +00:00
<Box m={2}>
2021-02-21 12:01:47 +00:00
<LinearProgress/>
2021-02-21 10:21:05 +00:00
</Box>
2021-02-21 10:08:22 +00:00
</DialogContent>
</Dialog>
</React.Fragment>
2021-02-21 12:01:47 +00:00
)) : (
2021-02-21 10:08:22 +00:00
<Component {...pageProps}/>
)
}
2021-01-24 10:12:26 +00:00
</ThemeProvider>
</React.Fragment>
);
2021-01-24 06:07:44 +00:00
}
2021-01-24 06:32:52 +00:00
MyApp.propTypes = {
2021-01-24 10:12:26 +00:00
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object.isRequired,
2021-01-24 06:32:52 +00:00
};