Open the Solar calculator from the map, pins and current location

The map's right-click (or long-press) menu gains "Calculate Solar
Performance", and each saved pin a sun button; both open the calculator at
that location.  A button beside Latitude fills in the browser's current
location.  Locations are rounded to 4 decimals, and longitudes from a map
panned around the world are wrapped back to -180..180.

The map removed lat/lon/z from the URL when it closed, even after navigating
to another page, which stripped the calculator's location from its URL (and
from links shared with the Share button).  It now only cleans up while the
URL is still a map page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-04 17:09:33 -06:00
parent 4f06dfe9c8
commit b5ea2054c1
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
9 changed files with 203 additions and 22 deletions

View file

@ -1,5 +1,5 @@
import React, {useCallback, useContext, useEffect, useRef, useState} from "react";
import {useSearchParams} from "react-router";
import {useNavigate, useSearchParams} from "react-router";
import maplibregl from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import {Protocol} from "pmtiles";
@ -11,6 +11,7 @@ import {Button, Checkbox, Header, Icon, Panel, TextInput, Toggle} from "./ui";
import {SettingsContext, ThemeContext} from "../contexts/contexts";
import {mapFlavor, mapSprite} from "../themes/names";
import {poiPopupHtml} from "./mapPoiPopup";
import {solarCalculatorPath} from "./calculators/solar";
// Terrain DEM file prefix for hillshade and contours.
const TERRAIN_PREFIX = "terrain-";
@ -531,6 +532,7 @@ export default function MapViewer() {
const [mapReady, setMapReady] = useState(false);
const [error, setError] = useState(null);
const [contextMenu, setContextMenu] = useState(null);
const navigate = useNavigate();
const [addingPin, setAddingPin] = useState(null); // {lat, lon} when pin dialog is open
const markersRef = useRef([]);
const pinsVisibleRef = useRef(true);
@ -772,12 +774,16 @@ export default function MapViewer() {
}
mapRef.current = null;
setMapReady(false);
// Clean up map params from URL so they don't leak to other pages.
// Clean up map params from URL so they don't leak to other pages. Only while the URL is
// still a map page: after navigating away, lat/lon belong to the new page (the Solar
// calculator, for one).
const url = new URL(window.location);
url.searchParams.delete("lat");
url.searchParams.delete("lon");
url.searchParams.delete("z");
window.history.replaceState(null, '', url.pathname + url.search);
if (url.pathname.startsWith("/map")) {
url.searchParams.delete("lat");
url.searchParams.delete("lon");
url.searchParams.delete("z");
window.history.replaceState(null, '', url.pathname + url.search);
}
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps
@ -923,6 +929,12 @@ export default function MapViewer() {
setContextMenu(null);
}, [contextMenu]);
const handleSolar = useCallback(() => {
if (!contextMenu) return;
setContextMenu(null);
navigate(solarCalculatorPath(contextMenu.lat, contextMenu.lon));
}, [contextMenu, navigate]);
const handleSetDefaultLocation = useCallback(async () => {
const map = mapRef.current;
if (!map) return;
@ -999,6 +1011,14 @@ export default function MapViewer() {
>
Add Pin Here
</div>
<div
style={{padding: "0.5rem 0.75rem", cursor: "pointer", userSelect: "none"}}
onMouseEnter={e => e.target.style.background = "#f0f0f0"}
onMouseLeave={e => e.target.style.background = "transparent"}
onClick={handleSolar}
>
Calculate Solar Performance
</div>
<div
style={{padding: "0.5rem 0.75rem", cursor: "pointer", userSelect: "none"}}
onMouseEnter={e => e.target.style.background = "#f0f0f0"}