Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerUsageLimits.tsx

124 lines4.3 KB

1import type { EnvironmentId, UsageLimitsReport } from "@t3tools/contracts";
2import { limitsNotice } from "@t3tools/shared/usageLimits";
3import { GaugeIcon } from "lucide-react";
4​
5import { getDriverOption } from "../settings/providerDriverMeta";
6import { RedactedSensitiveText } from "../settings/RedactedSensitiveText";
7import { LimitWindows, ResetCredits } from "../usage/UsageLimits";
8import { ComposerBanner } from "./ComposerBanner";
9import type { ComposerBannerStackItem } from "./ComposerBannerStack";
10​
11/** Driver name, then the instance when there could be more than one of that driver. */
12function accountLabel(account: UsageLimitsReport["accounts"][number]): string {
13 if (!account.instanceId) return account.label;
14 const driver = getDriverOption(account.driver)?.label ?? String(account.driver);
15 const instance =
16 account.displayName?.trim() ||
17 (String(account.instanceId) !== String(account.driver) ? account.instanceId : "");
18 // The default instance is often named after its driver; saying it twice adds nothing.
19 return instance && instance.toLowerCase() !== driver.toLowerCase()
20 ? `${driver} · ${instance}`
21 : driver;
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 );
44​
45/** The /usage-limits result as a composer notice: it stacks under warnings and dismisses like one. */
46export function usageLimitsBannerItem(
47 id: string,
48 report: UsageLimitsReport,
49 environmentId: EnvironmentId,
50 onDismiss: () => void,
51): ComposerBannerStackItem {
52 const [first] = report.accounts;
53 const single = report.accounts.length === 1 && first ? first : null;
54 const summary = single ? (
55 <AccountSummary account={single} />
56 ) : (
57 `${report.accounts.length} accounts`
58 );
59 return {
60 id,
61 variant: "info",
62 priority: "notice",
63 icon: <GaugeIcon />,
64 title: "Usage limits",
65 description: summary,
66 dismissLabel: "Dismiss usage limits",
67 onDismiss,
68 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
69 };
71​
72function UsageLimitsBannerBody({
73 report,
74 environmentId,
75}: {
76 readonly report: UsageLimitsReport;
77 readonly environmentId: EnvironmentId;
78}) {
79 const now = Date.parse(report.createdAt);
80 return (
81 <ComposerBanner.Scroll>
82 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
83 {report.accounts.map((account) => {
84 const resetCreditInput =
85 account.resetCreditInput ??
86 (account.instanceId ? { instanceId: account.instanceId } : undefined);
87 const notice = limitsNotice(account.limits);
88 return (
89 <div key={account.id} className="flex min-w-0 flex-col gap-1">
90 {report.accounts.length > 1 ? (
91 <span className="truncate text-xs text-muted-foreground">
92 <AccountSummary account={account} />
93 </span>
94 ) : null}
95 {notice ? (
96 <span className="text-xs text-muted-foreground">{notice}</span>
97 ) : (
98 <LimitWindows
99 compact
100 driver={account.driver}
101 windows={account.limits.windows}
102 now={now}
103 />
104 )}
105 {resetCreditInput && account.limits.resetCredits ? (
106 <ResetCredits
107 environmentId={environmentId}
108 input={resetCreditInput}
109 credits={account.limits.resetCredits}
110 now={now}
111 />
112 ) : null}
113 </div>
114 );
115 })}
116 {report.notices.map((notice) => (
117 <span key={notice} className="text-xs text-muted-foreground">
118 {notice}
119 </span>
120 ))}
121 </ComposerBanner.Body>
122 </ComposerBanner.Scroll>
123 );