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 }