Files
TREK/client/src/components/Map/glLazy.tsx
T
Maurice f51a64017d refactor(client): type both trip map renderers against one MapViewProps contract
MapViewAuto took props: any and the Leaflet MapView destructured any, so a prop
one renderer learned and the other never heard of went unnoticed.
components/Map/mapViewContract.ts holds MapViewProps, derived from the GL
renderer's props and what the callers pass; MapView and the GL bindings in
glLazy declare satisfies ComponentType against it, and the GL renderer extends
it with its engine. The file imports types only, so check:gl-split is not
affected. Types only, no runtime change.

The types show two known gaps, recorded in the contract rather than papered
over: onMapReady is called by the GL renderer only, and callers pass pins
without trip_id (assignment and collection places) while both renderers type
their pins as Place; MapViewAuto bridges the latter with one commented cast.
2026-10-10 21:31:34 +02:00

83 lines
3.6 KiB
TypeScript

import type { ComponentType } from 'react'
import { lazyWithRetry } from '../../utils/lazyWithRetry'
import type { GlMapProvider } from './glProviders'
import type { MapViewProps } from './mapViewContract'
/** What a caller hands the GL trip map: the shared map contract and which provider's style to load. */
export type MapViewGLProps = MapViewProps & { glProvider?: GlMapProvider }
/**
* The six ways into a GL map, one chunk per engine.
*
* mapbox-gl and maplibre-gl used to be static imports inside MapViewGL,
* JourneyMapGL and the settings preview, so rollup put both SDKs into a single
* 2.8 MB chunk. Every map user downloaded 1.8 MB of mapbox plus 1.1 MB of
* maplibre and then ran exactly one of them.
*
* Pairing the component with its engine behind the lazy boundary is what
* separates them: no module reaches both SDKs statically any more, so rolldown
* emits one shared core chunk and two engine chunks. The engine arrives as a
* prop — see the `gl` prop on each component.
*
* Both imports start together rather than in sequence: the engine is by far the
* larger of the two, and waiting for the component first would add its round
* trip in front of the download that actually costs time.
*/
export const MapViewGLMapbox = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([import('./MapViewGL'), import('./engines/mapbox')])
const MapViewGL = component.MapViewGL
const bound = (props: MapViewGLProps) => <MapViewGL {...props} gl={engine.default} />
return { default: bound satisfies ComponentType<MapViewGLProps> }
})
export const MapViewGLMaplibre = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([import('./MapViewGL'), import('./engines/maplibre')])
const MapViewGL = component.MapViewGL
const bound = (props: MapViewGLProps) => <MapViewGL {...props} gl={engine.default} />
return { default: bound satisfies ComponentType<MapViewGLProps> }
})
export const JourneyMapGLMapbox = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([
import('../Journey/JourneyMapGL'),
import('./engines/mapbox'),
])
const JourneyMapGL = component.default
// The journey map exposes an imperative handle (highlightMarker / focusMarker /
// invalidateSize). Since React 19 its ref is an ordinary prop, so it rides
// through the spread and the binding needs no forwardRef wrapper.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { default: (props: any) => <JourneyMapGL {...props} gl={engine.default} /> }
})
export const JourneyMapGLMaplibre = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([
import('../Journey/JourneyMapGL'),
import('./engines/maplibre'),
])
const JourneyMapGL = component.default
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { default: (props: any) => <JourneyMapGL {...props} gl={engine.default} /> }
})
export const GlMapPreviewMapbox = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([
import('../Settings/MapboxPreview'),
import('./engines/mapbox'),
])
const GlMapPreview = component.default
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { default: (props: any) => <GlMapPreview {...props} gl={engine.default} /> }
})
export const GlMapPreviewMaplibre = lazyWithRetry(async () => {
const [component, engine] = await Promise.all([
import('../Settings/MapboxPreview'),
import('./engines/maplibre'),
])
const GlMapPreview = component.default
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { default: (props: any) => <GlMapPreview {...props} gl={engine.default} /> }
})