Skip to content

Commit 0526630

Browse files
m4dm4rtig4nClément VALENTINclaude
authored
fix(simulator): resolve race condition in auto-launch with cached data (#72)
The simulator was not displaying cached data on first load due to a race condition where the initialization timeout completed before energy offers/providers queries finished loading. Now the initialization properly waits for these queries to complete before attempting auto-launch. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Clément VALENTIN <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent fc910ed commit 0526630

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

apps/web/src/pages/Simulator.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export default function Simulator() {
141141
})
142142

143143
// Fetch energy providers and offers
144-
const { data: providersData } = useQuery({
144+
const { data: providersData, isLoading: providersLoading } = useQuery({
145145
queryKey: ['energy-providers'],
146146
queryFn: async () => {
147147
const response = await energyApi.getProviders()
@@ -152,7 +152,7 @@ export default function Simulator() {
152152
},
153153
})
154154

155-
const { data: offersData } = useQuery({
155+
const { data: offersData, isLoading: offersLoading } = useQuery({
156156
queryKey: ['energy-offers'],
157157
queryFn: async () => {
158158
const response = await energyApi.getOffers()
@@ -213,13 +213,18 @@ export default function Simulator() {
213213
setIsInitializing(true)
214214
}, [selectedPdl])
215215

216-
// End initialization after cache has time to hydrate
216+
// End initialization when required data is loaded (offers and providers)
217+
// This ensures auto-launch can check cache properly before showing empty state
217218
useEffect(() => {
218-
const timer = setTimeout(() => {
219-
setIsInitializing(false)
220-
}, 100) // Short delay for cache hydration
221-
return () => clearTimeout(timer)
222-
}, [selectedPdl])
219+
// Wait for offers and providers to finish loading
220+
if (!offersLoading && !providersLoading) {
221+
// Small delay to allow cache hydration to complete
222+
const timer = setTimeout(() => {
223+
setIsInitializing(false)
224+
}, 50)
225+
return () => clearTimeout(timer)
226+
}
227+
}, [selectedPdl, offersLoading, providersLoading])
223228

224229
// Auto-collapse info section when simulation results are available
225230
useEffect(() => {
@@ -774,6 +779,8 @@ export default function Simulator() {
774779
pdlsDataLoaded: !!pdlsData,
775780
offersDataLoaded: !!offersData,
776781
providersDataLoaded: !!providersData,
782+
offersLoading,
783+
providersLoading,
777784
})
778785

779786
// Don't auto-launch if already launched, simulating, or have results
@@ -782,6 +789,12 @@ export default function Simulator() {
782789
return
783790
}
784791

792+
// Don't auto-launch while data is still loading
793+
if (offersLoading || providersLoading) {
794+
logger.log('[Auto-launch] Skipping auto-launch - still loading data')
795+
return
796+
}
797+
785798
// Don't auto-launch if PDL data, offers, or providers are not loaded yet
786799
if (!pdlsData || !Array.isArray(offersData) || offersData.length === 0 || !providersData) {
787800
logger.log('[Auto-launch] Skipping auto-launch - data not loaded yet', {
@@ -822,7 +835,7 @@ export default function Simulator() {
822835
} else {
823836
logger.log(`❌ Not enough cached data (${totalPoints} points), skipping auto-launch`)
824837
}
825-
}, [selectedPdl, isSimulating, simulationResult, hasAutoLaunched, isDemo, pdlsData, offersData, providersData, queryClient, handleSimulation])
838+
}, [selectedPdl, isSimulating, simulationResult, hasAutoLaunched, isDemo, pdlsData, offersData, providersData, offersLoading, providersLoading, queryClient, handleSimulation])
826839

827840
// Filter and sort simulation results
828841
// IMPORTANT: This hook must be before any early returns to respect React's rules of hooks

0 commit comments

Comments
 (0)