fix(proxy): sync system proxy switch state with background highlight #5240, #5439, #5421, #4651, #4536

Fixed race condition where button state and background color would desync
during rapid toggles or slow backend responses.

Root causes:
- Button used actualState while background used configState
- toggleSystemProxy returned immediately via setTimeout(0), releasing lock
  before backend operation completed
- 100ms delay in updateProxyStatus caused visual flash

Changes:
- Unified state source: both button and background now use actualState
- Made toggleSystemProxy truly async with proper await chain
- Wrapped toggle function with useLockFn to serialize operations
- Reduced state refresh delay from 100ms to 50ms
- Fixed GuardState lock release timing for async operations
- Added proper error rollback with state refresh on failure

Resolves issue where proxy toggle button would show as off while
background remained green, or vice versa.
This commit is contained in:
Tunglies
2025-11-23 12:17:52 +08:00
parent ddd24eb3ac
commit 44adf55975
3 changed files with 24 additions and 21 deletions
@@ -123,7 +123,7 @@ const ProxyControlSwitches = ({
const sysproxyRef = useRef<DialogRef>(null);
const tunRef = useRef<DialogRef>(null);
const { enable_tun_mode, enable_system_proxy } = verge ?? {};
const { enable_tun_mode } = verge ?? {};
const showErrorNotice = useCallback(
(msg: string) => showNotice.error(msg),
@@ -175,7 +175,7 @@ const ProxyControlSwitches = ({
onInfoClick={() => sysproxyRef.current?.open()}
onToggle={(value) => toggleSystemProxy(value)}
onError={onError}
highlight={enable_system_proxy}
highlight={systemProxyActualState}
/>
)}