Skip to content

Commit 018069b

Browse files
authored
Merge pull request #133 from HyperloopUPV-H8/ethernet-view/charts-qol
[ethernet-view] Charts QoL
2 parents f25ca75 + af15c0a commit 018069b

16 files changed

Lines changed: 427 additions & 337 deletions

File tree

common-front/package-lock.json

Lines changed: 208 additions & 201 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethernet-view/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethernet-view/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"nock": "^13.2.9",
5252
"sass": "^1.56.1",
5353
"typescript": "^4.6.4",
54-
"vite": "^3.2.7",
54+
"vite": "^3.2.10",
5555
"vitest": "^0.25.2"
5656
}
5757
}

ethernet-view/src/App.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,44 @@ import { TestingPage } from "pages/TestingPage/TestingPage";
33
import { SplashScreen } from "components/SplashScreen/SplashScreen";
44
import { WsHandlerProvider } from "common";
55
import { useLoadBackend } from "common";
6+
import { AppLayout } from "layouts/AppLayout/AppLayout";
7+
import { useState } from "react";
8+
import { LoggerPage } from "pages/LoggerPage/LoggerPage";
69

710

811
function App() {
912

1013
const isProduction = import.meta.env.PROD;
1114
const loadBackend = useLoadBackend(isProduction);
15+
const [pageShown, setPageShown] = useState<string>("testing");
1216

1317
return (
14-
<div className="App">
15-
{loadBackend.state === "fulfilled" &&
16-
<WsHandlerProvider handler={loadBackend.wsHandler}>
17-
<TestingPage />
18-
</WsHandlerProvider>}
19-
{loadBackend.state === "pending" && <SplashScreen />}
20-
{loadBackend.state === "rejected" && <div>{`${loadBackend.error}`}</div>}
21-
</div>
18+
<AppLayout
19+
pageShown={pageShown}
20+
setPageShown={setPageShown}
21+
>
22+
<div
23+
className="App"
24+
style={{
25+
display: pageShown == "testing" ? "block" : "none"
26+
}}
27+
>
28+
{loadBackend.state === "fulfilled" &&
29+
<WsHandlerProvider handler={loadBackend.wsHandler}>
30+
<TestingPage />
31+
</WsHandlerProvider>}
32+
{loadBackend.state === "pending" && <SplashScreen />}
33+
{loadBackend.state === "rejected" && <div>{`${loadBackend.error}`}</div>}
34+
</div>
35+
<div
36+
className="App"
37+
style={{
38+
display: pageShown == "logger" ? "block" : "none"
39+
}}
40+
>
41+
<LoggerPage />
42+
</div>
43+
</AppLayout>
2244
);
2345
}
2446

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 3 deletions
Loading
Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import { MeasurementId, NumericMeasurementInfo, UpdateFunction, useGlobalTicker } from "common";
2-
import { ColorType, IChartApi, ISeriesApi, UTCTimestamp, createChart } from "lightweight-charts";
3-
import { useEffect, useRef } from "react";
1+
import {
2+
MeasurementId,
3+
NumericMeasurementInfo,
4+
UpdateFunction,
5+
useGlobalTicker,
6+
} from 'common';
7+
import {
8+
ColorType,
9+
IChartApi,
10+
ISeriesApi,
11+
UTCTimestamp,
12+
createChart,
13+
} from 'lightweight-charts';
14+
import { useEffect, useRef } from 'react';
415

5-
type DataSerieAndUpdater = Map<MeasurementId, [ISeriesApi<"Line">, UpdateFunction]>;
16+
type DataSerieAndUpdater = Map<
17+
MeasurementId,
18+
[ISeriesApi<'Line'>, UpdateFunction]
19+
>;
620

