- Consolidated and reordered imports in various files for better readability and maintainability. - Removed unused imports and ensured consistent import styles. - Enhanced the structure of components by grouping related imports together. - Updated the layout and organization of hooks to streamline functionality. - Improved the overall code quality by following best practices in import management.
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import useSWR from "swr";
|
|
|
|
import { getRunningMode, isAdmin, isServiceAvailable } from "@/services/cmds";
|
|
|
|
/**
|
|
* 自定义 hook 用于获取系统运行状态
|
|
* 包括运行模式、管理员状态、系统服务是否可用
|
|
*/
|
|
export function useSystemState() {
|
|
// 获取运行模式
|
|
const { data: runningMode = "Sidecar", mutate: mutateRunningMode } = useSWR(
|
|
"getRunningMode",
|
|
getRunningMode,
|
|
{
|
|
suspense: false,
|
|
revalidateOnFocus: false,
|
|
},
|
|
);
|
|
const isSidecarMode = runningMode === "Sidecar";
|
|
const isServiceMode = runningMode === "Service";
|
|
|
|
// 获取管理员状态
|
|
const { data: isAdminMode = false } = useSWR("isAdmin", isAdmin, {
|
|
suspense: false,
|
|
revalidateOnFocus: false,
|
|
});
|
|
|
|
const { data: isServiceOk = false, mutate: mutateServiceOk } = useSWR(
|
|
"isServiceAvailable",
|
|
isServiceAvailable,
|
|
{
|
|
suspense: false,
|
|
revalidateOnFocus: false,
|
|
onSuccess: (data) => {
|
|
console.log("[useSystemState] 服务状态更新:", data);
|
|
},
|
|
onError: (error) => {
|
|
console.error("[useSystemState] 服务状态检查失败:", error);
|
|
},
|
|
isPaused: () => !isServiceMode, // 仅在非 Service 模式下暂停请求
|
|
},
|
|
);
|
|
|
|
const isTunModeAvailable = isAdminMode || isServiceOk;
|
|
|
|
return {
|
|
runningMode,
|
|
isAdminMode,
|
|
isSidecarMode,
|
|
isServiceMode,
|
|
isServiceOk,
|
|
isTunModeAvailable: isTunModeAvailable,
|
|
mutateRunningMode,
|
|
mutateServiceOk,
|
|
};
|
|
}
|