mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-18 18:02:08 +08:00
refactor: replace useSWR with custom hooks for update and network interfaces (#6195)
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user