feat(history): align units and history data
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import config from "@/config/config";
|
||||
import { apiFetch } from "@/lib/apiFetch";
|
||||
import {
|
||||
DEFAULT_NETWORK_RESULT_UNITS,
|
||||
type NetworkResultUnits,
|
||||
networkResultUnitsFromOptions,
|
||||
} from "@/utils/units";
|
||||
|
||||
export const fetchNetworkResultUnits = async (
|
||||
networkName: string,
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
const query = new URLSearchParams({ network: networkName });
|
||||
const response = await apiFetch(
|
||||
`${config.BACKEND_URL}/api/v1/network-options?${query}`,
|
||||
{ signal },
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`模型单位请求失败: HTTP ${response.status}`);
|
||||
}
|
||||
return networkResultUnitsFromOptions(
|
||||
(await response.json()) as Record<string, string>,
|
||||
);
|
||||
};
|
||||
|
||||
export const useNetworkResultUnits = (networkName?: string | null) => {
|
||||
const [loaded, setLoaded] = useState<{
|
||||
networkName: string;
|
||||
units: NetworkResultUnits;
|
||||
}>();
|
||||
|
||||
useEffect(() => {
|
||||
if (!networkName) return;
|
||||
const controller = new AbortController();
|
||||
void fetchNetworkResultUnits(networkName, controller.signal)
|
||||
.then((units) => setLoaded({ networkName, units }))
|
||||
.catch((error) => {
|
||||
if (controller.signal.aborted) return;
|
||||
console.error("[units] 获取项目模型单位失败:", error);
|
||||
setLoaded({ networkName, units: DEFAULT_NETWORK_RESULT_UNITS });
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [networkName]);
|
||||
|
||||
return loaded && loaded.networkName === networkName
|
||||
? loaded.units
|
||||
: DEFAULT_NETWORK_RESULT_UNITS;
|
||||
};
|
||||
Reference in New Issue
Block a user