The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 194 lines 6.39 KB
Raw
1 import type { EnvironmentId, UsageLimitsReport } from "@t3tools/contracts";
2 import type { TimestampFormat } from "@t3tools/contracts/settings";
3 import { limitsNotice } from "@t3tools/shared/usageLimitsCopy";
4 import { msg, T, useGT, useMessages } from "gt-react";
5 import { GaugeIcon } from "lucide-react";
6
7 import { m, type MessageResolver } from "../../i18n/messages";
8 import { ensureLocalApi } from "../../localApi";
9 import { formatUpcomingTimestamp } from "../../timestampFormat";
10 import { Button } from "../ui/button";
11 import { providerClients } from "../settings/providerDriverMeta";
12 import { RedactedSensitiveText } from "../settings/RedactedSensitiveText";
13 import { LimitWindows, ResetCredits } from "../usage/UsageLimits";
14 import { ComposerBanner } from "./ComposerBanner";
15 import type { ComposerBannerStackItem } from "./ComposerBannerStack";
16
17 function accountLabel(account: UsageLimitsReport["accounts"][number]): string {
18 if (!account.instanceId) return account.label;
19 const driver = providerClients.get(account.driver)?.label ?? String(account.driver);
20 const instance =
21 account.displayName?.trim() ||
22 (String(account.instanceId) !== String(account.driver) ? account.instanceId : "");
23 return instance && instance.toLowerCase() !== driver.toLowerCase()
24 ? `${driver} · ${instance}`
25 : driver;
26 }
27
28 function AccountSummary({ account }: { readonly account: UsageLimitsReport["accounts"][number] }) {
29 const gt = useGT();
30 const label = accountLabel(account);
31 return (
32 <>
33 {label.includes("@") ? (
34 <RedactedSensitiveText
35 key={label}
36 value={label}
37 ariaLabel={gt("Toggle account label visibility")}
38 revealTooltip={gt("Click to reveal account")}
39 hideTooltip={gt("Click to hide account")}
40 className="max-w-full truncate align-bottom font-sans text-xs leading-normal"
41 />
42 ) : (
43 label
44 )}
45 {account.plan ? ` · ${account.plan}` : null}
46 </>
47 );
48 }
49
50 export function usageLimitsBannerItem(
51 id: string,
52 report: UsageLimitsReport,
53 environmentId: EnvironmentId,
54 onDismiss: () => void,
55 resolve: MessageResolver = m,
56 ): ComposerBannerStackItem {
57 const [first] = report.accounts;
58 const single = report.accounts.length === 1 && first ? first : null;
59 const summary = single ? (
60 <AccountSummary account={single} />
61 ) : (
62 resolve(
63 msg("{count, plural, one {{count} account} other {{count} accounts}}", {
64 count: report.accounts.length,
65 }),
66 )
67 );
68 return {
69 id,
70 variant: "info",
71 icon: <GaugeIcon />,
72 title: resolve(msg("Usage limits")),
73 description: summary,
74 dismissLabel: resolve(msg("Dismiss usage limits")),
75 onDismiss,
76 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
77 };
78 }
79
80 export function modelFallbackBannerItem(
81 {
82 fallbackModelName,
83 defaultProviderName,
84 defaultModelName,
85 resetsAt,
86 timestampFormat,
87 onUseDefault,
88 onDismiss,
89 }: {
90 readonly fallbackModelName: string;
91 readonly defaultProviderName: string;
92 readonly defaultModelName: string;
93 readonly resetsAt: string | null;
94 readonly timestampFormat: TimestampFormat;
95 readonly onUseDefault: () => void;
96 readonly onDismiss: () => void;
97 },
98 resolve: MessageResolver = m,
99 ): ComposerBannerStackItem {
100 return {
101 id: "fallback-model",
102 variant: "info",
103 icon: <GaugeIcon />,
104 title: resolve(msg("Using {model}", { model: fallbackModelName })),
105 description: resetsAt
106 ? resolve(
107 msg("{provider} is out of usage · resets {time}", {
108 provider: defaultProviderName,
109 time: formatUpcomingTimestamp(
110 resetsAt,
111 timestampFormat,
112 Date.now(),
113 undefined,
114 resolve,
115 ),
116 }),
117 )
118 : resolve(msg("{provider} is out of usage", { provider: defaultProviderName })),
119 actions: (
120 <Button size="xs" variant="ghost" onClick={onUseDefault}>
121 {resolve(msg("Use {model}", { model: defaultModelName }))}
122 </Button>
123 ),
124 dismissLabel: resolve(msg("Dismiss fallback model notice")),
125 onDismiss,
126 };
127 }
128
129 function UsageLimitsBannerBody({
130 report,
131 environmentId,
132 }: {
133 readonly report: UsageLimitsReport;
134 readonly environmentId: EnvironmentId;
135 }) {
136 const messages = useMessages();
137 const now = Date.parse(report.createdAt);
138 return (
139 <ComposerBanner.Scroll>
140 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
141 {report.accounts.map((account) => {
142 const resetCreditInput =
143 account.resetCreditInput ??
144 (account.instanceId ? { instanceId: account.instanceId } : undefined);
145 const notice = limitsNotice(account.limits, messages);
146 const externalUsage = account.limits.externalUsage;
147 return (
148 <div key={account.id} className="flex min-w-0 flex-col gap-1">
149 {report.accounts.length > 1 ? (
150 <span className="truncate text-xs text-muted-foreground">
151 <AccountSummary account={account} />
152 </span>
153 ) : null}
154 {notice ? (
155 <span className="text-xs text-muted-foreground">{notice}</span>
156 ) : (
157 <LimitWindows
158 compact
159 driver={account.driver}
160 accentColor={account.accentColor}
161 windows={account.limits.windows}
162 now={now}
163 />
164 )}
165 {externalUsage ? (
166 <Button
167 size="sm"
168 variant="link"
169 className="self-start"
170 onClick={() => void ensureLocalApi().shell.openExternal(externalUsage.url)}
171 >
172 <T>Manage usage</T>
173 </Button>
174 ) : null}
175 {resetCreditInput && account.limits.resetCredits ? (
176 <ResetCredits
177 environmentId={environmentId}
178 input={resetCreditInput}
179 credits={account.limits.resetCredits}
180 now={now}
181 />
182 ) : null}
183 </div>
184 );
185 })}
186 {report.notices.map((notice) => (
187 <span key={notice} className="text-xs text-muted-foreground">
188 {notice}
189 </span>
190 ))}
191 </ComposerBanner.Body>
192 </ComposerBanner.Scroll>
193 );
194 }