Files
PolyWeather/frontend/components/dashboard/scan-terminal/use-ai-city-forecast.ts
T
2569718930@qq.com b122e7cbae Stabilize the decision workspace data boundaries
The scan terminal had grown into overlapping CSS, request-state, AI-provider, and city-card data responsibilities. This refactor separates those boundaries without changing product behavior: CSS modules are split by surface, city AI prompt/provider/fallback logic is isolated, and scan terminal request state now has reusable RemoteData adapters plus business-state tests.

Constraint: Preserve existing global scan-terminal class names and API responses during the refactor

Constraint: No new dependencies; keep this as a file-boundary cleanup

Rejected: Introduce React Query now | higher migration risk than the requested lightweight query-client path

Rejected: Rewrite AI stream behavior | progressive/fallback states are product-sensitive and were only adapter-split

Confidence: high

Scope-risk: moderate

Reversibility: clean

Directive: Keep AI stream state changes covered by business snapshots before changing fallback/cache wording

Tested: npm run test:business; npx tsc --noEmit; npm run build; python pytest -q; ruff check; py_compile targeted city AI modules

Not-tested: Live DeepSeek provider network replay and browser visual QA
2026-04-28 14:45:34 +08:00

129 lines
3.1 KiB
TypeScript

"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import { scanTerminalClient } from "@/components/dashboard/scan-terminal/scan-terminal-client";
import type { AiCityForecastState } from "@/components/dashboard/scan-terminal/types";
import type { CityDetail } from "@/lib/dashboard-types";
import {
buildAiCityErrorForecastState,
buildAiCityForecastCacheKey,
buildAiCityForecastKey,
buildAiCityForecastRequestKey,
buildAiCityLoadingForecastState,
buildAiCityProgressForecastState,
buildAiCityReadyForecastState,
readReadyCachedAiForecastState,
} from "./ai-city-forecast-stream-state";
export function useAiCityForecast({
detail,
detailCityName,
isEn,
locale,
report,
enabled = true,
}: {
detail: CityDetail | null;
detailCityName: string;
enabled?: boolean;
isEn: boolean;
locale: string;
report: string;
}) {
const [aiForecast, setAiForecast] = useState<AiCityForecastState>({
status: "idle",
});
const [aiRefreshToken, setAiRefreshToken] = useState(0);
const aiForecastKey = useMemo(
() => buildAiCityForecastKey({ detail, detailCityName, locale, report }),
[detail, detailCityName, locale, report],
);
useEffect(() => {
if (!enabled || !aiForecastKey) {
setAiForecast({ status: "idle" });
return;
}
let cancelled = false;
const cacheKey = buildAiCityForecastCacheKey(aiForecastKey);
const requestKey = buildAiCityForecastRequestKey(cacheKey, aiRefreshToken);
const readyCachedState = readReadyCachedAiForecastState(
cacheKey,
aiRefreshToken,
);
if (readyCachedState) {
setAiForecast(readyCachedState);
return () => {
cancelled = true;
};
}
const loadingState = buildAiCityLoadingForecastState({
cacheKey,
detail,
isEn,
report,
});
setAiForecast(loadingState);
void scanTerminalClient.streamAiCityRead({
city: detailCityName,
forceRefresh: aiRefreshToken > 0,
locale,
onProgress: (progress) => {
if (cancelled) return;
setAiForecast((current) =>
buildAiCityProgressForecastState({
cacheKey,
current,
isEn,
progress,
}) ?? current,
);
},
requestKey,
})
.then((payload) => {
if (!payload) return;
const readyState = buildAiCityReadyForecastState({
cacheKey,
detail,
isEn,
payload,
report,
});
if (!cancelled) {
setAiForecast(readyState);
}
})
.catch((error) => {
const errorState = buildAiCityErrorForecastState({
cacheKey,
detail,
error,
isEn,
report,
});
if (!cancelled) {
setAiForecast(errorState);
}
});
return () => {
cancelled = true;
};
}, [
aiForecastKey,
aiRefreshToken,
detail,
detailCityName,
enabled,
isEn,
locale,
report,
]);
const refreshAiForecast = useCallback(() => {
setAiRefreshToken((current) => current + 1);
}, []);
return { aiForecast, refreshAiForecast };
}