Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion apps/desktop-tauri/src-tauri/src/commands/usage_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ fn usage_spend_cache_key(cached: &[ProviderUsageSnapshot], selected_days: u32) -
let cost = snapshot
.cost
.as_ref()
.map(|cost| format!("{:.8}:{}", cost.used, cost.period))
.map(|cost| {
let daily = cost
.daily
.iter()
.map(|point| format!("{}:{:.8}", point.day, point.amount))
.collect::<Vec<_>>()
.join(",");
format!(
"{:.8}:{:?}:{:?}:{}:{}:{}",
cost.used, cost.limit, cost.balance, cost.currency_code, cost.period, daily
)
})
.unwrap_or_default();
format!(
"{}:{}:{}:{}",
Expand Down
51 changes: 38 additions & 13 deletions apps/desktop-tauri/src/surfaces/settings/tabs/UsageSpendTab.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { save } from "@tauri-apps/plugin-dialog";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { useLocale } from "../../../hooks/useLocale";
import {
getSettingsSnapshot,
Expand Down Expand Up @@ -163,24 +164,48 @@ export default function UsageSpendTab(_props: TabProps) {
});
}, []);

const load = useCallback((forceRefresh = false) => {
setLoading(true);
setError(null);
void getUsageSpendSummary({ historyDays: selectedDays, forceRefresh })
.then((data) => {
setSummary(data);
setLoading(false);
})
.catch((err: unknown) => {
setError(err instanceof Error ? err.message : String(err));
setLoading(false);
});
}, [includeOpenCodex, selectedDays]);
const load = useCallback(async (forceRefresh = false, silent = false) => {
if (!silent) {
setLoading(true);
setError(null);
}
try {
const data = await getUsageSpendSummary({ historyDays: selectedDays, forceRefresh });
setSummary(data);
} catch (err: unknown) {
if (!silent) setError(err instanceof Error ? err.message : String(err));
} finally {
if (!silent) setLoading(false);
}
}, [hideNativeCodex, includeOpenCodex, selectedDays]);

useEffect(() => {
load(false);
}, [load]);

// Upstream 0.55.0 #3106 parity: provider token/cost publications refresh
// Usage & Spend silently while the pane is open. Coalesce bursts so a
// multi-provider refresh does not trigger one dashboard rebuild per event.
useEffect(() => {
let unlisten: UnlistenFn | null = null;
let timer: ReturnType<typeof setTimeout> | null = null;
let cancelled = false;
void listen("provider-updated", () => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
if (!cancelled) void load(false, true);
}, 200);
}).then((fn) => {
if (cancelled) fn();
else unlisten = fn;
}).catch(() => {});
return () => {
cancelled = true;
if (timer) clearTimeout(timer);
unlisten?.();
};
}, [load]);

const onShare = useCallback(() => {
setShareError(null);
if (!summary) {
Expand Down
Loading