Map previews that never drew, and a Docker map with no world under it (#658)
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

* ui: a preview map that waited for a container it could never see

Both map preview modals -- the per-region eye button and "View All Regions"
-- opened to an empty box.  Each renders a div into a Modal and hands it to
MapLibre from an effect gated on `open`, reading the node out of a `useRef`:

    if (!open || !mapContainer.current) return;

Mantine's Modal mounts its body one commit after `opened` flips true, so on
the render that opens the modal the ref is still null.  The effect returns,
nothing it depends on changes again, and it never runs a second time.  No
console error, no rejected promise, no canvas -- just an empty div.

Semantic UI's Portal mounted the body in the same commit that opened it, so
the ref was attached by the time effects ran.  The effect bodies are
byte-identical across that migration; the modal library is the only variable,
confirmed against a device still running the older build where both previews
render.

A callback ref instead, so the effect re-runs when the node arrives and this
stops depending on any modal's mounting schedule.  The async source lookup
also gets a cancelled flag: it could resolve after the cleanup ran, building
a map nothing would ever remove and leaking a WebGL context per open.

The tests drive the real page and assert a map was built against a node that
is in the document -- the outcome, rather than the mechanism that quietly
changed underneath it.  Seven of the eight fail on the code before this.

* build: docker had no world underneath its map regions

Caddy serves /blobs/map-overview.pmtiles out of /opt/wrolpi-blobs, and that
blob is the world at zoom 0-6 -- what a regional extract sits on top of.
Without it a user who subscribes to a few regions gets those regions floating
on an empty background everywhere their data does not reach.  Only a full
planet file, at 134GB, makes it unnecessary.

Raspberry Pi and Debian installs get the blob from pi-gen, the live-build
config, and upgrade.sh.  Docker got nothing: the compose file bind-mounted a
git-ignored host directory over /opt/wrolpi-blobs, and a bind mount replaces
the image's directory rather than merging with it, so the path existed and was
empty on every Docker WROLPi.  Verified against a running deployment, where
that URL returned 404 while the same URL on hardware returned 206.

So the web image fetches the blob at build time, as the other three builders
do, and the mount that would hide it is gone.  `curl -f` so an HTTP error
fails the layer -- an image that builds without the blob is this same bug,
except silent at build time too.

The tests read the deployment files rather than a built container: building
to check costs a CDN download and several minutes, and both things that broke
-- nothing fetches it, a mount hides it -- are visible in the source.

* ui: cancel the load handler too, and make the cancel test falsifiable

Review follow-ups on the preview modals.

The `cancelled` flag was checked before constructing the map but not inside
the `load` handler it installs, so cleanup landing between the two left that
body drawing into a map `remove()` had already torn down.  Guarded, mirroring
the `destroyed` pattern in MapViewer.js.  The source promise also gets a
`.catch`: the helper cannot reject today, but nothing says it never will, and
an unhandled rejection here would be as quiet as the bug this all started as.

Two test corrections, both cases of an assertion claiming more than it read:

The region-drawing test spied on `addSource` AFTER construction and then
re-fired `load` by hand, so its count came entirely from that manual call and
said nothing about the production path.  FakeMap now records the sources it is
given, and the assertion reads what the component's own `load` drew -- each
region once, in order, and none for the entry with no bbox.

The new cancel test passed with the guard deleted, which is worse than not
having it: Mantine had not mounted the modal body when it closed, so the
effect had never run and there was nothing to cancel.  It now waits for the
effect to reach the source lookup before closing, and ships beside a control
that releases the same held promise WITHOUT closing and requires a map.  The
control is what makes the zero meaningful; verified that removing the guard
fails the cancel test while the control still passes.
This commit is contained in:
lrnselfreliance 2026-08-11 20:37:14 -06:00 • committed by Roland
parent 6d0f920c2a
commit ebdf9dcc4b
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 492 additions and 18 deletions

View file

@ -51,21 +51,34 @@ async function getPreviewMapSource() {
return {type: 'vector', url: `pmtiles://${MAP_OVERVIEW_URL}`};
}
/*
* The container is a piece of state set by a callback ref, not a `useRef` read in the effect.
*
* A modal body is not in the DOM on the render that opens the modal -- Mantine mounts it a
* commit later -- so an effect gated on `open` that reads `ref.current` finds null, returns,
* and never runs again, because nothing it depends on changes after that. Both previews were
* silently blank for exactly this reason. A callback ref re-runs the effect when the node
* arrives, whenever that is, so this does not depend on any modal's mounting schedule.
*/
function RegionPreviewModal({bbox, name, open, onClose}) {
const mapContainer = React.useRef(null);
const [mapContainer, setMapContainer] = useState(null);
React.useEffect(() => {
if (!open || !mapContainer.current || !bbox) return;
if (!open || !mapContainer || !bbox) return;
const [minLon, minLat, maxLon, maxLat] = bbox.split(',').map(Number);
const centerLon = (minLon + maxLon) / 2;
const centerLat = (minLat + maxLat) / 2;
let map;
// The source lookup is async, so the modal can close before it resolves. Without this
// the cleanup below runs while `map` is still undefined and the map built afterwards
// is never removed -- a leaked WebGL context on every quick open-and-close.
let cancelled = false;
getPreviewMapSource().then(source => {
if (!mapContainer.current) return;
if (cancelled) return;
map = new maplibregl.Map({
container: mapContainer.current,
container: mapContainer,
style: {
version: 8,
sources: {'basemap': source},
@ -79,6 +92,9 @@ function RegionPreviewModal({bbox, name, open, onClose}) {
});
map.on('load', () => {
// The flag again: cleanup can land between construction and `load`, and this
// body draws into a map `remove()` has already torn down.
if (cancelled) return;
map.addSource('bbox', {
type: 'geojson',
data: {
@ -109,16 +125,19 @@ function RegionPreviewModal({bbox, name, open, onClose}) {
});
map.fitBounds([[minLon, minLat], [maxLon, maxLat]], {padding: 40});
});
});
}).catch(e => console.error('Could not build the region preview map:', e));
return () => { if (map) map.remove(); };
}, [open, bbox]);
return () => {
cancelled = true;
if (map) map.remove();
};
}, [open, bbox, mapContainer]);
return <Modal open={open} onClose={onClose} size='fullscreen' closeIcon title={name}>
{/* No `.media` here: the bbox highlight is drawn as a map layer on the canvas itself
(see the `bbox` source/layers above), so the automatic `canvas` element-type filter
already covers it. There is no separate DOM overlay to wrap. */}
<div ref={mapContainer} style={{width: '100%', height: '50vh'}}/>
<div ref={setMapContainer} style={{width: '100%', height: '50vh'}}/>
</Modal>;
}
@ -129,17 +148,19 @@ const REGION_COLORS = [
'#a333c8', '#21ba45', '#db2828', '#fbbd08', '#a5673f', '#767676',
];
// See RegionPreviewModal for why the container is state set by a callback ref.
function AllRegionsPreviewModal({catalog, open, onClose}) {
const mapContainer = React.useRef(null);
const [mapContainer, setMapContainer] = useState(null);
React.useEffect(() => {
if (!open || !mapContainer.current) return;
if (!open || !mapContainer) return;
let map;
let cancelled = false;
getPreviewMapSource().then(source => {
if (!mapContainer.current) return;
if (cancelled) return;
map = new maplibregl.Map({
container: mapContainer.current,
container: mapContainer,
style: {
version: 8,
sources: {'basemap': source},
@ -153,6 +174,8 @@ function AllRegionsPreviewModal({catalog, open, onClose}) {
});
map.on('load', () => {
// See RegionPreviewModal: cleanup can land between construction and `load`.
if (cancelled) return;
const regions = catalog.filter(r => r.bbox);
regions.forEach((region, i) => {
const [minLon, minLat, maxLon, maxLat] = region.bbox.split(',').map(Number);
@ -197,14 +220,17 @@ function AllRegionsPreviewModal({catalog, open, onClose}) {
});
});
});
});
}).catch(e => console.error('Could not build the all-regions preview map:', e));
return () => { if (map) map.remove(); };
}, [open, catalog]);
return () => {
cancelled = true;
if (map) map.remove();
};
}, [open, catalog, mapContainer]);
return <Modal open={open} onClose={onClose} size='fullscreen' closeIcon title='All Map Regions'>
{/* No `.media` here either — same reasoning as RegionPreviewModal. */}
<div ref={mapContainer} style={{width: '100%', height: '75vh'}}/>
<div ref={setMapContainer} style={{width: '100%', height: '75vh'}}/>
</Modal>;
}