1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import { useCallback, useEffect, useState } from "react";
import { type PlannerSearchResult, type RoutePlan } from "../data/PlannerApi";
import { usePlanQuery } from "./usePlanQuery";
const STORAGE_KEY = "planner_last_route";
const EXPIRY_MS = 2 * 60 * 60 * 1000; // 2 hours
interface StoredRoute {
timestamp: number;
origin: PlannerSearchResult;
destination: PlannerSearchResult;
plan: RoutePlan;
searchTime?: Date;
arriveBy?: boolean;
selectedItineraryIndex?: number;
}
export function usePlanner() {
const [origin, setOrigin] = useState<PlannerSearchResult | null>(null);
const [destination, setDestination] = useState<PlannerSearchResult | null>(
null
);
const [plan, setPlan] = useState<RoutePlan | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [searchTime, setSearchTime] = useState<Date | null>(null);
const [arriveBy, setArriveBy] = useState(false);
const [selectedItineraryIndex, setSelectedItineraryIndex] = useState<
number | null
>(null);
const {
data: queryPlan,
isLoading: queryLoading,
error: queryError,
isFetching,
} = usePlanQuery(
origin?.lat,
origin?.lon,
destination?.lat,
destination?.lon,
searchTime ?? undefined,
arriveBy,
!!(origin && destination)
);
// Sync query result to local state and storage
useEffect(() => {
if (queryPlan) {
setPlan(queryPlan as any); // Cast because of slight type differences if any, but they should match now
if (origin && destination) {
const toStore: StoredRoute = {
timestamp: Date.now(),
origin,
destination,
plan: queryPlan as any,
searchTime: searchTime ?? new Date(),
arriveBy,
selectedItineraryIndex: selectedItineraryIndex ?? undefined,
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(toStore));
}
}
}, [
queryPlan,
origin,
destination,
searchTime,
arriveBy,
selectedItineraryIndex,
]);
// Load from storage on mount
useEffect(() => {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
try {
const data: StoredRoute = JSON.parse(stored);
if (Date.now() - data.timestamp < EXPIRY_MS) {
setOrigin(data.origin);
setDestination(data.destination);
setPlan(data.plan);
setSearchTime(data.searchTime ? new Date(data.searchTime) : null);
setArriveBy(data.arriveBy ?? false);
setSelectedItineraryIndex(data.selectedItineraryIndex ?? null);
} else {
localStorage.removeItem(STORAGE_KEY);
}
} catch (e) {
localStorage.removeItem(STORAGE_KEY);
}
}
}, []);
const searchRoute = async (
from: PlannerSearchResult,
to: PlannerSearchResult,
time?: Date,
arriveByParam: boolean = false
) => {
setOrigin(from);
setDestination(to);
setSearchTime(time ?? new Date());
setArriveBy(arriveByParam);
setSelectedItineraryIndex(null);
};
const clearRoute = () => {
setPlan(null);
setOrigin(null);
setDestination(null);
setSearchTime(null);
setArriveBy(false);
setSelectedItineraryIndex(null);
localStorage.removeItem(STORAGE_KEY);
};
const selectItinerary = useCallback((index: number) => {
setSelectedItineraryIndex(index);
// Update storage
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
try {
const data: StoredRoute = JSON.parse(stored);
data.selectedItineraryIndex = index;
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
} catch (e) {
// Ignore
}
}
}, []);
const deselectItinerary = useCallback(() => {
setSelectedItineraryIndex(null);
// Update storage
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
try {
const data: StoredRoute = JSON.parse(stored);
data.selectedItineraryIndex = undefined;
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
} catch (e) {
// Ignore
}
}
}, []);
return {
origin,
setOrigin,
destination,
setDestination,
plan,
loading: queryLoading || (isFetching && !plan),
error: queryError ? "Failed to calculate route. Please try again." : null,
searchTime,
arriveBy,
selectedItineraryIndex,
searchRoute,
clearRoute,
selectItinerary,
deselectItinerary,
};
}
|