robosats/frontend/src/pro/Main.tsx

213 lines
6.6 KiB
TypeScript
Raw Normal View History

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';
Refactor and Init RoboSats PRO (#296) commit 9c6d55cfc77d42471da3e865f2729167597868e5 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 10:35:49 2022 -0700 Small fixes commit 23d6c00ccb5e78593e768c36b866d02f26031e7b Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 06:12:42 2022 -0700 Refactor frontend commit b2c21d4a98c49f6168bc3ff6e6a3d7b9f8943a12 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 07:26:00 2022 -0700 Small fixes (more) commit 78a8ab799dc33e8f8b8f14e22e155bbc7104c3a9 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 02:11:03 2022 -0700 Try out to revert depth chart commit ef73c980a8cfc4ae760e720e3bca99acc30b7270 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 11:43:37 2022 -0700 Small fixes commit fa3e60208f8f292256dd90813e7beff15db3057a Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 09:43:03 2022 -0700 Add old UserGen and BottomBar to new main.tsx commit 1e257d1924df20e2fa4feb7f6afce4f31f2a9acc Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 04:01:53 2022 -0700 Add Maker and Book page to new main.tsx commit 037d46ceef34df09530e645a2e01c9fbd9b3efd4 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 08:54:55 2022 -0700 Add Main component WIP commit e43b274c33a75ab5050be360a3d01f655e1e8142 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 04:32:43 2022 -0700 App as functional component
2022-10-20 18:06:16 +00:00
import { apiClient } from '../services/api';
import checkVer from '../utils/checkVer';
Refactor and Init RoboSats PRO (#296) commit 9c6d55cfc77d42471da3e865f2729167597868e5 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 10:35:49 2022 -0700 Small fixes commit 23d6c00ccb5e78593e768c36b866d02f26031e7b Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 06:12:42 2022 -0700 Refactor frontend commit b2c21d4a98c49f6168bc3ff6e6a3d7b9f8943a12 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 07:26:00 2022 -0700 Small fixes (more) commit 78a8ab799dc33e8f8b8f14e22e155bbc7104c3a9 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 02:11:03 2022 -0700 Try out to revert depth chart commit ef73c980a8cfc4ae760e720e3bca99acc30b7270 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 11:43:37 2022 -0700 Small fixes commit fa3e60208f8f292256dd90813e7beff15db3057a Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 09:43:03 2022 -0700 Add old UserGen and BottomBar to new main.tsx commit 1e257d1924df20e2fa4feb7f6afce4f31f2a9acc Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 04:01:53 2022 -0700 Add Maker and Book page to new main.tsx commit 037d46ceef34df09530e645a2e01c9fbd9b3efd4 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 08:54:55 2022 -0700 Add Main component WIP commit e43b274c33a75ab5050be360a3d01f655e1e8142 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 04:32:43 2022 -0700 App as functional component
2022-10-20 18:06:16 +00:00
import {
Book,
LimitList,
Maker,
Robot,
Info,
Settings,
Favorites,
defaultMaker,
defaultRobot,
defaultInfo,
} from '../models';
import { PlaceholderWidget, MakerWidget, BookWidget, DepthChartWidget } from '../pro/Widgets';
import ToolBar from '../pro/ToolBar';
import LandingDialog from '../pro/LandingDialog';
Refactor and Init RoboSats PRO (#296) commit 9c6d55cfc77d42471da3e865f2729167597868e5 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 10:35:49 2022 -0700 Small fixes commit 23d6c00ccb5e78593e768c36b866d02f26031e7b Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 06:12:42 2022 -0700 Refactor frontend commit b2c21d4a98c49f6168bc3ff6e6a3d7b9f8943a12 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 07:26:00 2022 -0700 Small fixes (more) commit 78a8ab799dc33e8f8b8f14e22e155bbc7104c3a9 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 02:11:03 2022 -0700 Try out to revert depth chart commit ef73c980a8cfc4ae760e720e3bca99acc30b7270 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 11:43:37 2022 -0700 Small fixes commit fa3e60208f8f292256dd90813e7beff15db3057a Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 09:43:03 2022 -0700 Add old UserGen and BottomBar to new main.tsx commit 1e257d1924df20e2fa4feb7f6afce4f31f2a9acc Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 04:01:53 2022 -0700 Add Maker and Book page to new main.tsx commit 037d46ceef34df09530e645a2e01c9fbd9b3efd4 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 08:54:55 2022 -0700 Add Main component WIP commit e43b274c33a75ab5050be360a3d01f655e1e8142 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 04:32:43 2022 -0700 App as functional component
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,
};
};
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;
`,
);
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;
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 },
];
// 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 }>(
getWindowSize(em),
);
useEffect(() => {
if (typeof window !== undefined) {
window.addEventListener('resize', onResize);
}
fetchLimits();
fetchBook();
fetchInfo();
return () => {
if (typeof window !== undefined) {
window.removeEventListener('resize', onResize);
}
};
}, []);
const onResize = function () {
setWindowSize(getWindowSize(em));
};
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,
}),
);
};
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);
return (
<Grid container direction='column' sx={{ width: `${windowSize.width}em` }}>
<Grid item>
<ToolBar height={`${toolbarHeight}em`} settings={settings} setSettings={setSettings} />
<LandingDialog open={openLanding} onClose={() => setOpenLanding(!openLanding)} />
</Grid>
<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}
className='layout'
layout={layout}
2022-10-25 08:37:25 +00:00
cols={Number((windowSize.width / gridCellSize).toFixed())} // cols are 2em wide
margin={[0.5 * em, 0.5 * em]}
isDraggable={!settings.freezeViewports}
isResizable={!settings.freezeViewports}
2022-10-25 08:37:25 +00:00
rowHeight={gridCellSize * em} // rows are 2em high
autoSize={true}
onLayoutChange={(layout: Layout) => setLayout(layout)}
>
<div key='Maker'>
<MakerWidget
limits={limits}
fetchLimits={fetchLimits}
fav={fav}
setFav={setFav}
maker={maker}
setMaker={setMaker}
/>
</div>
<div key='Book'>
<BookWidget
book={book}
layout={layout[1]}
2022-10-25 08:37:25 +00:00
gridCellSize={gridCellSize}
fetchBook={fetchBook}
fav={fav}
setFav={setFav}
windowSize={windowSize}
/>
</div>
<div key='DepthChart'>
<DepthChartWidget
orders={book.orders}
2022-10-25 08:37:25 +00:00
gridCellSize={gridCellSize}
limitList={limits.list}
layout={layout[2]}
currency={fav.currency}
windowSize={windowSize}
/>
</div>
2022-10-25 08:37:25 +00:00
<div key='Garage'>
<PlaceholderWidget label='Robot Garage' />
</div>
2022-10-25 08:37:25 +00:00
<div key='History'>
<PlaceholderWidget label='Garage History' />
</div>
2022-10-25 08:37:25 +00:00
<div key='Trade'>
<PlaceholderWidget label='Trade Box' />
</div>
2022-10-25 08:37:25 +00:00
<div key='Settings'>
<PlaceholderWidget label='Settings' />
</div>
2022-10-25 08:37:25 +00:00
<div key='Other'>
<PlaceholderWidget label='Other' />
</div>
2022-10-25 08:37:25 +00:00
</StyledRGL>
</Grid>
</Grid>
);
Refactor and Init RoboSats PRO (#296) commit 9c6d55cfc77d42471da3e865f2729167597868e5 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 10:35:49 2022 -0700 Small fixes commit 23d6c00ccb5e78593e768c36b866d02f26031e7b Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 20 06:12:42 2022 -0700 Refactor frontend commit b2c21d4a98c49f6168bc3ff6e6a3d7b9f8943a12 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 07:26:00 2022 -0700 Small fixes (more) commit 78a8ab799dc33e8f8b8f14e22e155bbc7104c3a9 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 19 02:11:03 2022 -0700 Try out to revert depth chart commit ef73c980a8cfc4ae760e720e3bca99acc30b7270 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 11:43:37 2022 -0700 Small fixes commit fa3e60208f8f292256dd90813e7beff15db3057a Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 09:43:03 2022 -0700 Add old UserGen and BottomBar to new main.tsx commit 1e257d1924df20e2fa4feb7f6afce4f31f2a9acc Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Tue Oct 18 04:01:53 2022 -0700 Add Maker and Book page to new main.tsx commit 037d46ceef34df09530e645a2e01c9fbd9b3efd4 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 08:54:55 2022 -0700 Add Main component WIP commit e43b274c33a75ab5050be360a3d01f655e1e8142 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Mon Oct 17 04:32:43 2022 -0700 App as functional component
2022-10-20 18:06:16 +00:00
};
export default Main;