-
Notifications
You must be signed in to change notification settings - Fork 0
Tel 298 switch from contexts to zustand #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexwhelan12
wants to merge
4
commits into
main
Choose a base branch
from
TEL-298-Switch-from-contexts-to-Zustand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/client/src/components/containers/BottomInformationContainer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/client/src/components/global/AppStateEffectsManager.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect } from "react"; | ||
|
|
||
| import { CONNECTIONTYPES, useAppState } from "@/stores/useAppState"; | ||
|
|
||
| export default function AppStateEffects() { | ||
| const { currentAppState, setCurrentAppState } = useAppState(); | ||
|
|
||
| useEffect(() => { | ||
| if (currentAppState.connectionType === CONNECTIONTYPES.DEMO) { | ||
| if (currentAppState.socketConnected) { | ||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| connectionType: CONNECTIONTYPES.NETWORK, | ||
| loading: false, | ||
| })); | ||
| } | ||
| if (currentAppState.radioConnected) { | ||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| connectionType: CONNECTIONTYPES.RADIO, | ||
| loading: false, | ||
| })); | ||
| } | ||
| } | ||
| if (currentAppState.connectionType === CONNECTIONTYPES.NETWORK) { | ||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| loading: !currentAppState.socketConnected, | ||
| })); | ||
| } | ||
| if (currentAppState.connectionType === CONNECTIONTYPES.RADIO) { | ||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| loading: !currentAppState.radioConnected, | ||
| })); | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [ | ||
| currentAppState.socketConnected, | ||
| currentAppState.radioConnected, | ||
| setCurrentAppState, | ||
| ]); | ||
|
|
||
| useEffect(() => { | ||
| setTimeout(() => { | ||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| loading: false, | ||
| })); | ||
| }, 5000); | ||
| }, [setCurrentAppState]); | ||
|
|
||
| const fetchSettingsFromLocalStorage = useCallback(() => { | ||
| const savedSettings = localStorage.getItem("settings"); | ||
| const favourites = localStorage.getItem("favourites"); | ||
|
|
||
| if (savedSettings) { | ||
| const parsedSettings = JSON.parse(savedSettings); | ||
|
|
||
| const parsedFavourites = favourites | ||
| ? (JSON.parse(favourites) as string[]) | ||
| : [ | ||
| "Motor Temp", | ||
| "Battery Cell Voltage", | ||
| "Vehicle Velocity", | ||
| "Pack Voltage", | ||
| "Pack Current", | ||
| "Battery Average Voltage", | ||
| ]; | ||
|
|
||
| const hasPlaybackDateTime = !!parsedSettings.playbackDateTime; | ||
|
|
||
| const parsedPlaybackDateTime = hasPlaybackDateTime | ||
| ? { | ||
| date: parsedSettings.playbackDateTime!.date | ||
| ? new Date(parsedSettings.playbackDateTime!.date) | ||
| : null, | ||
| endTime: parsedSettings.playbackDateTime!.endTime | ||
| ? new Date(parsedSettings.playbackDateTime!.endTime) | ||
| : null, | ||
| startTime: parsedSettings.playbackDateTime!.startTime | ||
| ? new Date(parsedSettings.playbackDateTime!.startTime) | ||
| : null, | ||
| } | ||
| : { | ||
| date: null, | ||
| endTime: null, | ||
| startTime: null, | ||
| }; | ||
|
|
||
| setCurrentAppState((prev) => ({ | ||
| ...prev, | ||
| appUnits: parsedSettings.appUnits ?? prev.appUnits, | ||
| connectionType: parsedSettings.connectionType ?? prev.connectionType, | ||
| favourites: parsedFavourites, | ||
| lapCoords: parsedSettings.lapCoords ?? prev.lapCoords, | ||
| playbackDateTime: parsedPlaybackDateTime, | ||
| })); | ||
| } | ||
| }, [setCurrentAppState]); | ||
|
|
||
| const saveSettingsToLocalStorage = useCallback(() => { | ||
| localStorage.setItem("settings", JSON.stringify(currentAppState)); | ||
| }, [currentAppState]); | ||
|
|
||
| useEffect(() => { | ||
| fetchSettingsFromLocalStorage(); | ||
| }, [fetchSettingsFromLocalStorage]); | ||
|
|
||
| useEffect(() => { | ||
| if (!currentAppState.loading) { | ||
| saveSettingsToLocalStorage(); | ||
| } | ||
| }, [currentAppState.loading, saveSettingsToLocalStorage]); | ||
|
|
||
| return null; // This component only handles side effects | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import AppStateEffectsManager from "@/components/global/AppStateEffectsManager"; | ||
| import { LapListenerManager } from "@/components/global/LapDataListenerManager"; | ||
| import { PacketListenerManager } from "@/components/global/PacketListenerManager"; | ||
| import SocketManager from "@/components/global/SocketManager"; | ||
|
|
||
| export function EffectsProvider() { | ||
| return ( | ||
| <> | ||
| <LapListenerManager /> | ||
| <PacketListenerManager /> | ||
| <AppStateEffectsManager /> | ||
| <SocketManager /> | ||
| </> | ||
| ); | ||
| } |
67 changes: 67 additions & 0 deletions
67
packages/client/src/components/global/LapDataListenerManager.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { useEffect } from "react"; | ||
|
|
||
| import { socketIO } from "@/components/global/SocketManager"; | ||
| import { CONNECTIONTYPES, useAppState } from "@/stores/useAppState"; | ||
| import { formatLapData, useLapDataStore } from "@/stores/useLapData"; | ||
| import { notifications } from "@mantine/notifications"; | ||
| import type { ILapData } from "@shared/helios-types"; | ||
|
|
||
| export function LapListenerManager(): React.ReactElement | null { | ||
| const { currentAppState } = useAppState(); | ||
| const { addLapData, clearLapData, fetchLapData } = useLapDataStore(); | ||
|
|
||
| // Fetch initial lap data when manager mounts | ||
| useEffect(() => { | ||
| fetchLapData(); | ||
| }, [fetchLapData]); | ||
|
|
||
| // Handle connection type changes | ||
| useEffect(() => { | ||
| // Playback mode: no listeners, clear data | ||
| if (currentAppState.playbackSwitch) { | ||
| clearLapData(); | ||
| return; | ||
| } | ||
|
|
||
| // Network mode: attach socket listeners | ||
| if (currentAppState.connectionType === CONNECTIONTYPES.NETWORK) { | ||
| const handleLapData = (lapPacket: ILapData) => { | ||
| const formattedData = formatLapData(lapPacket); | ||
| addLapData(formattedData); | ||
| }; | ||
|
|
||
| const handleLapComplete = () => { | ||
| notifications.show({ | ||
| color: "green", | ||
| message: "A lap has been completed!", | ||
| title: "Lap Completion", | ||
| }); | ||
| }; | ||
|
|
||
| socketIO.on("lapData", handleLapData); | ||
| socketIO.on("lapComplete", handleLapComplete); | ||
|
|
||
| return () => { | ||
| socketIO.off("lapData", handleLapData); | ||
| socketIO.off("lapComplete", handleLapComplete); | ||
| }; | ||
| } | ||
|
|
||
| // Demo mode: clear existing lap data | ||
| else if (currentAppState.connectionType === CONNECTIONTYPES.DEMO) { | ||
| clearLapData(); | ||
| } | ||
|
|
||
| // Radio mode: TODO | ||
| else if (currentAppState.connectionType === CONNECTIONTYPES.RADIO) { | ||
| // TODO: handle radio | ||
| } | ||
| }, [ | ||
| currentAppState.connectionType, | ||
| currentAppState.playbackSwitch, | ||
| addLapData, | ||
| clearLapData, | ||
| ]); | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unconditionally clearing
loadingmasks real connection failures.This effect always flips
loadingtofalseafter five seconds, even ifsocketConnected/radioConnectedare stillfalse. As soon as a connection stalls, the spinner disappears forever andcurrentAppState.loadingreports a success that never happened. Guard the timeout so it only clears loading after a successful connection (or cancel it when connectivity is still down), and ensure you clear the timer on unmount.🤖 Prompt for AI Agents