Open History to see the last commit
1import type { EnvironmentId, UsageLimitsReport } from "@t3tools/contracts";
2import { limitsNotice } from "@t3tools/shared/usageLimits";
3import { GaugeIcon } from "lucide-react";
4
5import { ensureLocalApi } from "../../localApi";
6import { Button } from "../ui/button";
7import { providerClients } from "../settings/providerDriverMeta";
8import { RedactedSensitiveText } from "../settings/RedactedSensitiveText";
9import { LimitWindows, ResetCredits } from "../usage/UsageLimits";
10import { ComposerBanner } from "./ComposerBanner";
11import type { ComposerBannerStackItem } from "./ComposerBannerStack";
12
13function accountLabel(account: UsageLimitsReport["accounts"][number]): string {
14 if (!account.instanceId) return account.label;
15 const driver = providerClients.get(account.driver)?.label ?? String(account.driver);
16 const instance =
17 account.displayName?.trim() ||
18 (String(account.instanceId) !== String(account.driver) ? account.instanceId : "");
19 return instance && instance.toLowerCase() !== driver.toLowerCase()
20 ? `${driver} · ${instance}`
21 : driver;
22}
23
24function AccountSummary({ account }: { readonly account: UsageLimitsReport["accounts"][number] }) {
25 const label = accountLabel(account);
26 return (
27 <>
28 {label.includes("@") ? (
29 <RedactedSensitiveText
30 key={label}
31 value={label}
32 ariaLabel="Toggle account label visibility"
33 revealTooltip="Click to reveal account"
34 hideTooltip="Click to hide account"
35 className="max-w-full truncate align-bottom font-sans text-xs leading-normal"
36 />
37 ) : (
38 label
39 )}
40 {account.plan ? ` · ${account.plan}` : null}
41 </>
42 );
43}
44
45export function usageLimitsBannerItem(
46 id: string,
47 report: UsageLimitsReport,
48 environmentId: EnvironmentId,
49 onDismiss: () => void,
50): ComposerBannerStackItem {
51 const [first] = report.accounts;
52 const single = report.accounts.length === 1 && first ? first : null;
53 const summary = single ? (
54 <AccountSummary account={single} />
55 ) : (
56 `${report.accounts.length} accounts`
57 );
58 return {
59 id,
60 variant: "info",
61 icon: <GaugeIcon />,
62 title: "Usage limits",
63 description: summary,
64 dismissLabel: "Dismiss usage limits",
65 onDismiss,
66 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
67 };
68}
69
70function UsageLimitsBannerBody({
71 report,
72 environmentId,
73}: {
74 readonly report: UsageLimitsReport;
75 readonly environmentId: EnvironmentId;
76}) {
77 const now = Date.parse(report.createdAt);
78 return (
79 <ComposerBanner.Scroll>
80 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
81 {report.accounts.map((account) => {
82 const resetCreditInput =
83 account.resetCreditInput ??
84 (account.instanceId ? { instanceId: account.instanceId } : undefined);
85 const notice = limitsNotice(account.limits);
86 const externalUsage = account.limits.externalUsage;
87 return (
88 <div key={account.id} className="flex min-w-0 flex-col gap-1">
89 {report.accounts.length > 1 ? (
90 <span className="truncate text-xs text-muted-foreground">
91 <AccountSummary account={account} />
92 </span>
93 ) : null}
94 {notice ? (
95 <span className="text-xs text-muted-foreground">{notice}</span>
96 ) : (
97 <LimitWindows
98 compact
99 driver={account.driver}
100 windows={account.limits.windows}
101 now={now}
102 />
103 )}
104 {externalUsage ? (
105 <Button
106 size="sm"
107 variant="link"
108 className="self-start"
109 onClick={() => void ensureLocalApi().shell.openExternal(externalUsage.url)}
110 >
111 Manage usage
112 </Button>
113 ) : null}
114 {resetCreditInput && account.limits.resetCredits ? (
115 <ResetCredits
116 environmentId={environmentId}
117 input={resetCreditInput}
118 credits={account.limits.resetCredits}
119 now={now}
120 />
121 ) : null}
122 </div>
123 );
124 })}
125 {report.notices.map((notice) => (
126 <span key={notice} className="text-xs text-muted-foreground">
127 {notice}
128 </span>
129 ))}
130 </ComposerBanner.Body>
131 </ComposerBanner.Scroll>
132 );
133}