diff --git a/src/hooks/use-system-proxy-state.ts b/src/hooks/use-system-proxy-state.ts index 75ab9c6bc..9c169ac5a 100644 --- a/src/hooks/use-system-proxy-state.ts +++ b/src/hooks/use-system-proxy-state.ts @@ -48,10 +48,14 @@ export const useSystemProxyState = () => { // Coalesce rapid clicks so only the latest requested state is applied. const pendingRef = useRef(null) const busyRef = useRef(false) + // The drain loop outlives the render it started in; reading the config + // through a ref keeps mid-loop revalidations visible to later iterations. + const vergeRef = useRef(verge) + vergeRef.current = verge const toggleSystemProxy = async (enabled: boolean) => { // Roll failed optimistic writes back to the latest confirmed state. - let confirmed = verge?.enable_system_proxy ?? false + let confirmed = vergeRef.current?.enable_system_proxy ?? false mutateVerge((prev) => prev ? { ...prev, enable_system_proxy: enabled } : prev, ) @@ -69,7 +73,7 @@ export const useSystemProxyState = () => { errorNotice: false, }) confirmed = target - if (!target && verge?.auto_close_connection) { + if (!target && vergeRef.current?.auto_close_connection) { await mutate(() => closeAllConnections(), { id: 'close-all-connections', errorNotice: false,