|
3 | 3 |
|
4 | 4 | import { useT } from "../../i18n"; |
5 | 5 | import { fromNow } from "../../lib/format"; |
6 | | -import type { UsageWindow } from "../../lib/protocol.gen"; |
| 6 | +import type { AgentUsage, UsageWindow } from "../../lib/protocol.gen"; |
7 | 7 | import { useApp } from "../../store/context"; |
8 | 8 | import { Callout } from "../../ui/Callout"; |
9 | 9 | import { Dialog } from "../../ui/Dialog"; |
10 | 10 | import { TokensByBot } from "./TokensByBot"; |
11 | 11 |
|
12 | 12 | export function UsageDialog({ onClose }: { onClose(): void }) { |
13 | | - const u = useT().account.usage; |
14 | | - const usage = useApp((state) => state.system?.usage ?? null); |
15 | | - const limited = |
16 | | - usage !== null && usage.status !== "allowed" && usage.status !== "allowed_warning"; |
| 13 | + const t = useT(); |
| 14 | + const u = t.account.usage; |
| 15 | + const claude = useApp((state) => state.system?.usage ?? null); |
| 16 | + const others = useApp((state) => state.system?.agentUsage ?? NO_USAGE); |
| 17 | + const names = t.bots.dialog.agent.names; |
| 18 | + const named = others.length > 0; |
| 19 | + const shown = [ |
| 20 | + ...(claude === null ? [] : [{ agent: "claude" as const, plan: null, usage: claude }]), |
| 21 | + ...others, |
| 22 | + ].filter((entry) => entry.usage.windows.length > 0); |
17 | 23 |
|
18 | 24 | return ( |
19 | 25 | <Dialog title={u.title} onClose={onClose}> |
20 | 26 | <div className="flex flex-col gap-4 text-sm"> |
21 | 27 | <p className="text-ink-soft leading-relaxed">{u.intro}</p> |
22 | | - {usage === null || usage.windows.length === 0 ? ( |
| 28 | + {shown.length === 0 ? ( |
23 | 29 | <p className="text-muted">{u.empty}</p> |
24 | 30 | ) : ( |
25 | | - <> |
26 | | - {limited && <Callout tone="warn" title={u.limited} />} |
27 | | - <ul className="flex flex-col gap-4"> |
28 | | - {usage.windows.map((window) => ( |
29 | | - <Window key={window.name} window={window} /> |
30 | | - ))} |
31 | | - </ul> |
32 | | - <p className="text-muted text-xs">{u.updated(fromNow(usage.observedAt))}</p> |
33 | | - </> |
| 31 | + shown.map((entry) => { |
| 32 | + const limited = |
| 33 | + entry.usage.status !== "allowed" && entry.usage.status !== "allowed_warning"; |
| 34 | + return ( |
| 35 | + <section key={entry.agent} className="flex flex-col gap-4"> |
| 36 | + {named && ( |
| 37 | + <h3 className="font-medium"> |
| 38 | + {names[entry.agent]} |
| 39 | + {entry.plan !== null && ( |
| 40 | + <span className="ml-2 font-normal text-muted text-xs"> |
| 41 | + {u.plan(entry.plan)} |
| 42 | + </span> |
| 43 | + )} |
| 44 | + </h3> |
| 45 | + )} |
| 46 | + {limited && ( |
| 47 | + <Callout |
| 48 | + tone="warn" |
| 49 | + title={ |
| 50 | + entry.agent === "claude" ? u.limited : u.limitedAgent(names[entry.agent]) |
| 51 | + } |
| 52 | + /> |
| 53 | + )} |
| 54 | + <ul className="flex flex-col gap-4"> |
| 55 | + {entry.usage.windows.map((window) => ( |
| 56 | + <Window key={window.name} window={window} /> |
| 57 | + ))} |
| 58 | + </ul> |
| 59 | + <p className="text-muted text-xs">{u.updated(fromNow(entry.usage.observedAt))}</p> |
| 60 | + </section> |
| 61 | + ); |
| 62 | + }) |
34 | 63 | )} |
35 | 64 | <TokensByBot /> |
36 | 65 | </div> |
37 | 66 | </Dialog> |
38 | 67 | ); |
39 | 68 | } |
40 | 69 |
|
| 70 | +const NO_USAGE: AgentUsage[] = []; |
| 71 | + |
41 | 72 | function Window({ window }: { window: UsageWindow }) { |
42 | 73 | const u = useT().account.usage; |
43 | 74 | const percent = Math.round(Math.min(1, Math.max(0, window.utilization)) * 100); |
|
0 commit comments