mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix(atlas): retile the map instead of rebuilding it when the CARTO key lands
useTileUrl returns a new string once carto_api_key reaches the settings
store, which happens after the first render. That string was a dependency
of the effect that *builds* the Leaflet map, so the key arriving tore the
map down and built a new one.
Two things break in that window: Leaflet finishes a queued redraw against
the canvas renderer whose map has gone ("Cannot read properties of
undefined (reading 'save')"), and the country layer never comes back —
its effect only re-runs when its own data changes, so the rebuilt map
shows tiles and nothing else.
The template belongs to the tile layer, not to the map. Hold the layers
in a ref, read the template through a ref inside the build effect, and
setUrl them from an effect of their own. Same fix in JourneyMap, where
cartoKey and mapTileUrl sat in the dep arrays of the effects that draw
every marker and track.
Fixes #2097
This commit is contained in:
committed by
Maurice
parent
5a7b8d75aa
commit
1c5c3ae983
@@ -37,7 +37,7 @@ vi.mock('leaflet', () => {
|
||||
return {
|
||||
default: {
|
||||
map: vi.fn(() => mockMap),
|
||||
tileLayer: vi.fn(() => ({ addTo: vi.fn() })),
|
||||
tileLayer: vi.fn(() => ({ addTo: vi.fn(), setUrl: vi.fn() })),
|
||||
marker: vi.fn(() => mockMarker),
|
||||
polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line) }; return line }),
|
||||
divIcon: vi.fn(() => ({})),
|
||||
@@ -45,7 +45,7 @@ vi.mock('leaflet', () => {
|
||||
layerGroup: vi.fn(() => ({ addLayer: vi.fn(), addTo: vi.fn(), remove: vi.fn() })),
|
||||
},
|
||||
map: vi.fn(() => mockMap),
|
||||
tileLayer: vi.fn(() => ({ addTo: vi.fn() })),
|
||||
tileLayer: vi.fn(() => ({ addTo: vi.fn(), setUrl: vi.fn() })),
|
||||
marker: vi.fn(() => mockMarker),
|
||||
polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line) }; return line }),
|
||||
divIcon: vi.fn(() => ({})),
|
||||
@@ -388,6 +388,22 @@ describe('JourneyMap', () => {
|
||||
expect(vi.mocked(L.tileLayer).mock.calls[0][0]).toBe('https://tiles.test/{z}/{x}/{y}.png');
|
||||
});
|
||||
|
||||
it('FE-COMP-JOURNEYMAP-041: a CARTO key arriving after mount retiles instead of rebuilding the map (#2097)', () => {
|
||||
render(<JourneyMap checkins={[]} entries={entriesWithCoords} />);
|
||||
const tiles = vi.mocked(L.tileLayer).mock.results[0].value as { setUrl: ReturnType<typeof vi.fn> };
|
||||
const mapsBefore = vi.mocked(L.map).mock.calls.length;
|
||||
|
||||
act(() => {
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ carto_api_key: 'k1' }) });
|
||||
});
|
||||
|
||||
expect(tiles.setUrl).toHaveBeenCalledWith(expect.stringContaining('key=k1'));
|
||||
// The markers and tracks the map already carries are drawn by the effect that
|
||||
// builds it, so a rebuild here would tear them down mid-flight.
|
||||
expect(vi.mocked(L.map).mock.calls.length).toBe(mapsBefore);
|
||||
expect(mockedMap().remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-JOURNEYMAP-028: the activeMarkerId prop flies to that marker after the settle delay', () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
|
||||
@@ -154,6 +154,13 @@ function JourneyMap(
|
||||
const mapTileUrl = useSettingsStore(s => s.settings.map_tile_url)
|
||||
const storedCartoKey = useCartoApiKey()
|
||||
const cartoKey = cartoApiKey || storedCartoKey
|
||||
const tileUrl = resolveTileUrl(mapTileUrl, dark ? CARTO_DARK : CARTO_VOYAGER, cartoKey)
|
||||
// Read through a ref by the map effect, retiled in place by its own effect below:
|
||||
// the CARTO key reaches the store after the first render, and rebuilding the map
|
||||
// for that raced with the markers and layers already on it (#2097).
|
||||
const tileUrlRef = useRef(tileUrl)
|
||||
tileUrlRef.current = tileUrl
|
||||
const tileLayerRef = useRef<L.TileLayer | null>(null)
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const mapRef = useRef<L.Map | null>(null)
|
||||
const markersRef = useRef<Map<string, L.Marker>>(new Map())
|
||||
@@ -232,7 +239,7 @@ function JourneyMap(
|
||||
})
|
||||
mapRef.current = map
|
||||
|
||||
L.tileLayer(resolveTileUrl(mapTileUrl, dark ? CARTO_DARK : CARTO_VOYAGER, cartoKey), {
|
||||
const tiles = L.tileLayer(tileUrlRef.current, {
|
||||
maxZoom: 18,
|
||||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
||||
referrerPolicy: 'strict-origin-when-cross-origin',
|
||||
@@ -242,7 +249,9 @@ function JourneyMap(
|
||||
// updates and keep a larger ring of off-screen tiles ready.
|
||||
updateWhenIdle: false,
|
||||
keepBuffer: 4,
|
||||
} as any).addTo(map)
|
||||
} as any)
|
||||
tiles.addTo(map)
|
||||
tileLayerRef.current = tiles
|
||||
|
||||
const items = buildMarkerItems(entries)
|
||||
itemsRef.current = items
|
||||
@@ -337,9 +346,16 @@ function JourneyMap(
|
||||
return () => {
|
||||
map.remove()
|
||||
mapRef.current = null
|
||||
tileLayerRef.current = null
|
||||
markersRef.current.clear()
|
||||
}
|
||||
}, [entries, stableTrail, stableTracks, dark, mapTileUrl, cartoKey, fullScreen, paddingBottom])
|
||||
}, [entries, stableTrail, stableTracks, dark, fullScreen, paddingBottom])
|
||||
|
||||
// Retile in place rather than through the effect above, which would drop every
|
||||
// marker and track it just drew.
|
||||
useEffect(() => {
|
||||
tileLayerRef.current?.setUrl(tileUrl)
|
||||
}, [tileUrl])
|
||||
|
||||
// Photo layer (#1614). Its own effect on purpose: photos arriving must not tear
|
||||
// down and rebuild the map the way the entry effect does. Redrawn on zoom and
|
||||
@@ -382,7 +398,7 @@ function JourneyMap(
|
||||
photoLayerRef.current?.remove()
|
||||
photoLayerRef.current = null
|
||||
}
|
||||
}, [photos, entries, stableTrail, stableTracks, dark, mapTileUrl, cartoKey, fullScreen, paddingBottom])
|
||||
}, [photos, entries, stableTrail, stableTracks, dark, fullScreen, paddingBottom])
|
||||
|
||||
// react to activeMarkerId prop changes — runs after map is built
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user