revert(web): restore sparse hourly usage breakdown (#7718)

Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: maria <[email protected]>
This commit is contained in:
t3-code[bot]andmaria authored and GitHub committed 2026-08-20 16:18:36 -04:00
1 parent 21e80a0636
commit aa17ec6e7d
2 files changed
+116 -20

No files matched your search

@@ -0,0 +1,110 @@
import { USAGE_CONTRACT_VERSION } from "@t3tools/contracts";
import { mergeUsage } from "@t3tools/shared/usageMerge";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const testState = vi.hoisted(() => ({
useUsage: vi.fn(),
}));
vi.mock("react", async (importOriginal) => {
const actual = await importOriginal<typeof import("react")>();
return {
...actual,
useState: vi.fn((initial: unknown) => [
typeof initial === "function"
? {
days: 1,
window: {
sinceDay: "2026-08-10",
untilDay: "2026-08-11",
timeZone: "UTC",
resolution: "hour",
sinceTime: "2026-08-10T12:37:00.000Z",
untilTime: "2026-08-11T12:37:00.000Z",
},
}
: initial === "model"
? "time"
: initial,
vi.fn(),
]),
};
});
vi.mock("../../env", () => ({ isElectron: false }));
vi.mock("../../state/usage", () => ({ useUsage: testState.useUsage }));
vi.mock("../ui/button", () => ({ Button: "button" }));
vi.mock("../ui/scroll-area", () => ({ ScrollArea: "div" }));
vi.mock("../ui/select", () => ({
Select: "div",
SelectItem: "div",
SelectPopup: "div",
SelectTrigger: "div",
SelectValue: "div",
}));
vi.mock("../ui/sidebar", () => ({ SidebarInset: "div" }));
vi.mock("../ui/toggle-group", () => ({ Toggle: "button", ToggleGroup: "div" }));
vi.mock("../WorkspaceBreadcrumb", () => ({
WorkspaceBreadcrumb: "div",
WorkspaceBreadcrumbItem: "div",
WorkspaceBreadcrumbSeparator: "span",
}));
vi.mock("../WorkspacePageContainer", () => ({ WorkspacePageContainer: "main" }));
vi.mock("../WorkspacePageHeader", () => ({ WorkspacePageHeader: "header" }));
vi.mock("./UsageProviderChart", () => ({ UsageProviderChart: "div" }));
vi.mock("./usageProviders", () => ({
PROVIDER_ORDER: ["codex", "claude"],
PROVIDER_PRESENTATION: {
codex: { color: "white", label: "Codex", mark: "span" },
claude: { color: "orange", label: "Claude Code", mark: "span" },
},
}));
import { UsagePage } from "./UsagePage";
const providerTotals = (codex: number, claude: number) =>
new Map([
["codex", { costUsd: codex, totalTokens: codex * 1_000 }],
["claude", { costUsd: claude, totalTokens: claude * 1_000 }],
] as const);
beforeEach(() => {
testState.useUsage.mockReturnValue({
merged: {
...mergeUsage([], USAGE_CONTRACT_VERSION),
hourly: [
{
day: "2026-08-10",
hourStart: "2026-08-10T13:37:00.000Z",
costUsd: 13,
totalTokens: 13_000,
byProvider: providerTotals(7, 6),
},
{
day: "2026-08-11",
hourStart: "2026-08-11T11:37:00.000Z",
costUsd: 11,
totalTokens: 11_000,
byProvider: providerTotals(6, 5),
},
],
},
environments: [],
isPending: false,
isPartial: false,
refresh: vi.fn(),
});
});
describe("UsagePage hourly breakdown", () => {
it("keeps recent activity visible first without empty hourly rows", () => {
const markup = renderToStaticMarkup(<UsagePage />);
const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
expect(body.match(/<tr/g)).toHaveLength(2);
expect(body).toContain("$11.00");
expect(body).toContain("$13.00");
expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
});
});
+6 -20
View File
@@ -68,15 +68,12 @@ export function UsagePage() {
: enumerateHourStarts(window.sinceTime, window.untilTime),
[window.sinceTime, window.untilTime],
);
// The hourly window is small enough to render every period: the table then
// reads chronologically like the chart, instead of jumping between the hours
// that happened to have activity. Daily windows can run 90 periods, so those
// stay newest-first with the interesting end on top.
const breakdownPeriods = useMemo<readonly (DailyTotals | HourlyTotals)[]>(() => {
if (!isPast24Hours) return merged.daily.toReversed();
const byHour = new Map(merged.hourly.map((entry) => [entry.hourStart, entry]));
return hours.map((hourStart) => byHour.get(hourStart) ?? zeroHour(hourStart));
}, [isPast24Hours, merged.daily, merged.hourly, hours]);
// Newest first: the window can run 90 periods, so the interesting end
// belongs at the top of the table.
const breakdownPeriods = useMemo<readonly (DailyTotals | HourlyTotals)[]>(
() => (isPast24Hours ? merged.hourly : merged.daily).toReversed(),
[isPast24Hours, merged.daily, merged.hourly],
);
const selectWindow = (days: number) => {
setWindowSelection({
@@ -438,17 +435,6 @@ export function UsagePage() {
);
}
/** A zero-filled hourly period so the breakdown lists every hour in the window. */
function zeroHour(hourStart: string): HourlyTotals {
return {
day: "",
hourStart,
costUsd: 0,
totalTokens: 0,
byProvider: new Map<UsageProviderKind, { costUsd: number; totalTokens: number }>(),
};
}
/** Brand mark for the harness a row belongs to. */
function ProviderMark({
provider,