Skip to content

fix(keys): refresh API key form options - #5512

Merged
t0ng7u merged 1 commit into
mainfrom
fix/api-key-options-refresh
Jun 15, 2026
Merged

fix(keys): refresh API key form options#5512
t0ng7u merged 1 commit into
mainfrom
fix/api-key-options-refresh

Conversation

@QuentinHsu

@QuentinHsu QuentinHsu commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

问题描述

  • API key 新建/编辑表单的分组和模型选项会被前端缓存 5 分钟,后台配置更新后需要刷新整个页面才能看到最新数据。
  • API key 表格中的分组倍率查询使用了另一个 query key,导致同一接口存在重复缓存。

修复方式

  • 将表单中的 user-modelsuser-groups 查询改为 drawer 打开时启用,并设置 staleTime: 0
  • 将表格分组倍率查询统一到 user-groups query key,并取消 5 分钟 stale 缓存。

测试说明

  • 已运行 bun run typecheck

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

Bug Fixes

  • Enhanced data accuracy in the API keys management interface by modifying caching behavior for group and model information to ensure users always see current data.
  • Improved performance by implementing conditional data queries that only execute when the API keys drawer is open, reducing unnecessary network requests and system load.

- enable model and group queries only while the API key drawer is open and remove the five-minute stale window so the form uses fresh backend options.
- align the API key table group-ratio query key to avoid duplicate caches for the same group endpoint.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Two React Query configurations in the API keys feature are updated: useGroupRatios in api-keys-columns.tsx switches its queryKey from ['user-self-groups'] to ['user-groups'] and sets staleTime: 0. ApiKeysMutateDrawer adds enabled: open and staleTime: 0 to both the models and groups queries.

Changes

API Keys React Query Cache Configuration

Layer / File(s) Summary
Query key, staleTime, and enabled flag updates
web/default/src/features/keys/components/api-keys-columns.tsx, web/default/src/features/keys/components/api-keys-mutate-drawer.tsx
useGroupRatios switches its queryKey to ['user-groups'] and sets staleTime: 0. Both the models and groups queries in ApiKeysMutateDrawer gain enabled: open and staleTime: 0, preventing fetches when the drawer is closed and disabling stale data retention.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • QuantumNous/new-api#4772: Previously switched useGroupRatios to use getUserGroups; this PR continues adjusting the same hook's queryKey and staleTime.

Poem

🐇 Hop, hop! The cache goes stale no more,
With staleTime: 0 at every door,
The drawer stays quiet when it's shut,
No extra fetches in a rut.
Fresh data only — that's the score! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(keys): refresh API key form options' clearly and specifically summarizes the main objective of the PR - fixing caching issues to refresh form options for API keys.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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/api-key-options-refresh

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 `@web/default/src/features/keys/components/api-keys-mutate-drawer.tsx`:
- Around line 115-116: The form reset effect on line 150 that depends on
backendHasAuto is re-running when the user-groups query completes after the
drawer opens, causing unwanted form resets in create mode. To fix this, add a
condition to the reset effect to check if the form is dirty (using
form.formState.isDirty) before resetting, so that any user edits are preserved.
Specifically, wrap the reset logic with a guard that prevents resetting when the
form has been modified by the user (isDirty is true).
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62f44946-ab5f-425d-9fd5-91be28b4f064

📥 Commits

Reviewing files that changed from the base of the PR and between 1ac0f58 and 4c3a398.

📒 Files selected for processing (2)
  • web/default/src/features/keys/components/api-keys-columns.tsx
  • web/default/src/features/keys/components/api-keys-mutate-drawer.tsx

Comment on lines +115 to +116
enabled: open,
staleTime: 0,

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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read-only verification: confirm reset effect depends on backendHasAuto and is triggered by groups query timing.
rg -n -C3 "enabled:\s*open|backendHasAuto|form\.reset\(" web/default/src/features/keys/components/api-keys-mutate-drawer.tsx

Repository: QuantumNous/new-api

Length of output: 1394


Prevent form reset triggered by delayed user-groups query completion.

