Skip to content

Commit

Permalink
fix(app/automatic/page): ensure window.location is defined
Browse files Browse the repository at this point in the history
* Replaced custom input logic with reusable SetpointInput component
* Introduced mount-time check
    *   ensure `window.location` is accessed only on the client
  • Loading branch information
FelizCoder committed Jan 17, 2025
1 parent 3676121 commit 8c30717
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 48 deletions.
60 changes: 12 additions & 48 deletions app/automatic/page.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
"use client";

import { use, useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { FlowmeterWidgets } from "../sensors/ui/flowmeterWidgets";
import { InputConfirmed } from "../actuators/ui/inputConfirmed";
import useWebSocket from "../hooks/useWebSocket";
import { handleSetpointChange } from "./apiCalls";
import { Switch } from "antd";
import SetpointInput from "./setpointInput";

export default function Page() {
let [setpoint, setSetpoint] = useState<number>(0);

const { data: websocketState, error } = useWebSocket<number | null>({
hostname: window.location.hostname,
route: "/v1/sensors/flowmeters/ws/setpoint/0",
});
const [isMounted, setIsMounted] = useState(false);

useEffect(() => {
if (typeof websocketState === "number") {
setSetpoint(websocketState);
}
}, [websocketState]);

const onSetpointChange = (newSetpoint: number) => {
setSetpoint(newSetpoint);
};

const onSetpointConfirm = () => {
handleSetpointChange(setpoint, 0);
};

const handleToggle = (checked: boolean) => {
if (checked) {
handleSetpointChange(setpoint, 0);
} else {
handleSetpointChange(null, 0);
}
};
setIsMounted(true);
}, []);

return (
<div>
Expand All @@ -47,23 +21,13 @@ export default function Page() {
Flowmeters
</h2>
<FlowmeterWidgets />
</div>
<div>
<Switch
checked={typeof websocketState === "number"}
onClick={(checked) => handleToggle(checked)}
/>
</div>
<div>
<InputConfirmed
min={0.0}
max={25}
precision={2}
value={setpoint}
onValueChange={onSetpointChange}
onConfirm={onSetpointConfirm}
disabled={!(typeof websocketState === "number")}
/>

{isMounted && (
<SetpointInput
websocketHostname={window.location.hostname}
route="/v1/sensors/flowmeters/ws/setpoint/0"
/>
)}
</div>
</div>
);
Expand Down
70 changes: 70 additions & 0 deletions app/automatic/setpointInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use client";

import React, { useEffect, useState } from "react";
import useWebSocket from "../hooks/useWebSocket";
import { handleSetpointChange } from "./apiCalls";
import { Switch } from "antd";
import { InputConfirmed } from "../actuators/ui/inputConfirmed";

export interface SetpointInputProps {
websocketHostname: string;
route: string;
}

const SetpointInput: React.FC<SetpointInputProps> = ({
websocketHostname,
route,
}) => {
let [setpoint, setSetpoint] = useState<number>(0);

const { data: websocketState, error } = useWebSocket<number | null>({
hostname: websocketHostname,
route: route,
});

useEffect(() => {
if (typeof websocketState === "number") {
setSetpoint(websocketState);
}
}, [websocketState]);

const onSetpointChange = (newSetpoint: number) => {
setSetpoint(newSetpoint);
};

const onSetpointConfirm = () => {
handleSetpointChange(setpoint, 0);
};

const handleToggle = (checked: boolean) => {
if (checked) {
handleSetpointChange(setpoint, 0);
} else {
handleSetpointChange(null, 0);
}
};

return (
<div>
<div>
<Switch
checked={typeof websocketState === "number"}
onClick={(checked) => handleToggle(checked)}
/>
</div>
<div>
<InputConfirmed
min={0.0}
max={25}
precision={2}
value={setpoint}
onValueChange={onSetpointChange}
onConfirm={onSetpointConfirm}
disabled={!(typeof websocketState === "number")}
/>
</div>
</div>
);
};

export default SetpointInput;

0 comments on commit 8c30717

Please sign in to comment.