fix: 修正 Codex free 账号用量显示到每周窗口 - #3316
Conversation
WalkthroughThis change enhances rate limit window resolution logic in the CodexUsageModal component by introducing new helper functions to derive normalized window data from rate_limit and plan_type, while improving null-safety in UI rendering when data is unavailable. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~35 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment Tip You can customize the high-level summary generated by CodeRabbit.Configure the |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
web/src/components/table/channels/modals/CodexUsageModal.jsx (1)
57-60: Match the actual 5h / 7d durations instead of using a 24h split.Line 60 currently labels every window
< 24has “5小时窗口” and every window>= 24has “每周窗口”. A 6-hour or 24-hour upstream window would be shown under the wrong card title. Prefer matching the known 5-hour / 7-day values, then leaving unknown durations unmapped.♻️ Proposed change
+const FIVE_HOUR_WINDOW_SECONDS = 5 * 60 * 60; +const WEEKLY_WINDOW_SECONDS = 7 * 24 * 60 * 60; +const WINDOW_TOLERANCE_SECONDS = 60; + const classifyWindowByDuration = (windowData) => { const seconds = getWindowDurationSeconds(windowData); if (seconds == null) return null; - return seconds >= 24 * 60 * 60 ? 'weekly' : 'fiveHour'; + if ( + Math.abs(seconds - FIVE_HOUR_WINDOW_SECONDS) <= WINDOW_TOLERANCE_SECONDS + ) { + return 'fiveHour'; + } + if ( + Math.abs(seconds - WEEKLY_WINDOW_SECONDS) <= WINDOW_TOLERANCE_SECONDS + ) { + return 'weekly'; + } + return null; };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/CodexUsageModal.jsx` around lines 57 - 60, The classifyWindowByDuration function incorrectly splits at 24h; change it to map only the known durations by reading seconds from getWindowDurationSeconds(windowData) and returning 'fiveHour' when seconds equals 5 * 3600 and 'weekly' when seconds equals 7 * 24 * 3600, otherwise return null so unknown window lengths (e.g., 6h or 24h) remain unmapped; update the comparisons in classifyWindowByDuration accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/src/components/table/channels/modals/CodexUsageModal.jsx`:
- Around line 133-136: The current hasWindowData check only verifies keys exist
and will treat placeholder objects (e.g., { used_percent: null, reset_at: null
}) as populated; update the logic used by hasWindowData (or create a helper like
isMeaningfulWindowData) to ensure the object contains actual meaningful values
before rendering the usage card—for example require used_percent to be a finite
number (or other concrete fields you rely on) and/or non-null reset_at; then use
that stricter predicate wherever hasWindowData is referenced (including the
rendering block around the card currently at the render for lines ~152-175) so
placeholder objects are treated as empty.
---
Nitpick comments:
In `@web/src/components/table/channels/modals/CodexUsageModal.jsx`:
- Around line 57-60: The classifyWindowByDuration function incorrectly splits at
24h; change it to map only the known durations by reading seconds from
getWindowDurationSeconds(windowData) and returning 'fiveHour' when seconds
equals 5 * 3600 and 'weekly' when seconds equals 7 * 24 * 3600, otherwise return
null so unknown window lengths (e.g., 6h or 24h) remain unmapped; update the
comparisons in classifyWindowByDuration accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7b140fdf-cbaf-4bc7-9d9e-409745253591
📒 Files selected for processing (1)
web/src/components/table/channels/modals/CodexUsageModal.jsx
| const hasWindowData = | ||
| !!windowData && | ||
| typeof windowData === 'object' && | ||
| Object.keys(windowData).length > 0; |
There was a problem hiding this comment.
Treat placeholder objects as empty windows.
hasWindowData only checks whether the object has keys. If the API returns a placeholder like { used_percent: null, reset_at: null, ... }, Lines 152-175 still render a populated card with a 0% bar, which is the empty-state regression this PR is trying to avoid.
💡 Proposed fix
- const hasWindowData =
- !!windowData &&
- typeof windowData === 'object' &&
- Object.keys(windowData).length > 0;
+ const hasWindowData =
+ !!windowData &&
+ typeof windowData === 'object' &&
+ [
+ windowData?.used_percent,
+ windowData?.reset_at,
+ windowData?.reset_after_seconds,
+ getWindowDurationSeconds(windowData),
+ ].some((value) => value != null);Also applies to: 152-175
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/components/table/channels/modals/CodexUsageModal.jsx` around lines
133 - 136, The current hasWindowData check only verifies keys exist and will
treat placeholder objects (e.g., { used_percent: null, reset_at: null }) as
populated; update the logic used by hasWindowData (or create a helper like
isMeaningfulWindowData) to ensure the object contains actual meaningful values
before rendering the usage card—for example require used_percent to be a finite
number (or other concrete fields you rely on) and/or non-null reset_at; then use
that stricter predicate wherever hasWindowData is referenced (including the
rendering block around the card currently at the render for lines ~152-175) so
placeholder objects are treated as empty.
Summary
"plan_type": "free"时,将实际用量展示到“每周窗口”而不是“5小时窗口”0%Background
Codex 渠道用量弹窗当前固定将
rate_limit.primary_window渲染为“5小时窗口”,将rate_limit.secondary_window渲染为“每周窗口”。但在 free 账号场景下,上游返回中会包含
"plan_type": "free",此类账号没有 5 小时窗口,只有每周窗口。由于前端没有根据plan_type或窗口时长做实际归类,导致真实的周用量被错误展示在左侧“5小时窗口”中,右侧“每周窗口”反而为空或显示不正确。本 PR 修正该展示层问题,使 free 账号的额度信息能够正确落到每周窗口中,同时保留对非 free 场景的兼容。
Changes
web/src/components/table/channels/modals/CodexUsageModal.jsx新增额度窗口归类逻辑:
plan_type归一化处理limit_window_seconds的窗口识别逻辑plan_type === 'free'时,强制将有效窗口映射到“每周窗口”,不再错误显示到“5小时窗口”调整窗口卡片展示逻辑:
-0%Testing
plan_type: free场景下,用量显示在“每周窗口”Summary by CodeRabbit
Release Notes