The enabled: open gate on line 115 delays groupsData load, causing backendHasAuto to transition from false to true after the drawer opens. Since the reset effect on line 150 depends on backendHasAuto, it re-runs when the groups query completes, resetting form values in create mode even if the user has started editing. Gate the reset to the initial open transition only, or add a check like !form.formState.isDirty to avoid resetting if the form has been modified.

🤖 Prompt for 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.

In `@web/default/src/features/keys/components/api-keys-mutate-drawer.tsx` around
lines 115 - 116, The form reset effect on line 150 that depends on
backendHasAuto is re-running when the user-groups query completes after the
drawer opens, causing unwanted form resets in create mode. To fix this, add a
condition to the reset effect to check if the form is dirty (using
form.formState.isDirty) before resetting, so that any user edits are preserved.
Specifically, wrap the reset logic with a guard that prevents resetting when the
form has been modified by the user (isDirty is true).

@t0ng7u t0ng7u self-assigned this Jun 15, 2026
@t0ng7u
t0ng7u merged commit aeea3fa into main Jun 15, 2026
2 checks passed
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Upstream changes (50+ commits, v1.0.0-rc.11):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch model selector (QuantumNous#5515),
  API key form options (QuantumNous#5512), cell overflow (QuantumNous#5510), kimi k2.6 temp (QuantumNous#5390),
  Anthropic-compatible GLM chunked encoding (QuantumNous#5307), streaming image relay (QuantumNous#4608)
- Feat: audit auth method tracking (QuantumNous#5462), channel affinity clear toggle (QuantumNous#5306),
  relay idle timeout config (QuantumNous#5309), 6-decimal pricing precision (QuantumNous#5332)
- Classic frontend: Rsbuild support, Semi React 19 adapter
- Shared dialog wrapper, JSON code editor, debounce channel search

Conflict resolved: web/bun.lock (accepted upstream, will regenerate)

Co-Authored-By: Claude <noreply@anthropic.com>
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Merge upstream v1.0.0-rc.11 (50+ commits):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch (QuantumNous#5515), API key (QuantumNous#5512),
  kimi k2.6 temp (QuantumNous#5390), GLM chunked encoding (QuantumNous#5307), streaming image (QuantumNous#4608)
- Feat: audit auth tracking (QuantumNous#5462), channel affinity toggle (QuantumNous#5306),
  relay idle timeout (QuantumNous#5309), 6-decimal pricing (QuantumNous#5332)
- Shared dialog wrapper, JSON code editor, classic Rsbuild support

SEO optimization:
- robots.txt: 10 AI crawler blocks + 22 path disallows + crawl-delay
- sitemap.xml: 7 public URLs with 6-language hreflang annotations
- index.html: hreflang tags, og:locale:alternate, og:image, canonical,
  5 structured data types (Organization, SoftwareApplication, FAQPage,
  WebSite, SearchAction), expanded keywords (gateway, agent router,
  aggregation, orchestration)
- i18n/config.ts: sync <html lang> with active language for SEO

Co-Authored-By: Claude <noreply@anthropic.com>
@Calcium-Ion
Calcium-Ion deleted the fix/api-key-options-refresh branch June 24, 2026 10:42
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
- enable model and group queries only while the API key drawer is open and remove the five-minute stale window so the form uses fresh backend options.
- align the API key table group-ratio query key to avoid duplicate caches for the same group endpoint.
noah-wung pushed a commit to noah-wung/new-api that referenced this pull request Jul 17, 2026
- enable model and group queries only while the API key drawer is open and remove the five-minute stale window so the form uses fresh backend options.
- align the API key table group-ratio query key to avoid duplicate caches for the same group endpoint.
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
- enable model and group queries only while the API key drawer is open and remove the five-minute stale window so the form uses fresh backend options.
- align the API key table group-ratio query key to avoid duplicate caches for the same group endpoint.
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
- enable model and group queries only while the API key drawer is open and remove the five-minute stale window so the form uses fresh backend options.
- align the API key table group-ratio query key to avoid duplicate caches for the same group endpoint.
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.

2 participants