From 41bfcf2f765b0bc61d30dc51f8ad1b11d8450d9f Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 1 Apr 2026 18:29:40 +0200 Subject: [PATCH] fix: stale closure in updateRouteForDay causes route to disappear on place click MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useCallback captured tripStore at creation time (dep: [routeCalcEnabled]). If assignments were empty on first render (trip still loading), the callback would permanently see empty assignments and call setRoute(null) whenever invoked — e.g. when clicking a place triggers onSelectDay → updateRouteForDay. Fix: store tripStore in a ref updated on every render so the callback always reads the latest assignments without needing to be recreated. --- client/src/hooks/useRouteCalculation.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/client/src/hooks/useRouteCalculation.ts b/client/src/hooks/useRouteCalculation.ts index e78c3fb..60ce403 100644 --- a/client/src/hooks/useRouteCalculation.ts +++ b/client/src/hooks/useRouteCalculation.ts @@ -15,11 +15,14 @@ export function useRouteCalculation(tripStore: TripStoreState, selectedDayId: nu const [routeSegments, setRouteSegments] = useState([]) const routeCalcEnabled = useSettingsStore((s) => s.settings.route_calculation) !== false const routeAbortRef = useRef(null) + // Keep a ref to the latest tripStore so updateRouteForDay never has a stale closure + const tripStoreRef = useRef(tripStore) + tripStoreRef.current = tripStore const updateRouteForDay = useCallback(async (dayId: number | null) => { if (routeAbortRef.current) routeAbortRef.current.abort() if (!dayId) { setRoute(null); setRouteSegments([]); return } - const currentAssignments = tripStore.assignments || {} + const currentAssignments = tripStoreRef.current.assignments || {} const da = (currentAssignments[String(dayId)] || []).slice().sort((a, b) => a.order_index - b.order_index) const waypoints = da.map((a) => a.place).filter((p) => p?.lat && p?.lng) if (waypoints.length < 2) { setRoute(null); setRouteSegments([]); return }