diff --git a/frontend/components/dashboard/scan-terminal/use-ai-city-forecast.ts b/frontend/components/dashboard/scan-terminal/use-ai-city-forecast.ts index c1574a11..e6970734 100644 --- a/frontend/components/dashboard/scan-terminal/use-ai-city-forecast.ts +++ b/frontend/components/dashboard/scan-terminal/use-ai-city-forecast.ts @@ -30,15 +30,20 @@ export function useAiCityForecast({ locale: string; report: string; }) { - const [aiForecast, setAiForecast] = useState({ - status: "idle", - }); const [aiRefreshToken, setAiRefreshToken] = useState(0); const aiForecastKey = useMemo( () => buildAiCityForecastKey({ detail, detailCityName, locale, report }), [detail, detailCityName, locale, report], ); + const [aiForecast, setAiForecast] = useState(() => { + if (!enabled || !aiForecastKey) return { status: "idle" }; + const cacheKey = buildAiCityForecastCacheKey(aiForecastKey); + const cached = readReadyCachedAiForecastState(cacheKey, 0); + if (cached) return cached; + return { status: "idle" }; + }); + useEffect(() => { if (!enabled || !aiForecastKey) { setAiForecast({ status: "idle" }); @@ -47,16 +52,16 @@ export function useAiCityForecast({ let cancelled = false; const cacheKey = buildAiCityForecastCacheKey(aiForecastKey); const requestKey = buildAiCityForecastRequestKey(cacheKey, aiRefreshToken); - const readyCachedState = readReadyCachedAiForecastState( - cacheKey, - aiRefreshToken, - ); - if (readyCachedState) { - setAiForecast(readyCachedState); - return () => { - cancelled = true; - }; + + // If cache was loaded into initial state and no force refresh, skip + if (aiRefreshToken === 0) { + const cached = readReadyCachedAiForecastState(cacheKey, 0); + if (cached) { + setAiForecast(cached); + return () => { cancelled = true; }; + } } + const loadingState = buildAiCityLoadingForecastState({ cacheKey, detail,