721
interface Props {
822
measurementsInChart: NumericMeasurementInfo[];
@@ -11,65 +25,85 @@ interface Props {
1125
const CHART_HEIGHT = 300;
1226

1327
export const ChartCanvas = ({ measurementsInChart }: Props) => {
14-
1528
const chart = useRef<IChartApi | null>(null);
1629
const chartContainerRef = useRef<HTMLDivElement>(null);
1730
const chartDataSeries = useRef<DataSerieAndUpdater>(new Map());
1831

1932
useEffect(() => {
2033
const handleResize = () => {
2134
if (chartContainerRef.current)
22-
if(chart)
23-
chart.current?.applyOptions({ width: chartContainerRef.current.clientWidth });
35+
if (chart)
36+
chart.current?.applyOptions({
37+
width: chartContainerRef.current.clientWidth,
38+
});
2439
};
2540

2641
const resizeObserver = new ResizeObserver(handleResize);
2742
if (chartContainerRef.current)
28-
resizeObserver.observe(chartContainerRef.current);
43+
resizeObserver.observe(chartContainerRef.current);
2944

3045
if (chartContainerRef.current) {
31-
if(chart)
32-
chart.current = createChart(chartContainerRef.current, {
33-
layout: {
34-
background: { type: ColorType.Solid, color: "white" },
35-
textColor: "black",
36-
},
37-
width: chartContainerRef.current.clientWidth,
38-
height: CHART_HEIGHT,
39-
timeScale: {
40-
timeVisible: true,
41-
secondsVisible: true,
42-
fixLeftEdge: true,
43-
fixRightEdge: true,
44-
lockVisibleTimeRangeOnResize: true,
45-
rightBarStaysOnScroll: true,
46-
}
47-
});
46+
if (chart) {
47+
chart.current = createChart(chartContainerRef.current, {
48+
layout: {
49+
background: { type: ColorType.Solid, color: 'white' },
50+
textColor: 'black',
51+
},
52+
width: chartContainerRef.current.clientWidth,
53+
height: CHART_HEIGHT,
54+
timeScale: {
55+
timeVisible: true,
56+
secondsVisible: true,
57+
fixLeftEdge: true,
58+
fixRightEdge: true,
59+
lockVisibleTimeRangeOnResize: true,
60+
rightBarStaysOnScroll: true,
61+
tickMarkFormatter: (time: UTCTimestamp) => {
62+
const date = new Date(time * 1000);
63+
return date.toLocaleTimeString() + '.' + date.getMilliseconds();
64+
}
65+
},
66+
localization: {
67+
timeFormatter: (time: UTCTimestamp) => {
68+
const date = new Date(time * 1000);
69+
return date.toLocaleTimeString() + '.' + date.getMilliseconds();
70+
}
71+
}
72+
});
73+
}
4874
}
4975

5076
return () => {
5177
resizeObserver.disconnect();
5278
chart.current?.remove();
53-
}
79+
};
5480
}, []);
5581

5682
useEffect(() => {
5783
chartDataSeries.current.clear();
5884
measurementsInChart.forEach((measurement) => {
59-
if(chart.current)
60-
chartDataSeries.current.set(measurement.id, [chart.current.addLineSeries({color: measurement.color}), measurement.getUpdate]);
85+
if (chart.current)
86+
chartDataSeries.current.set(measurement.id, [
87+
chart.current.addLineSeries({
88+
color: measurement.color,
89+
priceFormat: {
90+
type: 'price',
91+
precision: 3,
92+
minMove: 0.001,
93+
},
94+
}),
95+
measurement.getUpdate,
96+
]);
6197
});
62-
})
98+
});
6399

64100
useGlobalTicker(() => {
65-
const now = Date.now() / 1000 as UTCTimestamp;
101+
const now = (Date.now() / 1000) as UTCTimestamp;
66102
chartDataSeries.current?.forEach((serieAndUpdater) => {
67103
const [DataSerie, Updater] = serieAndUpdater;
68-
DataSerie.update({ time: now, value: Updater() })
104+
DataSerie.update({ time: now, value: Updater() });
69105
});
70106
});
71107

72-
return (
73-
<div ref={chartContainerRef}></div>
74-
);
108+
return <div ref={chartContainerRef}></div>;
75109
};

ethernet-view/src/components/Navbar/Navbar.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import styles from "./Navbar.module.scss";
2-
import { ReactComponent as TeamLogo } from "assets/svg/team_logo.svg";
3-
import { Link } from "react-router-dom";
42
import { NavbarItem, NavbarItemData } from "./NavbarItem/NavbarItem";
53

64
type Props = {
75
items: NavbarItemData[];
6+
pageShown: string;
7+
setPageShown: (page: string) => void;
88
};
99

10-
export const Navbar = ({ items }: Props) => {
10+
export const Navbar = ({ items, pageShown, setPageShown }: Props) => {
1111
return (
1212
<nav className={styles.navbarWrapper}>
13-
<Link to={"/"}>
14-
<TeamLogo className={styles.logo} />
15-
</Link>
1613
<div className={styles.separator} />
1714
<div className={styles.items}>
1815
{items.map((item) => {
1916
return (
2017
<NavbarItem
21-
key={item.path}
2218
item={item}
19+
pageShown={pageShown}
20+
setPageShown={setPageShown}
2321
/>
2422
);
2523
})}

ethernet-view/src/components/Navbar/NavbarItem/NavbarItem.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
> * {
55
width: 2rem;
66
}
7+
cursor: pointer;
78
}
89

910
.link {
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import styles from "components/Navbar/NavbarItem/NavbarItem.module.scss";
2-
import { NavLink } from "react-router-dom";
32

43
export type NavbarItemData = {
5-
path: string;
64
icon: string;
5+
page: string;
76
};
87

98
type Props = {
109
item: NavbarItemData;
10+
pageShown: string;
11+
setPageShown: (page: string) => void;
1112
};
1213

13-
export const NavbarItem = ({ item }: Props) => {
14+
export const NavbarItem = ({ item, pageShown, setPageShown }: Props) => {
15+
16+
const handleClick = () => {
17+
setPageShown(item.page);
18+
}
19+
1420
return (
15-
<NavLink
16-
to={item.path}
17-
className={({isActive}) => isActive ? styles.active : ""}
18-
>
19-
<div className={styles.iconWrapper}>
21+
<div className={pageShown === item.page ? styles.active : ""}>
22+
<div className={styles.iconWrapper} onClick={handleClick}>
2023
<img src={item.icon} alt="icon" />
2124
</div>
22-
</NavLink>
25+
</div>
2326
);
2427
};

0 commit comments

Comments
 (0)