bbgo_origin/frontend/pages/_app.tsx

134 lines
3.9 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
2022-06-12 15:11:42 +00:00
import { ThemeProvider } from '@mui/material/styles';
2021-01-24 06:32:52 +00:00
2022-06-12 15:11:42 +00:00
import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
import LinearProgress from '@mui/material/LinearProgress';
import Box from '@mui/material/Box';
2021-02-21 10:08:22 +00:00
2022-06-12 15:11:42 +00:00
import CssBaseline from '@mui/material/CssBaseline';
2021-01-24 06:32:52 +00:00
import theme from '../src/theme';
2022-06-11 00:57:54 +00:00
import '../styles/globals.css';
import { querySessions, querySyncStatus } from '../api/bbgo';
2021-02-22 06:14:39 +00:00
2022-06-11 00:57:54 +00:00
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
2022-06-11 00:57:54 +00:00
let syncStatusPoller = null;
2021-02-22 07:01:05 +00:00
2021-01-24 06:32:52 +00:00
export default function MyApp(props) {
2022-06-11 00:57:54 +00:00
const { Component, pageProps } = props;
2021-01-24 10:12:26 +00:00
2022-06-11 00:57:54 +00:00
const [loading, setLoading] = React.useState(true);
const [syncing, setSyncing] = React.useState(false);
2021-02-21 10:08:22 +00:00
2022-06-11 00:57:54 +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
2022-06-11 00:57:54 +00:00
querySessions((sessions) => {
if (sessions.length > 0) {
setSyncing(true);
2021-02-21 11:36:03 +00:00
2022-06-11 00:57:54 +00:00
const pollSyncStatus = () => {
querySyncStatus((status) => {
switch (status) {
case SyncNotStarted:
break;
case Syncing:
setSyncing(true);
break;
case SyncDone:
clearInterval(syncStatusPoller);
setLoading(false);
setSyncing(false);
break;
2021-02-21 11:36:03 +00:00
}
2022-06-11 00:57:54 +00:00
}).catch((err) => {
console.error(err);
});
};
2021-02-21 10:08:22 +00:00
2022-06-11 00:57:54 +00:00
syncStatusPoller = setInterval(pollSyncStatus, 1000);
} else {
// no session found, so we can not sync any data
setLoading(false);
setSyncing(false);
}
}).catch((err) => {
console.error(err);
});
}, []);
2021-01-24 10:12:26 +00:00
2022-06-11 00:57:54 +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 />
{loading ? (
syncing ? (
<Dialog
open={syncing}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
{'Syncing Trades'}
</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
The environment is syncing trades from the exchange sessions.
Please wait a moment...
</DialogContentText>
<Box m={2}>
<LinearProgress />
</Box>
</DialogContent>
</Dialog>
) : (
<Dialog
open={loading}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{'Loading'}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Loading...
</DialogContentText>
<Box m={2}>
<LinearProgress />
</Box>
</DialogContent>
</Dialog>
)
) : (
<Component {...pageProps} />
)}
</ThemeProvider>
</React.Fragment>
);
2021-01-24 06:07:44 +00:00
}
2021-01-24 06:32:52 +00:00
MyApp.propTypes = {
2022-06-11 00:57:54 +00:00
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object.isRequired,
2021-01-24 06:32:52 +00:00
};