refactor: replace useSWR with custom hooks for update and network interfaces (#6195)

This commit is contained in:
Tunglies
2026-01-27 12:52:20 +00:00
committed by GitHub
parent 36926df26c
commit c57a962109
8 changed files with 97 additions and 69 deletions
+5 -4
View File
@@ -51,11 +51,12 @@ const validatePorts = (patch: ClashInfoPatch) => {
});
};
export const useRuntimeConfig = (shouldFetch: boolean = true) => {
return useSWR(shouldFetch ? "getRuntimeConfig" : null, getRuntimeConfig);
};
export const useClash = () => {
const { data: clash, mutate: mutateClash } = useSWR(
"getRuntimeConfig",
getRuntimeConfig,
);
const { data: clash, mutate: mutateClash } = useRuntimeConfig();
const { data: versionData, mutate: mutateVersion } = useSWR(
"getVersion",
+22
View File
@@ -0,0 +1,22 @@
import useSWR from "swr";
import { getNetworkInterfacesInfo } from "@/services/cmds";
export const useNetworkInterfaces = () => {
const { data, error, isLoading, mutate } = useSWR(
"getNetworkInterfacesInfo",
getNetworkInterfacesInfo,
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
fallbackData: [],
},
);
return {
networkInterfaces: data || [],
loading: isLoading,
error,
mutate,
};
};
+46
View File
@@ -0,0 +1,46 @@
import useSWR, { SWRConfiguration } from "swr";
import { checkUpdateSafe } from "@/services/update";
import { useVerge } from "./use-verge";
export interface UpdateInfo {
version: string;
body: string;
date: string;
available: boolean;
downloadAndInstall: (onEvent?: any) => Promise<void>;
}
export const useUpdate = (
enabled: boolean = true,
options?: SWRConfiguration,
) => {
const { verge } = useVerge();
const { auto_check_update } = verge || {};
// Determine if we should check for updates
// If enabled is explicitly false, don't check
// Otherwise, respect the auto_check_update setting (or default to true if null/undefined for manual triggers)
const shouldCheck = enabled && auto_check_update !== false;
const {
data: updateInfo,
mutate: checkUpdate,
isValidating,
} = useSWR(shouldCheck ? "checkUpdate" : null, checkUpdateSafe, {
errorRetryCount: 2,
revalidateIfStale: false,
revalidateOnFocus: false,
focusThrottleInterval: 36e5, // 1 hour
refreshInterval: 24 * 60 * 60 * 1000, // 24 hours
dedupingInterval: 60 * 60 * 1000, // 1 hour
...options,
});
return {
updateInfo,
checkUpdate,
loading: isValidating,
};
};