Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/client/src/components/global/SocketManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { socketURL } from "@shared/helios-types";

interface ClientToServerEvents {
ping: (cb: (val: number) => void) => void;
setLapCoords: (coords: CoordInfoUpdate) => void;
setLapCoords: (
coords: CoordInfoUpdate,
callback: (response: CoordUpdateResponse) => void,
) => void;
}

interface ServerToClientEvents {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { useTheme } from "next-themes";
import React from "react";

import { socketIO } from "@/components/global/SocketManager";
import { usePacketStore } from "@/stores/usePacket";
import { helios, heliosCompliment, sand } from "@/styles/colors";
import { notifications } from "@mantine/notifications";
import { Button, TextField } from "@mui/material";
import { CoordUpdateResponse } from "@shared/helios-types";

const SetFinishLineLocation = () => {
const { currentPacket } = usePacketStore();
const { resolvedTheme } = useTheme();
const [password, setPassword] = React.useState("");

const handleSetFinishLineLocation = () => {
socketIO.emit(
"setLapCoords",
{
lat: String(currentPacket?.Telemetry?.GpsLatitude ?? 0),
long: String(currentPacket?.Telemetry?.GpsLongitude ?? 0),
password: password,
},
(response: CoordUpdateResponse) => {
if ("error" in response) {
notifications.show({
color: "red",
message: response.error,
title: "Error Setting Finish Line Location",
});
return;
}
notifications.show({
color: "green",
message: "Finish Line Location Updated Successfully",
title: "Success",
});
},
);
Comment on lines +16 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle invalidFields-only responses as errors.

CoordUpdateResponse can include invalidFields without an error message. In that case, the current logic shows a success notification or an empty error message. Consider treating invalidFields as an error with a fallback message.

💡 Suggested fix
       (response: CoordUpdateResponse) => {
-        if ("error" in response) {
+        if (
+          ("error" in response && response.error) ||
+          ("invalidFields" in response && response.invalidFields?.length)
+        ) {
           notifications.show({
             color: "red",
-            message: response.error,
+            message:
+              response.error ??
+              `Invalid fields: ${response.invalidFields?.join(", ")}`,
             title: "Error Setting Finish Line Location",
           });
           return;
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleSetFinishLineLocation = () => {
socketIO.emit(
"setLapCoords",
{
lat: String(currentPacket?.Telemetry?.GpsLatitude ?? 0),
long: String(currentPacket?.Telemetry?.GpsLongitude ?? 0),
password: password,
},
(response: CoordUpdateResponse) => {
if ("error" in response) {
notifications.show({
color: "red",
message: response.error,
title: "Error Setting Finish Line Location",
});
return;
}
notifications.show({
color: "green",
message: "Finish Line Location Updated Successfully",
title: "Success",
});
},
);
const handleSetFinishLineLocation = () => {
socketIO.emit(
"setLapCoords",
{
lat: String(currentPacket?.Telemetry?.GpsLatitude ?? 0),
long: String(currentPacket?.Telemetry?.GpsLongitude ?? 0),
password: password,
},
(response: CoordUpdateResponse) => {
if (
("error" in response && response.error) ||
("invalidFields" in response && response.invalidFields?.length)
) {
notifications.show({
color: "red",
message:
response.error ??
`Invalid fields: ${response.invalidFields?.join(", ")}`,
title: "Error Setting Finish Line Location",
});
return;
}
notifications.show({
color: "green",
message: "Finish Line Location Updated Successfully",
title: "Success",
});
},
);
};
🤖 Prompt for AI Agents
In
`@packages/client/src/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation.tsx`
around lines 16 - 39, The response handler in handleSetFinishLineLocation treats
any response without an "error" key as success; update the socketIO.emit
callback that receives CoordUpdateResponse to also treat responses containing a
non-empty invalidFields property as errors: check if ("error" in response) ||
("invalidFields" in response && response.invalidFields?.length > 0) and call
notifications.show with a red/error notification using response.error ||
response.invalidFields.join(", ") || "Invalid fields in request" as the message;
only show the green success notification when neither error nor invalidFields
are present.

};

return (
<div className="mb-4 grid grid-cols-2 items-center justify-between gap-4">
<div className="col-span-1">
<label className="mr-2">Update Finish Line Location:</label>
</div>
<div className="col-span-1 flex flex-col items-stretch gap-2 sm:flex-row sm:items-center">
<Button
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im going to create a ticket to remove mui from our repo bro this is crazy

className="h-14 min-w-0 flex-1"
disabled={!password}
onClick={handleSetFinishLineLocation}
sx={{
"&.Mui-disabled": {
borderColor: resolvedTheme === "dark" ? sand : helios,
color: resolvedTheme === "dark" ? sand : helios,
opacity: 0.5,
},
"&:hover": {
backgroundColor:
resolvedTheme === "dark" ? heliosCompliment : helios,
borderColor: resolvedTheme === "dark" ? sand : heliosCompliment,
color: resolvedTheme === "dark" ? sand : "white",
},
borderColor: resolvedTheme === "dark" ? sand : helios,
color: resolvedTheme === "dark" ? sand : helios,
fontSize: { md: "0.875rem", sm: "0.75rem", xs: "0.65rem" },
padding: { sm: "12px", xs: "8px" },
whiteSpace: "normal",
wordWrap: "break-word",
}}
variant="outlined"
>
Set Finish Line Location
</Button>
<TextField
className="min-w-0 flex-1"
label="Password"
onChange={(e) => setPassword(e.target.value)}
sx={{
"& .MuiInputBase-input": {
color: resolvedTheme === "dark" ? "white" : "black",
},
"& .MuiInputBase-input::label": {
color: resolvedTheme === "dark" ? sand : helios,
},
"& .MuiInputLabel-root, & .MuiInputLabel-root.Mui-focused": {
color: resolvedTheme === "dark" ? sand : helios,
},
"& .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline, & .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline":
{
borderColor: resolvedTheme === "dark" ? sand : helios,
},
}}
type="password"
value={password}
/>
</div>
</div>
);
};

export default SetFinishLineLocation;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useTheme } from "next-themes";
import { useCallback, useState } from "react";

import SetFinishLineLocation from "@/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation";
import { APPUNITS, CONNECTIONTYPES, useAppState } from "@/stores/useAppState";
import { helios, heliosCompliment, sand } from "@/styles/colors";
import { TimeInput } from "@mantine/dates";
import SettingsIcon from "@mui/icons-material/Settings";
import Modal from "@mui/material/Modal";
import ToggleButton from "@mui/material/ToggleButton";
Expand Down Expand Up @@ -242,6 +242,7 @@ function SettingsComponent() {
</div>
</div>
<DriverNameUpdate />
<SetFinishLineLocation />
</div>
</Modal>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class LapController implements LapControllerType {
): CoordUpdateResponse {
logger.info(JSON.stringify(newCoordInfo));
const { lat, long, password } = newCoordInfo;
if (password !== process.env.LAP_POSITION_PASSWORD) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are renaming this we have to remove references of lap_position_password

if (password !== process.env.FINISH_LINE_UPDATE_PASSWORD) {
logger.error("Invalid Password: " + password);
return { error: "Invalid Password", invalidFields: ["password"] };
}
Expand Down
33 changes: 23 additions & 10 deletions packages/server/src/datasources/SocketIO/SocketIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,30 @@ export class SocketIO implements SocketIOType {
socket.on("ping", (callback: () => void) => {
callback();
});
socket.on("setLapCoords", (newCoordInfo: CoordInfoUpdate) => {
const { lat, long, password } = newCoordInfo;
logger.info("UPDATED COORDS: ");
logger.info("lat: ", lat, "long: ", long);
const res =
this.backendController.lapController.setFinishLineLocation(
newCoordInfo,
);
socket.on(
"setLapCoords",
(
newCoordInfo: CoordInfoUpdate,
callback?: (response: CoordUpdateResponse) => void,
) => {
const { lat, long } = newCoordInfo;

this.broadcastLapCoords(res);
});
logger.info("UPDATED COORDS: ");
logger.info("lat: ", lat, "long: ", long);
const res =
this.backendController.lapController.setFinishLineLocation(
newCoordInfo,
);
// Only broadcast if successful
if (!("error" in res)) {
this.broadcastLapCoords(res);
}

if (typeof callback === "function") {
callback(res);
}
},
);
socket.on("disconnect", () => {
logger.info("Client disconnected");
});
Expand Down