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

TunnelUsage.tsx

145 lines4.7 KB

1import { T3CHAT_PLAN_TUNNEL_LIMITS } from "@t3tools/contracts/relay";
2import { TriangleAlertIcon } from "lucide-react";
3import { msg, T, useGT } from "gt-react";
4import type { ReactNode } from "react";
5​
6import type { ManagedTunnelUpgrade } from "../../cloud/managedRelayState";
7import { m, type MessageResolver } from "../../i18n/messages";
8import { cn } from "../../lib/utils";
9import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert";
10import { Button } from "../ui/button";
11import { Skeleton } from "../ui/skeleton";
12​
13// Above this, one slot per tunnel is too thin to read, so the bar fills instead.
14const MAX_SEGMENTS = 10;
15​
16export const T3CHAT_SUBSCRIPTION_URL = "https://t3.chat/settings/subscription";
17​
18/** Components pass `useMessages()` so the copy follows a language switch. */
19export function tunnelUpgradeCopy(
20 upgrade: Exclude<ManagedTunnelUpgrade, null>,
21 resolve: MessageResolver = m,
22): string {
23 const { pro, premier } = T3CHAT_PLAN_TUNNEL_LIMITS;
24 switch (upgrade) {
25 case "link":
26 return resolve(
27 msg`Link T3 Chat Pro for up to ${{ pro }} tunnels, or Premier for up to ${{ premier }}.`,
28 );
29 case "pro":
30 return resolve(
31 msg`Upgrade to T3 Chat Pro for up to ${{ pro }} tunnels, or Premier for up to ${{ premier }}.`,
32 );
33 case "premier":
34 return resolve(msg`Upgrade to T3 Chat Premier for up to ${{ premier }} tunnels.`);
35 }
37​
38/** Holds the meter's height while usage loads, so the rows below it stay put. */
39export function TunnelUsageMeterSkeleton() {
40 return (
41 <div aria-hidden className="flex max-w-sm flex-col gap-1.5">
42 <div className="flex h-4 items-center">
43 <Skeleton shape="pill" className="h-3 w-32" />
44 </div>
45 <Skeleton shape="pill" className="h-1.5 w-full" />
46 </div>
47 );
49​
50/** "2 of 3 tunnels in use" with one bar slot per tunnel. */
51export function TunnelUsageMeter({
52 used,
53 limit,
54 action,
55 className,
56}: {
57 readonly used: number;
58 readonly limit: number;
59 readonly action?: ReactNode;
60 readonly className?: string;
61}) {
62 const gt = useGT();
63 const full = used >= limit;
64 const fill = full ? "bg-warning" : "bg-foreground/72";
65 return (
66 <div className={cn("flex max-w-sm flex-col gap-1.5", className)}>
67 <div className="flex items-baseline justify-between gap-3 text-xs">
68 <span className={cn("tabular-nums", full ? "text-warning-foreground" : "text-foreground")}>
69 {gt`${{ used }} of ${{ limit }} tunnels in use`}
70 </span>
71 {action}
72 </div>
73 <div
74 role="meter"
75 aria-label={gt`T3 Connect tunnels in use`}
76 aria-valuemin={0}
77 aria-valuemax={limit}
78 aria-valuenow={used}
79 className="flex h-1.5 gap-1"
80 >
81 {limit <= MAX_SEGMENTS ? (
82 Array.from({ length: limit }, (_, index) => (
83 <span
84 key={index}
85 className={cn("flex-1 rounded-full", index < used ? fill : "bg-muted")}
86 />
87 ))
88 ) : (
89 <span className="flex-1 overflow-hidden rounded-full bg-muted">
90 <span
91 className={cn("block h-full rounded-full", fill)}
92 style={{ width: `${Math.min(100, (used / limit) * 100)}%` }}
93 />
94 </span>
95 )}
96 </div>
97 </div>
98 );
100​
101/** Shown when the relay refuses a new tunnel because the account is at its cap. */
102export function TunnelLimitAlert({
103 upgrade,
104 onLinkT3Chat,
105 onManageTunnels,
106}: {
107 readonly upgrade: ManagedTunnelUpgrade;
108 readonly onLinkT3Chat: (() => void) | null;
109 readonly onManageTunnels: (() => void) | null;
110}) {
111 const gt = useGT();
112 const action =
113 upgrade === "link" && onLinkT3Chat ? (
114 <Button size="sm" onClick={onLinkT3Chat}>
115 <T>Link T3 Chat</T>
116 </Button>
117 ) : upgrade === "pro" || upgrade === "premier" ? (
118 <Button
119 size="sm"
120 render={<a href={T3CHAT_SUBSCRIPTION_URL} target="_blank" rel="noopener noreferrer" />}
121 >
122 <T>Upgrade on T3 Chat</T>
123 </Button>
124 ) : onManageTunnels ? (
125 <Button size="sm" variant="outline" onClick={onManageTunnels}>
126 <T>Manage tunnels</T>
127 </Button>
128 ) : null;
129 return (
130 <Alert variant="warning">
131 <TriangleAlertIcon />
132 <AlertTitle>
133 <T>You&apos;re out of tunnels (they cost us money).</T>
134 </AlertTitle>
135 <AlertDescription>
136 {upgrade === "link"
137 ? gt`Switch to Tailscale or get more tunnels by linking your T3 Chat sub (sorry!)`
138 : upgrade
139 ? gt`Switch to Tailscale or get more tunnels by upgrading your T3 Chat sub (sorry!)`
140 : gt`Switch to Tailscale or deregister an environment you no longer use (sorry!)`}
141 </AlertDescription>
142 {action ? <AlertAction>{action}</AlertAction> : null}
143 </Alert>
144 );