diff options
| -rw-r--r-- | src/data/StopDataProvider.ts | 26 | ||||
| -rw-r--r-- | src/pages/Home.tsx | 20 | ||||
| -rw-r--r-- | src/pages/Stop.tsx | 2 |
3 files changed, 48 insertions, 0 deletions
diff --git a/src/data/StopDataProvider.ts b/src/data/StopDataProvider.ts index 620a659..6245e7a 100644 --- a/src/data/StopDataProvider.ts +++ b/src/data/StopDataProvider.ts @@ -62,4 +62,30 @@ export class StopDataProvider { } return false; } + + RECENT_STOPS_LIMIT = 10; + + 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 > this.RECENT_STOPS_LIMIT) { + const iterator = recentStops.values(); + recentStops.delete(iterator.next().value); + } + + localStorage.setItem('recentStops', JSON.stringify(Array.from(recentStops))); + } + + getRecent(): number[] { + const rawRecentStops = localStorage.getItem('recentStops'); + if (rawRecentStops) { + return JSON.parse(rawRecentStops) as number[]; + } + return []; + } }
\ No newline at end of file diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 1f5c319..568b2f0 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -28,6 +28,14 @@ export function Home() { return data?.filter(stop => stop.favourite) ?? [] }, [data]) + const recentStops = useMemo(() => { + const recent = sdp.getRecent(); + + if (recent.length === 0) return null; + + return recent.map(stopId => data?.find(stop => stop.stopId === stopId) as Stop).reverse(); + }, [data]) + if (data === null) return <h1>Loading...</h1> return ( @@ -63,6 +71,18 @@ export function Home() { ))} </ul> + <h2>Recientes</h2> + + <ul> + {recentStops?.map((stop: Stop) => ( + <li key={stop.stopId}> + <Link to={`/${stop.stopId}`}> + ({stop.stopId}) {stop.name} - {stop.lines?.join(', ')} + </Link> + </li> + ))} + </ul> + <h2>Paradas</h2> <ul> diff --git a/src/pages/Stop.tsx b/src/pages/Stop.tsx index 4fa68cf..feb11b4 100644 --- a/src/pages/Stop.tsx +++ b/src/pages/Stop.tsx @@ -32,6 +32,8 @@ export function Stop(): JSX.Element { useEffect(() => { loadData(); + sdp.pushRecent(parseInt(params.stopId ?? "")); + setFavourited( sdp.isFavourite(parseInt(params.stopId ?? "")) ); |
