bbgo_origin/apps/frontend/pages/index.tsx

122 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-06-11 00:57:54 +00:00
import React, { useState } from 'react';
import { useRouter } from 'next/router';
2021-01-24 10:12:26 +00:00
2022-06-12 15:19:39 +00:00
import { makeStyles } from '@mui/styles';
2022-06-12 15:11:42 +00:00
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Grid from '@mui/material/Grid';
import Paper from '@mui/material/Paper';
2021-01-24 06:32:52 +00:00
2021-01-25 10:29:31 +00:00
import TotalAssetsPie from '../components/TotalAssetsPie';
import TotalAssetSummary from '../components/TotalAssetsSummary';
import TotalAssetDetails from '../components/TotalAssetsDetails';
2021-01-25 10:29:31 +00:00
2021-01-28 10:51:35 +00:00
import TradingVolumePanel from '../components/TradingVolumePanel';
2021-01-25 10:29:31 +00:00
import ExchangeSessionTabPanel from '../components/ExchangeSessionTabPanel';
2021-01-24 06:07:44 +00:00
2021-02-01 09:13:27 +00:00
import DashboardLayout from '../layouts/DashboardLayout';
2022-06-11 00:57:54 +00:00
import { queryAssets, querySessions } from '../api/bbgo';
2021-10-25 18:02:29 +00:00
import { ChainId, Config, DAppProvider } from '@usedapp/core';
2022-06-12 15:19:39 +00:00
import { Theme } from '@mui/material/styles';
// fix the `theme.spacing` missing error
// https://stackoverflow.com/a/70707121/3897950
declare module '@mui/styles/defaultTheme' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface (remove this line if you don't have the rule enabled)
interface DefaultTheme extends Theme {}
}
2021-10-25 18:02:29 +00:00
2021-01-24 10:12:26 +00:00
const useStyles = makeStyles((theme) => ({
2022-06-11 00:57:54 +00:00
totalAssetsSummary: {
margin: theme.spacing(2),
padding: theme.spacing(2),
},
grid: {
flexGrow: 1,
},
control: {
padding: theme.spacing(2),
},
2021-01-24 10:12:26 +00:00
}));
2021-10-25 18:02:29 +00:00
const config: Config = {
2022-06-11 00:57:54 +00:00
readOnlyChainId: ChainId.Mainnet,
readOnlyUrls: {
[ChainId.Mainnet]:
'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
},
};
2021-10-25 18:02:29 +00:00
2021-02-01 09:13:27 +00:00
// props are pageProps passed from _app.tsx
export default function Home() {
2022-06-11 00:57:54 +00:00
const classes = useStyles();
const router = useRouter();
const [assets, setAssets] = useState({});
const [sessions, setSessions] = React.useState([]);
React.useEffect(() => {
querySessions((sessions) => {
if (sessions && sessions.length > 0) {
setSessions(sessions);
queryAssets(setAssets);
} else {
router.push('/setup');
}
}).catch((err) => {
console.error(err);
});
}, [router]);
if (sessions.length == 0) {
2021-01-25 10:29:31 +00:00
return (
2022-06-11 00:57:54 +00:00
<DashboardLayout>
<Box m={4}>
<Typography variant="h4" gutterBottom>
Loading
</Typography>
</Box>
</DashboardLayout>
2021-01-25 08:56:02 +00:00
);
2022-06-11 00:57:54 +00:00
}
2021-01-24 06:32:52 +00:00
2022-06-11 00:57:54 +00:00
console.log('index: assets', assets);
return (
<DAppProvider config={config}>
<DashboardLayout>
<Paper className={classes.totalAssetsSummary}>
<Typography variant="h4" gutterBottom>
Total Assets
</Typography>
<div className={classes.grid}>
<Grid
container
direction="row"
justifyContent="space-around"
alignItems="flex-start"
spacing={1}
>
<Grid item xs={12} md={8}>
<TotalAssetSummary assets={assets} />
<TotalAssetsPie assets={assets} />
</Grid>
<Grid item xs={12} md={4}>
<TotalAssetDetails assets={assets} />
</Grid>
</Grid>
</div>
</Paper>
<TradingVolumePanel />
<ExchangeSessionTabPanel />
</DashboardLayout>
</DAppProvider>
);
}