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