Files
TREK/client/src/repo/chargingRepo.ts
T
Maurice 00d00be1c0 feat(roadtrip): what a charger costs, before it is a stop
A tester found the charging panel once a station was on the trip and asked
for it while still deciding. The availability and the published tariffs were
reachable only for a saved place: the route takes a places id, and a hit
found along the route has no row yet.

ChargingService.load already needed nothing but a coordinate and a name, so
it now has a public method that says so, behind a trip-scoped route with the
same guard stack, a Zod contract in shared, and the parallel MCP tool in this
same change. Same service, same caches, so a station looked at in the dialog
and then added keys the entry already fetched. Trip-scoped even though the
trip is not part of the question: without :tripId the trip access guard goes
too, and what is left is an open proxy in front of somebody else's public API.

It renders in the add dialog, and only while the selected kind is charging,
so a fuel or food stop costs nothing and one opened dialog is one lookup.
Deliberately not per result row: the corridor search publishes box by box,
every mounted panel owns a refresh timer, a cache miss can cost seven
upstream requests, and a station in a dense charging park is the case the
upstream matcher refuses as ambiguous.

Power and socket counts are not in this answer and were not asked of it.
They are OSM tag data and already ride along on the corridor row for free.
2026-09-14 12:28:47 +02:00

45 lines
2.1 KiB
TypeScript

import { chargingInfoSchema, type ChargingInfo } from '@trek/shared'
import { apiClient } from '../api/client'
import { isEffectivelyOffline } from '../sync/networkMode'
const pending = new Map<string, { until: number; value: Promise<ChargingInfo> }>()
/**
* One request per station per minute, however many places ask for it.
*
* The rail renders a badge for every charging stop, the inspector a panel for the same
* one and the add dialog a third for a station that is not on the trip yet, each on
* its own refresh timer. The window is a shade under the server's own minute of cache,
* so a refresh that gets through lands on a fresh answer rather than the cached one.
*
* A rejection stays in the map for the full window on purpose: a station the upstream
* registry cannot answer for should not be asked again a second later.
*/
function coalesce(key: string, load: () => Promise<ChargingInfo>): Promise<ChargingInfo> {
const cached = pending.get(key)
if (cached && cached.until > Date.now()) return cached.value
if (pending.size > 200) pending.clear()
const value = load()
pending.set(key, { until: Date.now() + 55000, value })
return value
}
export const chargingRepo = {
async read(tripId: number, placeId: number) {
if (isEffectivelyOffline()) throw new Error('Offline')
return coalesce(`${tripId}/${placeId}`, () =>
apiClient.get(`/trips/${tripId}/roadtrip/charging/${placeId}`).then(reply => chargingInfoSchema.parse(reply.data)))
},
/**
* The same answer for a station the trip has not saved yet, which is the only way to
* ask before adding it: the saved-stop route takes a place id, and a hit found along
* the route has none. Cached under the coordinate rather than an id, so looking at
* the same charger twice is one request.
*/
async readAt(tripId: number, lat: number, lng: number, name: string) {
if (isEffectivelyOffline()) throw new Error('Offline')
return coalesce(`${tripId}/at:${lat},${lng}:${name}`, () =>
apiClient.post(`/trips/${tripId}/roadtrip/charging-lookup`, { lat, lng, name }).then(reply => chargingInfoSchema.parse(reply.data)))
},
}