Recent locations save
This commit is contained in:
fraxle
2026-07-04 21:53:14 +01:00
parent bf64122bc2
commit 318e532d10
7 changed files with 140 additions and 2 deletions
+21 -1
View File
@@ -57,9 +57,29 @@ export function useAppState() {
return { name: 'London, England', lat: 51.509, lon: -0.126, country: 'GB' };
});
// Recent locations: the most recently viewed spots, so the user can
// hop back with one click. The list is kept newest-first with the
// current location at the front; the UI shows the others as chips.
const [recentLocations, setRecentLocations] = useState(() => {
try {
const saved = localStorage.getItem('sunscope_recent_locations');
if (saved) return JSON.parse(saved);
} catch (e) { /* ignore */ }
return [];
});
const locKey = (l) => `${l.lat.toFixed(3)},${l.lon.toFixed(3)}`;
const setLocationAndSave = (loc) => {
try { localStorage.setItem('sunscope_last_location', JSON.stringify(loc)); } catch (e) { /* ignore */ }
setLocation(loc);
setRecentLocations((prev) => {
// Keep the current spot at the front and the 3 previous distinct
// ones behind it (4 total, so 3 chips remain after excluding current).
const next = [loc, ...prev.filter((l) => locKey(l) !== locKey(loc))].slice(0, 4);
try { localStorage.setItem('sunscope_recent_locations', JSON.stringify(next)); } catch (e) { /* ignore */ }
return next;
});
};
// Pro tier flag is read early so useForecast can pick its refresh cadence
@@ -589,7 +609,7 @@ export function useAppState() {
// ── RETURN ALL STATE + HANDLERS ───────────────────────────────────────
return {
// location
location, setLocationAndSave,
location, setLocationAndSave, recentLocations,
// forecast
forecast, airQuality, loading, error, now, fetchedAt,
// search