bbgo_origin/frontend/pages/_app.tsx

135 lines
5.1 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-02-22 06:14:39 +00:00
import {Sync} from "@material-ui/icons";
const SyncNotStarted = 0
const Syncing = 1
const SyncDone = 2
2021-01-24 06:07:44 +00:00
2021-02-22 07:01:05 +00:00
// session is configured, check if we're syncing data
let syncStatusPoller = null
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)
const pollSyncStatus = () => {
querySyncStatus((status) => {
2021-02-22 06:14:39 +00:00
switch (status) {
case SyncNotStarted:
break
case Syncing:
setSyncing(true);
break;
case SyncDone:
2021-02-22 07:01:05 +00:00
clearInterval(syncStatusPoller);
2021-02-22 06:14:39 +00:00
setLoading(false);
setSyncing(false);
break;
2021-02-21 11:36:03 +00:00
}
}).catch((err) => {
console.error(err)
})
2021-02-21 10:08:22 +00:00
}
2021-02-22 07:01:05 +00:00
syncStatusPoller = setInterval(pollSyncStatus, 1000)
2021-02-21 11:36:03 +00:00
} else {
2021-02-21 12:10:19 +00:00
// no session found, so we can not sync any data
2021-02-21 11:36:03 +00:00
setLoading(false)
2021-02-21 12:10:19 +00:00
setSyncing(false)
2021-02-21 11:36:03 +00:00
}
}).catch((err) => {
console.error(err)
})
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
};