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

ComposerUsageLimits.tsx

134 lines4.5 KB

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;
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​
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 priority: "notice",
62 icon: <GaugeIcon />,
63 title: "Usage limits",
64 description: summary,
65 dismissLabel: "Dismiss usage limits",
66 onDismiss,
67 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
68 };
70​
71function UsageLimitsBannerBody({
72 report,
73 environmentId,
74}: {
75 readonly report: UsageLimitsReport;
76 readonly environmentId: EnvironmentId;
77}) {
78 const now = Date.parse(report.createdAt);
79 return (
80 <ComposerBanner.Scroll>
81 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
82 {report.accounts.map((account) => {
83 const resetCreditInput =
84 account.resetCreditInput ??
85 (account.instanceId ? { instanceId: account.instanceId } : undefined);
86 const notice = limitsNotice(account.limits);
87 const externalUsage = account.limits.externalUsage;
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 {externalUsage ? (
106 <Button
107 size="sm"
108 variant="link"
109 className="self-start"
110 onClick={() => void ensureLocalApi().shell.openExternal(externalUsage.url)}
111 >
112 Manage usage
113 </Button>
114 ) : null}
115 {resetCreditInput && account.limits.resetCredits ? (
116 <ResetCredits
117 environmentId={environmentId}
118 input={resetCreditInput}
119 credits={account.limits.resetCredits}
120 now={now}
121 />
122 ) : null}
123 </div>
124 );
125 })}
126 {report.notices.map((notice) => (
127 <span key={notice} className="text-xs text-muted-foreground">
128 {notice}
129 </span>
130 ))}
131 </ComposerBanner.Body>
132 </ComposerBanner.Scroll>
133 );