Skip to content

Commit fe9f55a

Browse files
authored
Set up PodContext to provide data & socket client (#88)
- Create a context provider to access the pod data and socket client from child components within App - Update `ControlPanel` and `StatusIndicator` to access the context - Move `SensorData` and `StatusIndicator` to `Dashboard`
1 parent eadf30a commit fe9f55a

File tree

7 files changed

+42
-27
lines changed

7 files changed

+42
-27
lines changed

control-station/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

control-station/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"eslint-plugin-react": "^7.33.2",
2727
"eslint-plugin-react-hooks": "^4.6.0",
2828
"eslint-plugin-react-refresh": "^0.4.3",
29-
"prettier": "3.2.5",
29+
"prettier": "^3.2.5",
3030
"typescript": "^5.0.2",
3131
"vite": "^4.4.5"
3232
}

control-station/src/App.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { ControlPanel, Navbar, SensorData, StatusIndicator } from "@/components";
1+
import { ControlPanel, Navbar } from "@/components";
22
import usePodData from "./services/usePodData";
3+
import PodContext from "./services/PodContext";
4+
import { Dashboard } from "@/views";
35

46
function App() {
57
const { podData, podSocketClient } = usePodData();
68

79
return (
810
<main>
9-
<Navbar />
10-
<SensorData />
11-
<StatusIndicator state={podData.state} />
12-
<ControlPanel podSocketClient={podSocketClient} />
11+
<PodContext.Provider value={{ podData, podSocketClient }}>
12+
<Navbar />
13+
<Dashboard />
14+
<ControlPanel />
15+
</PodContext.Provider>
1316
</main>
1417
);
1518
}

control-station/src/components/ControlPanel/ControlPanel.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { useContext } from "react";
2+
3+
import PodContext from "@/services/PodContext";
4+
15
import "./ControlPanel.css";
2-
import PodSocketClient from "@/services/PodSocketClient";
36

4-
interface ControlPanelProps {
5-
podSocketClient: PodSocketClient;
6-
}
7+
function ControlPanel() {
8+
const { podSocketClient } = useContext(PodContext);
79

8-
function ControlPanel({ podSocketClient }: ControlPanelProps) {
910
return (
1011
<div className="controlpanel">
1112
<button className="button run" onClick={() => podSocketClient.sendRun()}>

control-station/src/components/StatusIndicator/StatusIndicator.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { useContext } from "react";
2+
3+
import PodContext from "@/services/PodContext";
14
import { State } from "@/services/PodSocketClient";
5+
26
import "./StatusIndicator.css";
37

4-
interface StatusIndicatorProps {
5-
state: State;
6-
}
8+
function StatusIndicator() {
9+
const { podData } = useContext(PodContext);
10+
const { state } = podData;
711

8-
function StatusIndicator({ state }: StatusIndicatorProps) {
912
return (
1013
<div className="status-indicator">
1114
{Object.values(State).map((s) => {
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createContext } from "react";
2+
3+
import PodSocketClient, { PodData } from "./PodSocketClient";
4+
5+
interface PodContext {
6+
podSocketClient: PodSocketClient;
7+
podData: Readonly<PodData>;
8+
}
9+
10+
// Initialize with unusable object assuming proper values are always provided
11+
const PodContext = createContext<PodContext>({
12+
podSocketClient: {} as PodSocketClient,
13+
podData: {} as PodData,
14+
});
15+
16+
export default PodContext;

control-station/src/views/Dashboard/Dashboard.tsx

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import { Status } from "@/components";
2-
import usePodData from "@/services/usePodData";
1+
import { SensorData, StatusIndicator } from "@/components";
32

43
function Dashboard() {
5-
const { podData, podSocketClient } = usePodData();
6-
74
return (
85
<div>
9-
<h1>Dashboard</h1>
10-
<Status />
11-
<p>{podData.connected ? "connected" : "disconnected"}</p>
12-
<button onClick={() => podSocketClient.sendStop()}>Send Stop</button>
13-
<button onClick={() => podSocketClient.sendHalt()}>Send Halt</button>
14-
<button onClick={() => podSocketClient.sendLoad()}>Send Load</button>
15-
<button onClick={() => podSocketClient.sendRun()}>Send Run</button>
6+
<SensorData />
7+
<StatusIndicator />
168
</div>
179
);
1810
}

0 commit comments

Comments
 (0)