mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-05-19 19:50:26 +08:00
d164191bd1
Codex OAuth (ChatGPT Plus/Pro) providers previously fell through to the
default UsageFooter branch and showed no quota at all, while Copilot and
official Codex providers already had a wham/usage-backed quota footer.
This wires up the same five-hour / seven-day tier badges for codex_oauth
provider cards by reusing the existing query_codex_quota function and
SubscriptionQuotaFooter rendering, parameterized to keep both the CLI
credential path ("codex") and the cc-switch managed OAuth path
("codex_oauth") working from a single source of truth.
- Parameterize services::subscription::query_codex_quota with tool_label
and expired_message; promote SubscriptionQuota constructors to
pub(crate). The CLI path keeps its existing "codex" label and the
"re-login with Codex CLI" message; the new path passes "codex_oauth"
and a cc-switch-specific re-login hint.
- Add a new get_codex_oauth_quota Tauri command in commands/codex_oauth.rs
that resolves the ChatGPT account (explicit binding > default account
> not_found), pulls a valid access_token from CodexOAuthManager
(auto-refresh handled), and delegates to query_codex_quota.
- Extract SubscriptionQuotaFooter's render body into a pure
SubscriptionQuotaView component (props: quota / loading / refetch /
appIdForExpiredHint / inline). The existing SubscriptionQuotaFooter
becomes a thin wrapper with identical props and behavior, so
CopilotQuotaFooter and the official Claude/Codex/Gemini paths are
untouched. This avoids duplicating ~280 lines of five-state rendering.
- Add CodexOauthQuotaFooter, a 38-line wrapper that calls the new
useCodexOauthQuota hook and forwards to SubscriptionQuotaView.
- ProviderCard inserts an isCodexOauth branch between isCopilot and
isOfficial, keyed off PROVIDER_TYPES.CODEX_OAUTH (newly added to
config/constants.ts to centralize the previously scattered string).
- Frontend hook caches per (codex_oauth, accountId) so multiple cards
bound to the same ChatGPT account share one fetch via react-query
dedup; cards bound to different accounts get independent fetches.
- No new i18n keys: existing subscription.fiveHour / sevenDay / expired /
refresh / queryFailed / expiredHint are reused.
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
||
import { subscriptionApi } from "@/lib/api/subscription";
|
||
import type { AppId } from "@/lib/api/types";
|
||
import type { ProviderMeta } from "@/types";
|
||
import { resolveManagedAccountId } from "@/lib/authBinding";
|
||
import { PROVIDER_TYPES } from "@/config/constants";
|
||
|
||
const REFETCH_INTERVAL = 5 * 60 * 1000; // 5 minutes
|
||
|
||
export function useSubscriptionQuota(appId: AppId, enabled: boolean) {
|
||
return useQuery({
|
||
queryKey: ["subscription", "quota", appId],
|
||
queryFn: () => subscriptionApi.getQuota(appId),
|
||
enabled: enabled && ["claude", "codex", "gemini"].includes(appId),
|
||
refetchInterval: REFETCH_INTERVAL,
|
||
refetchOnWindowFocus: true,
|
||
staleTime: REFETCH_INTERVAL,
|
||
retry: 1,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Codex OAuth (ChatGPT Plus/Pro 反代) 订阅额度查询 hook
|
||
*
|
||
* 与 `useSubscriptionQuota` 平行:数据走 cc-switch 自管的 OAuth token,
|
||
* 而不是 Codex CLI 的 ~/.codex/auth.json。
|
||
*
|
||
* Query key 包含 accountId,多张卡片绑定到同一账号时会自动去重共享请求。
|
||
* accountId 为 null 时使用 "default" 占位,让后端 fallback 到默认账号。
|
||
*/
|
||
export function useCodexOauthQuota(
|
||
meta: ProviderMeta | undefined,
|
||
enabled: boolean,
|
||
) {
|
||
const accountId = resolveManagedAccountId(meta, PROVIDER_TYPES.CODEX_OAUTH);
|
||
return useQuery({
|
||
queryKey: ["codex_oauth", "quota", accountId ?? "default"],
|
||
queryFn: () => subscriptionApi.getCodexOauthQuota(accountId),
|
||
enabled,
|
||
refetchInterval: REFETCH_INTERVAL,
|
||
refetchOnWindowFocus: true,
|
||
staleTime: REFETCH_INTERVAL,
|
||
retry: 1,
|
||
});
|
||
}
|