A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io
Rust ai-toolsclaude-codecodexdeepseek-harnessdesktop-appgrokgrokbuildhermeskimi-codemcpopen-sourceopencodepiprovider-managementrustskillsskills-managementtauriwsl-support
import { RefreshCw } from "lucide-react";
import { useTranslation } from "react-i18next";
import {
useIsFetching,
useQueryClient,
type Query,
type QueryClient,
} from "@tanstack/react-query";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import type { ManagedAuthStatus } from "@/lib/api/auth";
import {
DSH_ACCOUNT_STATUS_KEY,
markDshForceRefresh,
} from "@/components/providers/DshAccount";
import {
WORKBUDDY_QUOTA_KEY,
markWorkBuddyQuotaForceRefresh,
} from "@/components/providers/workbuddyQuota";
import {
agyAuthStatusQuery,
markAgyQuotaForceRefresh,
markCodebuddyQuotaForceRefresh,
} from "@/lib/query/subscription";
/** 授权中心各组账号行的额度缓存:`[服务, "quota", 账号]` */
const QUOTA_SERVICES = new Set([
"claude_official",
"copilot",
"codex_oauth",
"xai_oauth",
"kimi_oauth",
"agy_oauth",
"codebuddy_oauth",
]);
/**
* 当前页面上(有组件在用)的账号额度。只手动查询时这些 query 是 disabled 的,
* `queryClient.refetchQueries` 会把 disabled 的过滤掉,所以要直接对 query 调 `fetch()`。
*/
function isShownAccountQuotaQuery(
query: Query,
queryClient: QueryClient,
): boolean {
return (
query.getObserversCount() > 0 &&
isAccountQuotaQuery(query) &&
!isNonCurrentAgyQuotaQuery(query, queryClient)
);
}
/** Antigravity 只能查 agy 当前登着的号;登录状态已知时,非当前号直接跳过,免得多读钥匙串。 */
function isNonCurrentAgyQuotaQuery(
query: Query,
queryClient: QueryClient,
): boolean {
const [service, kind, accountId] = query.queryKey;
if (service !== "agy_oauth" || kind !== "quota") return false;
const status = queryClient.getQueryData<ManagedAuthStatus>(
agyAuthStatusQuery.queryKey,
);
if (!status) return false;
return status.default_account_id !== accountId;
}
function isAccountQuotaQuery({ queryKey }: Query): boolean {
const [service, kind] = queryKey;
if (service === DSH_ACCOUNT_STATUS_KEY[0]) return true;
if (service === WORKBUDDY_QUOTA_KEY[0]) return true;
return (
typeof service === "string" &&
QUOTA_SERVICES.has(service) &&
kind === "quota"
);
}
/**
* 授权中心页头的「全部刷新」:重查当前页面上所有账号行的额度(DeepSeek 是余额),
* 跟每行自己的 ↻ 走同一份缓存,所以各行也会一起转圈。
*/
export function RefreshAllQuotaButton() {
const { t } = useTranslation();
const queryClient = useQueryClient();
const fetching = useIsFetching({ predicate: isAccountQuotaQuery }) > 0;
return (
<Button
type="button"
variant="quiet"
size="regular"
disabled={fetching}
onClick={() => {
markAgyQuotaForceRefresh();
markCodebuddyQuotaForceRefresh();
markDshForceRefresh();
markWorkBuddyQuotaForceRefresh();
void Promise.all(
queryClient
.getQueryCache()
.findAll({
predicate: (query) =>
isShownAccountQuotaQuery(query, queryClient),
})
.map((query) => query.fetch().catch(() => undefined)),
);
}}
>
<RefreshCw
className={cn("h-4 w-4", fetching && "motion-safe:animate-spin")}
strokeWidth={2}
/>
{t("authCenter.quota.refreshAll", { defaultValue: "全部刷新" })}
</Button>
);
}