fix(usage): 关闭「启用用量查询」后不再发起查询且隐藏用量行
useUsageQuery 增加 disabled 门控:关闭时自动查询不发、refresh no-op、 状态立即复位 idle(UsageFooter 对 idle 返回 null,compact/detail 均 整体隐藏);在途响应按 generation 作废且不写入模块级共享缓存, 避免关闭瞬间的失败结果在 5 分钟内重新挂载时复活。
This commit is contained in:
1 parent
36fc9c50a8
commit
1de8b9ee4c
2 files changed
+20
-7
No files matched your search
@@ -69,7 +69,8 @@ function ProviderCard({
|
|||||||
agent,
|
agent,
|
||||||
provider.name,
|
provider.name,
|
||||||
provider.usageKinds,
|
provider.usageKinds,
|
||||||
provider.usageConfig?.autoQueryIntervalMinutes
|
provider.usageConfig?.autoQueryIntervalMinutes,
|
||||||
|
provider.usageConfig?.enabled === false
|
||||||
);
|
);
|
||||||
const providerModels = Object.values(models).filter(
|
const providerModels = Object.values(models).filter(
|
||||||
(m) => m.provider === provider.name
|
(m) => m.provider === provider.name
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ export function useUsageQuery(
|
|||||||
agent: Agent,
|
agent: Agent,
|
||||||
providerName: string,
|
providerName: string,
|
||||||
usageKinds?: string[],
|
usageKinds?: string[],
|
||||||
autoIntervalMinutes?: number
|
autoIntervalMinutes?: number,
|
||||||
|
disabled?: boolean
|
||||||
): UsageQueryState {
|
): UsageQueryState {
|
||||||
const supported = (usageKinds?.length ?? 0) > 0;
|
const supported = (usageKinds?.length ?? 0) > 0;
|
||||||
// Cache key includes the agent so a Kimi Code provider and a Pi provider
|
// Cache key includes the agent so a Kimi Code provider and a Pi provider
|
||||||
@@ -79,12 +80,13 @@ export function useUsageQuery(
|
|||||||
|
|
||||||
const runQuery = useCallback(
|
const runQuery = useCallback(
|
||||||
async (forceRefresh: boolean) => {
|
async (forceRefresh: boolean) => {
|
||||||
|
if (disabled) return;
|
||||||
const gen = ++genRef.current;
|
const gen = ++genRef.current;
|
||||||
setStatus("loading");
|
setStatus("loading");
|
||||||
setError(undefined);
|
setError(undefined);
|
||||||
const finish = (entry: CacheEntry) => {
|
const finish = (entry: CacheEntry) => {
|
||||||
cache.set(cacheKey, entry);
|
|
||||||
if (genRef.current !== gen) return;
|
if (genRef.current !== gen) return;
|
||||||
|
cache.set(cacheKey, entry);
|
||||||
setStatus(entry.status);
|
setStatus(entry.status);
|
||||||
setData(entry.data);
|
setData(entry.data);
|
||||||
setError(entry.error);
|
setError(entry.error);
|
||||||
@@ -129,11 +131,21 @@ export function useUsageQuery(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agent, providerName, cacheKey]
|
[agent, providerName, cacheKey, disabled]
|
||||||
);
|
);
|
||||||
|
|
||||||
// On mount / provider change: serve fresh cache, otherwise query once.
|
// On mount / provider change: serve fresh cache, otherwise query once.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (disabled) {
|
||||||
|
// Toggle back off: stop immediately and return to idle so the footer
|
||||||
|
// hides; bump gen so an in-flight response is dropped (no stale write).
|
||||||
|
genRef.current += 1;
|
||||||
|
setStatus("idle");
|
||||||
|
setData([]);
|
||||||
|
setError(undefined);
|
||||||
|
setUpdatedAt(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!supported) return;
|
if (!supported) return;
|
||||||
const cached = cache.get(cacheKey);
|
const cached = cache.get(cacheKey);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
@@ -144,7 +156,7 @@ export function useUsageQuery(
|
|||||||
if (Date.now() - cached.updatedAt < STALE_TTL_MS) return;
|
if (Date.now() - cached.updatedAt < STALE_TTL_MS) return;
|
||||||
}
|
}
|
||||||
void runQuery(false);
|
void runQuery(false);
|
||||||
}, [supported, cacheKey, runQuery]);
|
}, [supported, cacheKey, runQuery, disabled]);
|
||||||
|
|
||||||
// Ignore late responses after unmount.
|
// Ignore late responses after unmount.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -156,12 +168,12 @@ export function useUsageQuery(
|
|||||||
// Auto query interval — the hook is called once per provider card and both
|
// Auto query interval — the hook is called once per provider card and both
|
||||||
// variants read the same state, so there is no second mount to double-fire.
|
// variants read the same state, so there is no second mount to double-fire.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!supported) return;
|
if (!supported || disabled) return;
|
||||||
const mins = autoIntervalMinutes ?? 0;
|
const mins = autoIntervalMinutes ?? 0;
|
||||||
if (mins <= 0) return;
|
if (mins <= 0) return;
|
||||||
const id = setInterval(() => void runQuery(false), mins * 60_000);
|
const id = setInterval(() => void runQuery(false), mins * 60_000);
|
||||||
return () => clearInterval(id);
|
return () => clearInterval(id);
|
||||||
}, [supported, autoIntervalMinutes, runQuery]);
|
}, [supported, autoIntervalMinutes, runQuery, disabled]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status,
|
status,
|
||||||
|
|||||||
Reference in new issue
Block a user