Skip to content

fix: 代理开关状态切换页面后不保存 (#183)#185

Merged
AmintaCCCP merged 2 commits into
mainfrom
fix/proxy-toggle-not-persisting
Jun 1, 2026
Merged

fix: 代理开关状态切换页面后不保存 (#183)#185
AmintaCCCP merged 2 commits into
mainfrom
fix/proxy-toggle-not-persisting

Conversation

@AmintaCCCP

@AmintaCCCP AmintaCCCP commented Jun 1, 2026

Copy link
Copy Markdown
Owner

问题

设置 → 网络设置 → 代理开关一旦开启后无法关闭。关闭开关后切到别的页面再切回来,开关仍然显示打开状态。

根因

代理开关的 只更新了本地 React state(),没有将 状态持久化到 Zustand store。用户需要手动点击「保存」按钮才会生效,但 toggle 开关的 UX 语义是即时生效的。

修复

修改 toggle 的 处理器,在切换时立即同步 状态到:

  • 本地 form state()— 即时视觉反馈
  • Zustand store()— 页面切换后状态不丢失
  • 后端()— 保持服务端代理配置一致
  • Electron 主进程()— 保持 Electron 代理配置一致

所有远程同步均为 best-effort,不会因网络问题阻塞 UI。

Fixes #183

Summary by CodeRabbit

  • Bug Fixes
    • Network panel toggles now persist immediately when changed, removing the need for a separate save action.
    • Changes are now propagated automatically with best-effort synchronization to remote and system configuration.

The proxy enable/disable toggle only updated local React state (setForm)
without persisting to the Zustand store. Users expected the toggle to
take effect immediately (standard UX for switches), but the change was
lost when navigating away from the Network panel.

Now the toggle immediately syncs the enabled state to:
- Local form state (instant visual feedback)
- Zustand store (survives page navigation)
- Backend via PUT /api/settings/proxy (keeps server config in sync)
- Electron main process via electronProxy.setProxy

Fixes #183

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f7876d45-563d-48c5-a5d2-b92b4e2d78c2

📥 Commits

Reviewing files that changed from the base of the PR and between 2d6d3f6 and 5fb2dab.

📒 Files selected for processing (1)
  • src/components/settings/NetworkPanel.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/settings/NetworkPanel.tsx

📝 Walkthrough

Walkthrough

The NetworkPanel toggle handler now updates local form state, immediately persists the new proxy config via setProxyConfig, and best-effort syncs the change to the backend API and Electron (both in try/catch, without response validation).

Changes

Immediate Proxy Toggle Persistence

Layer / File(s) Summary
Async enable/disable toggle with multi-target sync
src/components/settings/NetworkPanel.tsx
The toggle's onClick handler computes newForm, calls setForm and setProxyConfig(newForm), then attempts to PUT /api/settings/proxy when backend.isAvailable and to call electronProxy.setProxy(newForm) when running in Electron; both remote calls use best-effort try/catch and do not validate response status.

Sequence Diagram

sequenceDiagram
  participant User
  participant NetworkPanel
  participant ProxyStore
  participant BackendAPI
  participant Electron
  User->>NetworkPanel: Click toggle
  NetworkPanel->>NetworkPanel: Compute newForm (toggled enabled)
  NetworkPanel->>ProxyStore: setProxyConfig(newForm)
  ProxyStore-->>NetworkPanel: Store updated
  NetworkPanel->>BackendAPI: PUT /api/settings/proxy (best-effort)
  BackendAPI-->>NetworkPanel: Response (ignored/non-blocking)
  NetworkPanel->>Electron: electronProxy.setProxy(newForm) (best-effort)
  Electron-->>NetworkPanel: Ack (best-effort)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • AmintaCCCP/GithubStarsManager#163: Implements initial network proxy settings configuration that this PR extends with immediate toggle persistence and multi-target sync.

Poem

A rabbit flips the proxy switch with glee,
It hops to the store and sets the key,
Then whispers to backend, then taps Electron's door,
Best-effort nudges — no blocking chore,
Quick syncs, small hops — the network's set free. 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the fix: proxy toggle state not persisting after page navigation, which is the core issue being addressed.
Linked Issues check ✅ Passed The PR implementation fulfills issue #183 requirements: toggle now persists state via Zustand store, backend sync, and Electron sync, enabling immediate persistence without manual save.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the proxy toggle persistence issue; no unrelated modifications detected beyond the toggle handler and state synchronization logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/proxy-toggle-not-persisting

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/settings/NetworkPanel.tsx`:
- Around line 122-146: The code is sending inconsistent payloads: it updates the
store with only { enabled: newEnabled } and sends { ...proxyConfig, enabled:
newEnabled } to the backend while sending newForm to Electron, which can discard
uncommitted edits due to the useEffect that mirrors proxyConfig into form. Fix
by treating newForm as the single source of truth: call setProxyConfig(newForm)
(not setProxyConfig({ enabled: newEnabled })), send newForm as the body to
fetch('/api/settings/proxy'), and continue using
electronProxy.setProxy(newForm); keep the existing authHeaders/try-catch logic
so both backend and Electron receive the identical newForm payload.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 72ab9683-ce3a-4054-8153-50f8d274cda8

📥 Commits

Reviewing files that changed from the base of the PR and between ae47396 and 2d6d3f6.

📒 Files selected for processing (1)
  • src/components/settings/NetworkPanel.tsx

Comment thread src/components/settings/NetworkPanel.tsx
… sync

Address CodeRabbit review: send identical newForm to all three targets
instead of mixing { enabled }, { ...proxyConfig, enabled }, and newForm.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@AmintaCCCP AmintaCCCP merged commit 79788cc into main Jun 1, 2026
5 checks passed
@AmintaCCCP AmintaCCCP deleted the fix/proxy-toggle-not-persisting branch June 1, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

网络代理无法关闭

1 participant