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

ComposerUsageLimits.tsx

100 lines3.6 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 { LimitWindows, ResetCredits } from "../usage/UsageLimits";
7import { ComposerBanner } from "./ComposerBanner";
8import type { ComposerBannerStackItem } from "./ComposerBannerStack";
9​
10/** Driver name, then the instance when there could be more than one of that driver. */
11function accountLabel(account: UsageLimitsReport["accounts"][number]): string {
12 if (!account.instanceId) return account.label;
13 const driver = getDriverOption(account.driver)?.label ?? String(account.driver);
14 const instance =
15 account.displayName?.trim() ||
16 (String(account.instanceId) !== String(account.driver) ? account.instanceId : "");
17 // The default instance is often named after its driver; saying it twice adds nothing.
18 return instance && instance.toLowerCase() !== driver.toLowerCase()
19 ? `${driver} · ${instance}`
20 : driver;
22​
23/** The /usage-limits result as a composer notice: it stacks under warnings and dismisses like one. */
24export function usageLimitsBannerItem(
25 id: string,
26 report: UsageLimitsReport,
27 environmentId: EnvironmentId,
28 onDismiss: () => void,
29): ComposerBannerStackItem {
30 const [first] = report.accounts;
31 const single = report.accounts.length === 1 && first ? first : null;
32 const summary = single
33 ? [accountLabel(single), single.plan].filter(Boolean).join(" · ")
34 : `${report.accounts.length} accounts`;
35 return {
36 id,
37 variant: "info",
38 priority: "notice",
39 icon: <GaugeIcon />,
40 title: "Usage limits",
41 description: summary,
42 dismissLabel: "Dismiss usage limits",
43 onDismiss,
44 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
45 };
47​
48function UsageLimitsBannerBody({
49 report,
50 environmentId,
51}: {
52 readonly report: UsageLimitsReport;
53 readonly environmentId: EnvironmentId;
54}) {
55 const now = Date.parse(report.createdAt);
56 return (
57 <ComposerBanner.Scroll>
58 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
59 {report.accounts.map((account) => {
60 const resetCreditInput =
61 account.resetCreditInput ??
62 (account.instanceId ? { instanceId: account.instanceId } : undefined);
63 const notice = limitsNotice(account.limits);
64 return (
65 <div key={account.id} className="flex min-w-0 flex-col gap-1">
66 {report.accounts.length > 1 ? (
67 <span className="truncate text-xs text-muted-foreground">
68 {[accountLabel(account), account.plan].filter(Boolean).join(" · ")}
69 </span>
70 ) : null}
71 {notice ? (
72 <span className="text-xs text-muted-foreground">{notice}</span>
73 ) : (
74 <LimitWindows
75 compact
76 driver={account.driver}
77 windows={account.limits.windows}
78 now={now}
79 />
80 )}
81 {resetCreditInput && account.limits.resetCredits ? (
82 <ResetCredits
83 environmentId={environmentId}
84 input={resetCreditInput}
85 credits={account.limits.resetCredits}
86 now={now}
87 />
88 ) : null}
89 </div>
90 );
91 })}
92 {report.notices.map((notice) => (
93 <span key={notice} className="text-xs text-muted-foreground">
94 {notice}
95 </span>
96 ))}
97 </ComposerBanner.Body>
98 </ComposerBanner.Scroll>
99 );