2.5.1
Slow down API calls to every 15mins on non-pro
This commit is contained in:
@@ -12,13 +12,15 @@
|
||||
// c. Stale soil cache — used if ICON fetch fails.
|
||||
// 3. Fetch /v1/air-quality on location change, with a 6-hour
|
||||
// localStorage cache (AQI / pollen update slowly).
|
||||
// 4. Auto-refresh forecast every 15 minutes (matches Open-Meteo
|
||||
// update cadence). Air quality only refetches if cache is stale.
|
||||
// 4. Auto-refresh forecast every 5 minutes for Pro, 15 minutes for
|
||||
// free (Open-Meteo updates ~every 15 min). Air quality only
|
||||
// refetches if cache is stale.
|
||||
// 5. Tick `now` every 2 minutes to keep the current-row highlight
|
||||
// accurate without any API cost.
|
||||
//
|
||||
// Inputs:
|
||||
// location - { lat, lon, name, country }
|
||||
// isPro - boolean; true tightens auto-refresh to 5 min
|
||||
//
|
||||
// Outputs:
|
||||
// forecast - raw /v1/forecast response, or null
|
||||
@@ -119,7 +121,7 @@ function mergeSoil(forecastData, soilHourly) {
|
||||
return { ...forecastData, hourly: h };
|
||||
}
|
||||
|
||||
export function useForecast(location) {
|
||||
export function useForecast(location, isPro = false) {
|
||||
const [forecast, setForecast] = useState(null);
|
||||
const [airQuality, setAirQuality] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -235,8 +237,11 @@ export function useForecast(location) {
|
||||
return () => clearInterval(id);
|
||||
}, []);
|
||||
|
||||
// ─── 15-MINUTE REFRESH (matches Open-Meteo update cadence) ────────
|
||||
// ─── AUTO REFRESH (Pro: 5 min, free: 15 min) ─────────────────────
|
||||
// Open-Meteo updates roughly every 15 min; Pro users get a tighter
|
||||
// poll so the current-conditions row stays fresher.
|
||||
useEffect(() => {
|
||||
const refreshMs = isPro ? FIVE_MIN_MS : FIFTEEN_MIN_MS;
|
||||
const id = setInterval(() => {
|
||||
async function refresh() {
|
||||
const key = forecastCacheKey(location);
|
||||
@@ -254,9 +259,9 @@ export function useForecast(location) {
|
||||
loadAirQuality(location);
|
||||
}
|
||||
refresh();
|
||||
}, FIFTEEN_MIN_MS);
|
||||
}, refreshMs);
|
||||
return () => clearInterval(id);
|
||||
}, [location]);
|
||||
}, [location, isPro]);
|
||||
|
||||
return { forecast, airQuality, loading, error, now, fetchedAt };
|
||||
}
|
||||
Reference in New Issue
Block a user