43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { detectEnv } from "../ipc";
|
|
import type { PlatformEnv } from "../ipc/types";
|
|
|
|
export interface UseEnvResult {
|
|
env: PlatformEnv | null;
|
|
loading: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
// 本机环境本地缓存(Wave 2.2 Req 4):切换页面先用缓存立即渲染,后台静默刷新。
|
|
let cachedEnv: PlatformEnv | null = null;
|
|
|
|
/** 加载本机环境检测结果(缓存优先 + 后台刷新) */
|
|
export function useEnv(): UseEnvResult {
|
|
const [env, setEnv] = useState<PlatformEnv | null>(cachedEnv);
|
|
const [loading, setLoading] = useState(cachedEnv == null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
detectEnv()
|
|
.then((e) => {
|
|
if (!cancelled) {
|
|
cachedEnv = e;
|
|
setEnv(e);
|
|
setLoading(false);
|
|
}
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (!cancelled) {
|
|
setError(String(err));
|
|
setLoading(false);
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, []);
|
|
|
|
return { env, loading, error };
|
|
}
|