2022-10-22 14:32:33 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import GridLayout, { Layout } from 'react-grid-layout';
|
2022-10-25 08:37:25 +00:00
|
|
|
import { Grid, styled, useTheme } from '@mui/material';
|
2022-10-20 18:06:16 +00:00
|
|
|
import { apiClient } from '../services/api';
|
2022-10-23 18:29:39 +00:00
|
|
|
import checkVer from '../utils/checkVer';
|
2022-10-20 18:06:16 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
Book,
|
|
|
|
LimitList,
|
|
|
|
Maker,
|
|
|
|
Robot,
|
|
|
|
Info,
|
|
|
|
Settings,
|
|
|
|
Favorites,
|
|
|
|
defaultMaker,
|
|
|
|
defaultRobot,
|
|
|
|
defaultInfo,
|
|
|
|
} from '../models';
|
|
|
|
|
2022-10-23 18:29:39 +00:00
|
|
|
import { PlaceholderWidget, MakerWidget, BookWidget, DepthChartWidget } from '../pro/Widgets';
|
2022-10-22 14:32:33 +00:00
|
|
|
import ToolBar from '../pro/ToolBar';
|
|
|
|
import LandingDialog from '../pro/LandingDialog';
|
|
|
|
|
2022-10-20 18:06:16 +00:00
|
|
|
const getWindowSize = function (fontSize: number) {
|
|
|
|
// returns window size in EM units
|
|
|
|
return {
|
|
|
|
width: window.innerWidth / fontSize,
|
|
|
|
height: window.innerHeight / fontSize,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-22 14:32:33 +00:00
|
|
|
interface MainProps {
|
|
|
|
settings: Settings;
|
|
|
|
setSettings: (state: Settings) => void;
|
|
|
|
}
|
|
|
|
|
2022-10-25 08:37:25 +00:00
|
|
|
// To Do. Add dotted grid when layout is not frozen
|
|
|
|
// ${freeze ?
|
|
|
|
// `background: radial-gradient(${theme.palette.text.disabled} 1px, transparent 0px);
|
|
|
|
// background-size: ${gridCellSize}em ${gridCellSize}em;
|
|
|
|
// background-position: left 1em bottom 1.5em;`
|
|
|
|
// :''}
|
|
|
|
|
|
|
|
const StyledRGL = styled(GridLayout)(
|
|
|
|
({ theme, gridCellSize, height, width, freeze }) => `
|
|
|
|
height: ${height}em;
|
|
|
|
width: ${width}px;
|
|
|
|
max-height: ${height}em;
|
|
|
|
`,
|
|
|
|
);
|
|
|
|
|
2022-10-22 14:32:33 +00:00
|
|
|
const Main = ({ settings, setSettings }: MainProps): JSX.Element => {
|
|
|
|
const theme = useTheme();
|
2022-10-25 08:37:25 +00:00
|
|
|
const em: number = theme.typography.fontSize;
|
|
|
|
const toolbarHeight: number = 3;
|
|
|
|
const gridCellSize: number = 2;
|
2022-10-22 14:32:33 +00:00
|
|
|
|
|
|
|
const defaultLayout: Layout = [
|
2022-10-25 08:37:25 +00:00
|
|
|
{ i: 'Maker', w: 10, h: 16, x: 67, y: 0, minW: 8, maxW: 22, minH: 10, maxH: 28 },
|
|
|
|
{ i: 'Book', w: 43, h: 15, x: 34, y: 16, minW: 6, maxW: 70, minH: 9, maxH: 25 },
|
|
|
|
{ i: 'DepthChart', w: 15, h: 10, x: 19, y: 16, minW: 6, maxW: 22, minH: 9, maxH: 25 },
|
|
|
|
{ i: 'Garage', w: 52, h: 16, x: 0, y: 0, minW: 15, maxW: 78, minH: 8, maxH: 30 },
|
|
|
|
{ i: 'History', w: 10, h: 10, x: 9, y: 16, minW: 6, maxW: 22, minH: 9, maxH: 25 },
|
|
|
|
{ i: 'Trade', w: 15, h: 16, x: 52, y: 0, minW: 6, maxW: 22, minH: 9, maxH: 25 },
|
|
|
|
{ i: 'Settings', w: 9, h: 15, x: 0, y: 16, minW: 6, maxW: 22, minH: 9, maxH: 25 },
|
|
|
|
{ i: 'Other', w: 25, h: 5, x: 9, y: 26, minW: 2, maxW: 50, minH: 4, maxH: 25 },
|
2022-10-22 14:32:33 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// All app data structured
|
|
|
|
const [book, setBook] = useState<Book>({ orders: [], loading: true });
|
|
|
|
const [limits, setLimits] = useState<{ list: LimitList; loading: boolean }>({
|
|
|
|
list: [],
|
|
|
|
loading: true,
|
|
|
|
});
|
|
|
|
const [robot, setRobot] = useState<Robot>(defaultRobot);
|
|
|
|
const [maker, setMaker] = useState<Maker>(defaultMaker);
|
|
|
|
const [info, setInfo] = useState<Info>(defaultInfo);
|
|
|
|
const [fav, setFav] = useState<Favorites>({ type: null, currency: 0 });
|
|
|
|
const [layout, setLayout] = useState<Layout>(defaultLayout);
|
|
|
|
|
|
|
|
const [openLanding, setOpenLanding] = useState<boolean>(true);
|
|
|
|
const [windowSize, setWindowSize] = useState<{ width: number; height: number }>(
|
2022-10-23 18:29:39 +00:00
|
|
|
getWindowSize(em),
|
2022-10-22 14:32:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof window !== undefined) {
|
|
|
|
window.addEventListener('resize', onResize);
|
|
|
|
}
|
|
|
|
fetchLimits();
|
|
|
|
fetchBook();
|
2022-10-23 18:29:39 +00:00
|
|
|
fetchInfo();
|
2022-10-22 14:32:33 +00:00
|
|
|
return () => {
|
|
|
|
if (typeof window !== undefined) {
|
|
|
|
window.removeEventListener('resize', onResize);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onResize = function () {
|
2022-10-23 18:29:39 +00:00
|
|
|
setWindowSize(getWindowSize(em));
|
2022-10-22 14:32:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const fetchLimits = async () => {
|
|
|
|
setLimits({ ...limits, loading: true });
|
|
|
|
const data = apiClient.get('/api/limits/').then((data) => {
|
|
|
|
setLimits({ list: data ?? [], loading: false });
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
return await data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchBook = function () {
|
|
|
|
setBook({ ...book, loading: true });
|
|
|
|
apiClient.get('/api/book/').then((data: any) =>
|
|
|
|
setBook({
|
|
|
|
loading: false,
|
|
|
|
orders: data.not_found ? [] : data,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-23 18:29:39 +00:00
|
|
|
const fetchInfo = function () {
|
|
|
|
apiClient.get('/api/info/').then((data: any) => {
|
|
|
|
const versionInfo: any = checkVer(data.version.major, data.version.minor, data.version.patch);
|
|
|
|
setInfo({
|
|
|
|
...data,
|
|
|
|
openUpdateClient: versionInfo.updateAvailable,
|
|
|
|
coordinatorVersion: versionInfo.coordinatorVersion,
|
|
|
|
clientVersion: versionInfo.clientVersion,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2022-10-25 08:37:25 +00:00
|
|
|
|
|
|
|
console.log(layout);
|
2022-10-22 14:32:33 +00:00
|
|
|
return (
|
2022-10-23 18:29:39 +00:00
|
|
|
<Grid container direction='column' sx={{ width: `${windowSize.width}em` }}>
|
2022-10-22 14:32:33 +00:00
|
|
|
<Grid item>
|
2022-10-23 18:29:39 +00:00
|
|
|
<ToolBar height={`${toolbarHeight}em`} settings={settings} setSettings={setSettings} />
|
2022-10-22 14:32:33 +00:00
|
|
|
<LandingDialog open={openLanding} onClose={() => setOpenLanding(!openLanding)} />
|
|
|
|
</Grid>
|
|
|
|
|
2022-10-23 18:29:39 +00:00
|
|
|
<Grid item>
|
2022-10-25 08:37:25 +00:00
|
|
|
<StyledRGL
|
|
|
|
height={windowSize.height - toolbarHeight}
|
|
|
|
width={Number((windowSize.width / gridCellSize).toFixed()) * gridCellSize * em}
|
|
|
|
theme={theme}
|
|
|
|
freeze={!settings.freezeViewports}
|
|
|
|
gridCellSize={gridCellSize}
|
2022-10-22 14:32:33 +00:00
|
|
|
className='layout'
|
|
|
|
layout={layout}
|
2022-10-25 08:37:25 +00:00
|
|
|
cols={Number((windowSize.width / gridCellSize).toFixed())} // cols are 2em wide
|
2022-10-23 18:29:39 +00:00
|
|
|
margin={[0.5 * em, 0.5 * em]}
|
2022-10-22 14:32:33 +00:00
|
|
|
isDraggable={!settings.freezeViewports}
|
|
|
|
isResizable={!settings.freezeViewports}
|
2022-10-25 08:37:25 +00:00
|
|
|
rowHeight={gridCellSize * em} // rows are 2em high
|
2022-10-22 14:32:33 +00:00
|
|
|
autoSize={true}
|
|
|
|
onLayoutChange={(layout: Layout) => setLayout(layout)}
|
|
|
|
>
|
2022-10-23 18:29:39 +00:00
|
|
|
<div key='Maker'>
|
2022-10-22 14:32:33 +00:00
|
|
|
<MakerWidget
|
|
|
|
limits={limits}
|
|
|
|
fetchLimits={fetchLimits}
|
|
|
|
fav={fav}
|
|
|
|
setFav={setFav}
|
|
|
|
maker={maker}
|
|
|
|
setMaker={setMaker}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-23 18:29:39 +00:00
|
|
|
<div key='Book'>
|
2022-10-22 14:32:33 +00:00
|
|
|
<BookWidget
|
|
|
|
book={book}
|
2022-10-23 18:29:39 +00:00
|
|
|
layout={layout[1]}
|
2022-10-25 08:37:25 +00:00
|
|
|
gridCellSize={gridCellSize}
|
2022-10-22 14:32:33 +00:00
|
|
|
fetchBook={fetchBook}
|
|
|
|
fav={fav}
|
|
|
|
setFav={setFav}
|
|
|
|
windowSize={windowSize}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-23 18:29:39 +00:00
|
|
|
<div key='DepthChart'>
|
|
|
|
<DepthChartWidget
|
|
|
|
orders={book.orders}
|
2022-10-25 08:37:25 +00:00
|
|
|
gridCellSize={gridCellSize}
|
2022-10-23 18:29:39 +00:00
|
|
|
limitList={limits.list}
|
|
|
|
layout={layout[2]}
|
|
|
|
currency={fav.currency}
|
|
|
|
windowSize={windowSize}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
<div key='Garage'>
|
2022-10-23 18:29:39 +00:00
|
|
|
<PlaceholderWidget label='Robot Garage' />
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
<div key='History'>
|
2022-10-23 18:29:39 +00:00
|
|
|
<PlaceholderWidget label='Garage History' />
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
<div key='Trade'>
|
2022-10-23 18:29:39 +00:00
|
|
|
<PlaceholderWidget label='Trade Box' />
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
<div key='Settings'>
|
2022-10-23 18:29:39 +00:00
|
|
|
<PlaceholderWidget label='Settings' />
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
<div key='Other'>
|
2022-10-23 18:29:39 +00:00
|
|
|
<PlaceholderWidget label='Other' />
|
|
|
|
</div>
|
2022-10-25 08:37:25 +00:00
|
|
|
</StyledRGL>
|
2022-10-22 14:32:33 +00:00
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
2022-10-20 18:06:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Main;
|