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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
import { REGION_DATA } from "~/config/RegionConfig";
export interface CachedStopList {
timestamp: number;
data: Stop[];
}
export type StopName = {
original: string;
intersect?: string;
};
export interface Stop {
stopId: string;
type?: "bus" | "train";
name: StopName;
latitude?: number;
longitude?: number;
lines: string[];
favourite?: boolean;
amenities?: string[];
title?: string;
message?: string;
alert?: "info" | "warning" | "error";
cancelled?: boolean;
}
// In-memory cache and lookup map per region
const cachedStopsByRegion: Record<string, Stop[] | null> = {};
const stopsMapByRegion: Record<string, Record<string, Stop>> = {};
// Custom names loaded from localStorage per region
const customNamesByRegion: Record<string, Record<string, string>> = {};
// Helper to normalize ID
function normalizeId(id: number | string): string {
const s = String(id);
if (s.includes(":")) return s;
return `vitrasa:${s}`;
}
// Initialize cachedStops and customNames once per region
async function initStops() {
if (!cachedStopsByRegion[REGION_DATA.id]) {
const response = await fetch(REGION_DATA.stopsEndpoint);
const rawStops = (await response.json()) as any[];
// build array and map
stopsMapByRegion[REGION_DATA.id] = {};
cachedStopsByRegion[REGION_DATA.id] = rawStops.map((raw) => {
const id = normalizeId(raw.stopId);
const entry = {
...raw,
stopId: id,
type: raw.type || (id.startsWith("renfe:") ? "train" : "bus"),
favourite: false,
} as Stop;
stopsMapByRegion[REGION_DATA.id][id] = entry;
return entry;
});
// load custom names
const rawCustom = localStorage.getItem(`customStopNames_${REGION_DATA.id}`);
if (rawCustom) {
const parsed = JSON.parse(rawCustom);
const normalized: Record<string, string> = {};
for (const [key, value] of Object.entries(parsed)) {
normalized[normalizeId(key)] = value as string;
}
customNamesByRegion[REGION_DATA.id] = normalized;
} else {
customNamesByRegion[REGION_DATA.id] = {};
}
}
}
async function getStops(): Promise<Stop[]> {
await initStops();
// update favourites
const rawFav = localStorage.getItem("favouriteStops_vigo");
const favouriteStops = rawFav
? (JSON.parse(rawFav) as (number | string)[]).map(normalizeId)
: [];
cachedStopsByRegion["vigo"]!.forEach(
(stop) => (stop.favourite = favouriteStops.includes(stop.stopId))
);
return cachedStopsByRegion["vigo"]!;
}
// New: get single stop by id
async function getStopById(stopId: string | number): Promise<Stop | undefined> {
await initStops();
const id = normalizeId(stopId);
const stop = stopsMapByRegion[REGION_DATA.id]?.[id];
if (stop) {
const rawFav = localStorage.getItem(`favouriteStops_${REGION_DATA.id}`);
const favouriteStops = rawFav
? (JSON.parse(rawFav) as (number | string)[]).map(normalizeId)
: [];
stop.favourite = favouriteStops.includes(id);
}
return stop;
}
// Updated display name to include custom names
function getDisplayName(stop: Stop): string {
const customNames = customNamesByRegion[REGION_DATA.id] || {};
if (customNames[stop.stopId]) return customNames[stop.stopId];
const nameObj = stop.name;
return nameObj.intersect || nameObj.original;
}
// New: set or remove custom names
function setCustomName(stopId: string | number, label: string) {
const id = normalizeId(stopId);
if (!customNamesByRegion[REGION_DATA.id]) {
customNamesByRegion[REGION_DATA.id] = {};
}
customNamesByRegion[REGION_DATA.id][id] = label;
localStorage.setItem(
`customStopNames_${REGION_DATA.id}`,
JSON.stringify(customNamesByRegion[REGION_DATA.id])
);
}
function removeCustomName(stopId: string | number) {
const id = normalizeId(stopId);
if (customNamesByRegion[REGION_DATA.id]?.[id]) {
delete customNamesByRegion[REGION_DATA.id][id];
localStorage.setItem(
`customStopNames_${REGION_DATA.id}`,
JSON.stringify(customNamesByRegion[REGION_DATA.id])
);
}
}
// New: get custom label for a stop
function getCustomName(stopId: string | number): string | undefined {
const id = normalizeId(stopId);
return customNamesByRegion[REGION_DATA.id]?.[id];
}
function addFavourite(stopId: string | number) {
const id = normalizeId(stopId);
const rawFavouriteStops = localStorage.getItem(`favouriteStops_vigo`);
let favouriteStops: string[] = [];
if (rawFavouriteStops) {
favouriteStops = (JSON.parse(rawFavouriteStops) as (number | string)[]).map(
normalizeId
);
}
if (!favouriteStops.includes(id)) {
favouriteStops.push(id);
localStorage.setItem(`favouriteStops_vigo`, JSON.stringify(favouriteStops));
}
}
function removeFavourite(stopId: string | number) {
const id = normalizeId(stopId);
const rawFavouriteStops = localStorage.getItem(`favouriteStops_vigo`);
let favouriteStops: string[] = [];
if (rawFavouriteStops) {
favouriteStops = (JSON.parse(rawFavouriteStops) as (number | string)[]).map(
normalizeId
);
}
const newFavouriteStops = favouriteStops.filter((sid) => sid !== id);
localStorage.setItem(
`favouriteStops_vigo`,
JSON.stringify(newFavouriteStops)
);
}
function isFavourite(stopId: string | number): boolean {
const id = normalizeId(stopId);
const rawFavouriteStops = localStorage.getItem(`favouriteStops_vigo`);
if (rawFavouriteStops) {
const favouriteStops = (
JSON.parse(rawFavouriteStops) as (number | string)[]
).map(normalizeId);
return favouriteStops.includes(id);
}
return false;
}
const RECENT_STOPS_LIMIT = 10;
function pushRecent(stopId: string | number) {
const id = normalizeId(stopId);
const rawRecentStops = localStorage.getItem(`recentStops_vigo`);
let recentStops: Set<string> = new Set();
if (rawRecentStops) {
recentStops = new Set(
(JSON.parse(rawRecentStops) as (number | string)[]).map(normalizeId)
);
}
recentStops.add(id);
if (recentStops.size > RECENT_STOPS_LIMIT) {
const iterator = recentStops.values();
const val = iterator.next().value as string;
recentStops.delete(val);
}
localStorage.setItem(
`recentStops_vigo`,
JSON.stringify(Array.from(recentStops))
);
}
function getRecent(): string[] {
const rawRecentStops = localStorage.getItem(`recentStops_vigo`);
if (rawRecentStops) {
return (JSON.parse(rawRecentStops) as (number | string)[]).map(normalizeId);
}
return [];
}
function getFavouriteIds(): string[] {
const rawFavouriteStops = localStorage.getItem(`favouriteStops_vigo`);
if (rawFavouriteStops) {
return (JSON.parse(rawFavouriteStops) as (number | string)[]).map(
normalizeId
);
}
return [];
}
// New function to load stops from network
async function loadStopsFromNetwork(): Promise<Stop[]> {
const response = await fetch(REGION_DATA.stopsEndpoint);
const rawStops = (await response.json()) as any[];
return rawStops.map((raw) => {
const id = normalizeId(raw.stopId);
return {
...raw,
stopId: id,
type: raw.type || (id.startsWith("renfe:") ? "train" : "bus"),
favourite: false,
} as Stop;
});
}
export default {
getStops,
getStopById,
getCustomName,
getDisplayName,
setCustomName,
removeCustomName,
addFavourite,
removeFavourite,
isFavourite,
pushRecent,
getRecent,
getFavouriteIds,
loadStopsFromNetwork,
};
|