aboutsummaryrefslogtreecommitdiff
path: root/src/data/StopDataProvider.ts
blob: 55d0e78cc1b916153732bde1400c7b8ad3d13e53 (plain)
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
export interface CachedStopList {
	timestamp: number;
	data: Stop[];
}

export type StopName = {
	original: string;
	intersect?: string;
}

export interface Stop {
	stopId: number;
	name: StopName;
	latitude?: number;
	longitude?: number;
	lines: string[];
	favourite?: boolean;
}

export default {
	getStops,
	getDisplayName,
	addFavourite,
	removeFavourite,
	isFavourite,
	pushRecent,
	getRecent
};

async function getStops(): Promise<Stop[]> {
	const rawFavouriteStops = localStorage.getItem('favouriteStops');
	let favouriteStops: number[] = [];
	if (rawFavouriteStops) {
		favouriteStops = JSON.parse(rawFavouriteStops) as number[];
	}

	const response = await fetch('/stops.json');
	const stops = await response.json() as Stop[];

	return stops.map((stop: Stop) => {
		return {
			...stop,
			favourite: favouriteStops.includes(stop.stopId)
		};
	});
}

// Get display name based on preferences or context
function getDisplayName(stop: Stop): string {
	if (typeof stop.name === 'string') {
		return stop.name;
	}

	return stop.name.intersect || stop.name.original;
}

function addFavourite(stopId: number) {
	const rawFavouriteStops = localStorage.getItem('favouriteStops');
	let favouriteStops: number[] = [];
	if (rawFavouriteStops) {
		favouriteStops = JSON.parse(rawFavouriteStops) as number[];
	}

	if (!favouriteStops.includes(stopId)) {
		favouriteStops.push(stopId);
		localStorage.setItem('favouriteStops', JSON.stringify(favouriteStops));
	}
}

function removeFavourite(stopId: number) {
	const rawFavouriteStops = localStorage.getItem('favouriteStops');
	let favouriteStops: number[] = [];
	if (rawFavouriteStops) {
		favouriteStops = JSON.parse(rawFavouriteStops) as number[];
	}

	const newFavouriteStops = favouriteStops.filter(id => id !== stopId);
	localStorage.setItem('favouriteStops', JSON.stringify(newFavouriteStops));
}

function isFavourite(stopId: number): boolean {
	const rawFavouriteStops = localStorage.getItem('favouriteStops');
	if (rawFavouriteStops) {
		const favouriteStops = JSON.parse(rawFavouriteStops) as number[];
		return favouriteStops.includes(stopId);
	}
	return false;
}

const RECENT_STOPS_LIMIT = 10;

function pushRecent(stopId: number) {
	const rawRecentStops = localStorage.getItem('recentStops');
	let recentStops: Set<number> = new Set();
	if (rawRecentStops) {
		recentStops = new Set(JSON.parse(rawRecentStops) as number[]);
	}

	recentStops.add(stopId);
	if (recentStops.size > RECENT_STOPS_LIMIT) {
		const iterator = recentStops.values();
		const val = iterator.next().value as number;
		recentStops.delete(val);
	}

	localStorage.setItem('recentStops', JSON.stringify(Array.from(recentStops)));
}

function getRecent(): number[] {
	const rawRecentStops = localStorage.getItem('recentStops');
	if (rawRecentStops) {
		return JSON.parse(rawRecentStops) as number[];
	}
	return [];
}