Compare: resolve a location's name immediately on add

Add a lightweight GET /api/v2/place?lat=&lon= endpoint that snaps to the grid
cell and returns the reverse-geocoded "neighborhood, city, region" label (the
same string the view endpoints expose as place, cached + throttled). On the
compare page, adding a location now fires this right away so the chip flips from
coordinates to the place name immediately — instead of only resolving when the
full comparison loads on Refresh. Best-effort: the series load still resolves the
name, so a failed/again call is harmless.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3Q2EkHHnTUX2BfhV5q1Zc
This commit is contained in:
Emi Griffith 2026-07-11 07:50:48 -07:00
parent a1cf72642a
commit 608b245598

View file

@ -177,11 +177,30 @@ function loadPending(token) {
function addLocation(lat, lon) { function addLocation(lat, lon) {
// Ignore a spot already on the list (same grid neighborhood). // Ignore a spot already on the list (same grid neighborhood).
if (locations.some((l) => Math.abs(l.lat - lat) < 0.05 && Math.abs(l.lon - lon) < 0.05)) return; if (locations.some((l) => Math.abs(l.lat - lat) < 0.05 && Math.abs(l.lon - lon) < 0.05)) return;
locations.push({ lat, lon, name: null, series: null, loading: false, error: null }); const loc = { lat, lon, name: null, series: null, loading: false, error: null };
locations.push(loc);
window.Thermograph.saveLastLocation(lat, lon); window.Thermograph.saveLastLocation(lat, lon);
persistLocations(); persistLocations();
writeHashState(); writeHashState();
renderAll(); // pending → the Refresh button appears; no fetch until it's tapped renderAll(); // pending → the Refresh button appears; no fetch until it's tapped
resolveName(loc); // but resolve the neighborhood+city right away (before Refresh)
}
// Resolve just the place name for a freshly-added location, independent of its
// (deferred, heavier) series load, so the chip flips from coordinates to the
// neighborhood + city immediately on add. Best-effort — the series load resolves
// the name too, so a failure here is harmless.
async function resolveName(loc) {
try {
const res = await fetch(`api/v2/place?lat=${loc.lat}&lon=${loc.lon}`);
if (!res.ok) return;
const d = await res.json();
// Skip if the location was removed meanwhile, or already got named by a load.
if (!locations.includes(loc) || loc.name || !d || !d.place) return;
loc.name = d.place;
persistLocations();
renderAll();
} catch (e) { /* best-effort — the series load will still resolve the name */ }
} }
function removeLocation(i) { function removeLocation(i) {