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 { findErrorTraceId, thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { |
| 3 | isAtomCommandInterrupted, |
| 4 | squashAtomCommandFailure, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 7 | import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay"; |
| 8 | import { ServerIcon } from "lucide-react"; |
| 9 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 10 | import { useRef, useState } from "react"; |
| 11 | |
| 12 | import { |
| 13 | deregisterManagedRelayEnvironmentCommand, |
| 14 | useManagedRelayEnvironments, |
| 15 | useManagedTunnelUsage, |
| 16 | } from "../../cloud/managedRelayState"; |
| 17 | import { relayFailureReason } from "../../cloud/relayFailureReason"; |
| 18 | import { relayEnvironmentDiscovery } from "../../state/relay"; |
| 19 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 20 | import { T3CHAT_SUBSCRIPTION_URL, TunnelUsageMeter, tunnelUpgradeCopy } from "../cloud/TunnelUsage"; |
| 21 | import { Button, InlineButton } from "../ui/button"; |
| 22 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
| 23 | import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty"; |
| 24 | import { toastManager } from "../ui/toast"; |
| 25 | import { stackedThreadToast } from "../ui/toastHelpers"; |
| 26 | import { |
| 27 | ClerkUserProfilePage, |
| 28 | ClerkUserProfileRefreshButton, |
| 29 | ClerkUserProfileRow, |
| 30 | } from "./ClerkUserProfilePage"; |
| 31 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 32 | import { useT3ChatLink } from "./useT3ChatLink"; |
| 33 | |
| 34 | const linkedAtFormatter = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }); |
| 35 | |
| 36 | function linkedAtLabel(value: string, resolve: MessageResolver): string { |
| 37 | const linkedAt = new Date(value); |
| 38 | return Number.isNaN(linkedAt.getTime()) |
| 39 | ? resolve(msg("Link date unavailable")) |
| 40 | : resolve(msg("Linked {date}", { date: linkedAtFormatter.format(linkedAt) })); |
| 41 | } |
| 42 | |
| 43 | function endpointLabel( |
| 44 | environment: RelayClientEnvironmentRecord, |
| 45 | resolve: MessageResolver, |
| 46 | ): string { |
| 47 | return environment.endpoint.providerKind === "cloudflare_tunnel" |
| 48 | ? resolve(msg("Managed tunnel")) |
| 49 | : resolve(msg("Activity publishing only")); |
| 50 | } |
| 51 | |
| 52 | function T3ConnectEnvironmentRow(props: { |
| 53 | readonly environment: RelayClientEnvironmentRecord; |
| 54 | readonly confirmationOpen: boolean; |
| 55 | readonly mutationPending: boolean; |
| 56 | readonly onConfirmationChange: (open: boolean) => void; |
| 57 | readonly onDeregister: (environment: RelayClientEnvironmentRecord) => void; |
| 58 | }) { |
| 59 | const { environment } = props; |
| 60 | const gt = useGT(); |
| 61 | const m = useMessages(); |
| 62 | return ( |
| 63 | <ClerkUserProfileRow icon={<ServerIcon className="size-4" />}> |
| 64 | <Collapsible open={props.confirmationOpen} onOpenChange={props.onConfirmationChange}> |
| 65 | <div className="flex items-start gap-4"> |
| 66 | <div className="min-w-0 flex-1"> |
| 67 | <h3 className="truncate text-sm leading-4.5 font-medium text-foreground"> |
| 68 | {environment.label} |
| 69 | </h3> |
| 70 | <p className="mt-1 text-xs leading-4.5 text-muted-foreground"> |
| 71 | {linkedAtLabel(environment.linkedAt, m)} · {endpointLabel(environment, m)} |
| 72 | </p> |
| 73 | </div> |
| 74 | <CollapsibleTrigger |
| 75 | render={ |
| 76 | <Button size="sm" variant="destructive-outline" disabled={props.mutationPending}> |
| 77 | <T>Deregister</T> |
| 78 | </Button> |
| 79 | } |
| 80 | /> |
| 81 | </div> |
| 82 | |
| 83 | <CollapsiblePanel> |
| 84 | <div className="pt-3"> |
| 85 | <div |
| 86 | className="rounded-lg border border-input bg-muted/32 px-5 py-4 shadow-xs/5" |
| 87 | role="group" |
| 88 | aria-label={gt("Confirm deregistration of {environment}", { |
| 89 | environment: environment.label, |
| 90 | })} |
| 91 | > |
| 92 | <h4 className="text-sm leading-4.5 font-semibold text-foreground"> |
| 93 | <T>Deregister server</T> |
| 94 | </h4> |
| 95 | <p className="mt-1 text-xs leading-4.5 text-muted-foreground"> |
| 96 | <T> |
| 97 | “<Var>{environment.label}</Var>” will be removed from this account. |
| 98 | </T> |
| 99 | </p> |
| 100 | <p className="mt-4 max-w-xl text-xs leading-4.5 text-muted-foreground"> |
| 101 | <T> |
| 102 | T3 Connect access will be revoked, any managed tunnel will be removed, and a host |
| 103 | space will become available. Local connections on your devices are not changed. |
| 104 | </T> |
| 105 | </p> |
| 106 | <div className="mt-4 flex justify-end gap-2"> |
| 107 | <Button |
| 108 | size="sm" |
| 109 | variant="ghost" |
| 110 | disabled={props.mutationPending} |
| 111 | onClick={() => props.onConfirmationChange(false)} |
| 112 | > |
| 113 | <T>Cancel</T> |
| 114 | </Button> |
| 115 | <Button |
| 116 | size="sm" |
| 117 | variant="destructive" |
| 118 | disabled={props.mutationPending} |
| 119 | onClick={() => props.onDeregister(environment)} |
| 120 | > |
| 121 | {props.mutationPending ? gt("Deregistering…") : gt("Deregister")} |
| 122 | </Button> |
| 123 | </div> |
| 124 | </div> |
| 125 | </div> |
| 126 | </CollapsiblePanel> |
| 127 | </Collapsible> |
| 128 | </ClerkUserProfileRow> |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | function T3ConnectTunnelUsage() { |
| 133 | const usage = useManagedTunnelUsage(); |
| 134 | const t3Chat = useT3ChatLink(); |
| 135 | const gt = useGT(); |
| 136 | const m = useMessages(); |
| 137 | if (usage.used === null || usage.limit === null) return null; |
| 138 | return ( |
| 139 | <div className="border-t py-4"> |
| 140 | <TunnelUsageMeter used={usage.used} limit={usage.limit} /> |
| 141 | {usage.upgrade ? ( |
| 142 | <p className="mt-2 text-xs leading-4.5 text-muted-foreground"> |
| 143 | {usage.upgrade === "link" && t3Chat.linkUrl ? ( |
| 144 | <T> |
| 145 | Confirm on{" "} |
| 146 | <a |
| 147 | className="underline" |
| 148 | href={t3Chat.linkUrl} |
| 149 | target="_blank" |
| 150 | rel="noopener noreferrer" |
| 151 | > |
| 152 | t3.chat |
| 153 | </a> |
| 154 | , then select Refresh. |
| 155 | </T> |
| 156 | ) : ( |
| 157 | <> |
| 158 | {tunnelUpgradeCopy(usage.upgrade, m)}{" "} |
| 159 | {usage.upgrade === "link" ? ( |
| 160 | <InlineButton disabled={t3Chat.pending !== null} onClick={() => void t3Chat.link()}> |
| 161 | {t3Chat.pending === "link" ? gt("Opening…") : gt("Link T3 Chat")} |
| 162 | </InlineButton> |
| 163 | ) : ( |
| 164 | <a |
| 165 | className="underline" |
| 166 | href={T3CHAT_SUBSCRIPTION_URL} |
| 167 | target="_blank" |
| 168 | rel="noopener noreferrer" |
| 169 | > |
| 170 | <T>Upgrade on T3 Chat</T> |
| 171 | </a> |
| 172 | )} |
| 173 | </> |
| 174 | )} |
| 175 | </p> |
| 176 | ) : null} |
| 177 | </div> |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | export function T3ConnectUserProfilePage() { |
| 182 | const environmentsState = useManagedRelayEnvironments(); |
| 183 | const gt = useGT(); |
| 184 | const deregisterEnvironment = useAtomCommand(deregisterManagedRelayEnvironmentCommand, { |
| 185 | reportFailure: false, |
| 186 | }); |
| 187 | const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refreshAfterMutation, { |
| 188 | reportFailure: false, |
| 189 | }); |
| 190 | const [deregisteringEnvironmentId, setDeregisteringEnvironmentId] = |
| 191 | useState<EnvironmentId | null>(null); |
| 192 | const [confirmingEnvironmentId, setConfirmingEnvironmentId] = useState<EnvironmentId | null>( |
| 193 | null, |
| 194 | ); |
| 195 | const mutationPendingRef = useRef(false); |
| 196 | const [removedEnvironments, setRemovedEnvironments] = useState<{ |
| 197 | readonly accountId: string | null; |
| 198 | readonly linkedAtById: ReadonlyMap<EnvironmentId, string>; |
| 199 | }>({ accountId: null, linkedAtById: new Map() }); |
| 200 | |
| 201 | const handleDeregister = async (environment: RelayClientEnvironmentRecord) => { |
| 202 | const accountId = environmentsState.accountId; |
| 203 | if (!accountId || mutationPendingRef.current) return; |
| 204 | |
| 205 | mutationPendingRef.current = true; |
| 206 | setDeregisteringEnvironmentId(environment.environmentId); |
| 207 | const result = await deregisterEnvironment({ |
| 208 | accountId, |
| 209 | environmentId: environment.environmentId, |
| 210 | }); |
| 211 | mutationPendingRef.current = false; |
| 212 | setDeregisteringEnvironmentId(null); |
| 213 | setConfirmingEnvironmentId(null); |
| 214 | |
| 215 | if (result._tag === "Success") { |
| 216 | setRemovedEnvironments((current) => { |
| 217 | const linkedAtById = new Map(current.accountId === accountId ? current.linkedAtById : []); |
| 218 | linkedAtById.set(environment.environmentId, environment.linkedAt); |
| 219 | return { accountId, linkedAtById }; |
| 220 | }); |
| 221 | environmentsState.refresh(); |
| 222 | void refreshRelayEnvironments(); |
| 223 | toastManager.add({ |
| 224 | type: "success", |
| 225 | title: gt("Server deregistered"), |
| 226 | description: gt("T3 Connect access was revoked and a host space is now available."), |
| 227 | }); |
| 228 | return; |
| 229 | } |
| 230 | if (isAtomCommandInterrupted(result)) return; |
| 231 | |
| 232 | // The relay may have removed it before failing. |
| 233 | environmentsState.refresh(); |
| 234 | const cause = squashAtomCommandFailure(result); |
| 235 | const message = thrownErrorMessage(cause, m) ?? gt("Could not deregister the server."); |
| 236 | const traceId = findErrorTraceId(cause); |
| 237 | console.error("[t3-connect] Could not deregister environment", { |
| 238 | environmentId: environment.environmentId, |
| 239 | message, |
| 240 | traceId, |
| 241 | cause, |
| 242 | }); |
| 243 | toastManager.add( |
| 244 | stackedThreadToast({ |
| 245 | type: "error", |
| 246 | title: gt("Could not deregister server"), |
| 247 | description: relayFailureReason(cause) ?? message, |
| 248 | ...(traceId |
| 249 | ? { |
| 250 | actionProps: { |
| 251 | children: gt("Copy trace ID"), |
| 252 | onClick: () => void navigator.clipboard?.writeText(traceId), |
| 253 | }, |
| 254 | actionVariant: "outline", |
| 255 | } |
| 256 | : {}), |
| 257 | data: { hideCopyButton: true }, |
| 258 | }), |
| 259 | ); |
| 260 | }; |
| 261 | |
| 262 | const removedEnvironmentLinkedAt = |
| 263 | removedEnvironments.accountId === environmentsState.accountId |
| 264 | ? removedEnvironments.linkedAtById |
| 265 | : new Map<EnvironmentId, string>(); |
| 266 | const environments = (environmentsState.data ?? []).filter( |
| 267 | (environment) => |
| 268 | removedEnvironmentLinkedAt.get(environment.environmentId) !== environment.linkedAt, |
| 269 | ); |
| 270 | const usage = useManagedTunnelUsage(); |
| 271 | const refreshAll = usage.refresh; |
| 272 | const isInitialLoad = |
| 273 | !environmentsState.accountId || (environmentsState.data === null && !environmentsState.error); |
| 274 | |
| 275 | return ( |
| 276 | <ClerkUserProfilePage |
| 277 | title="T3 Connect" |
| 278 | description={gt( |
| 279 | "Environments registered to your account. Connections on this device are managed in Settings.", |
| 280 | )} |
| 281 | action={ |
| 282 | <ClerkUserProfileRefreshButton |
| 283 | disabled={deregisteringEnvironmentId !== null} |
| 284 | isPending={environmentsState.isPending} |
| 285 | onClick={refreshAll} |
| 286 | /> |
| 287 | } |
| 288 | > |
| 289 | <div> |
| 290 | <T3ConnectTunnelUsage /> |
| 291 | {environmentsState.error ? ( |
| 292 | <div className="mb-4 border-t border-destructive/35 py-3 text-xs" role="alert"> |
| 293 | <p className="font-medium text-destructive-foreground"> |
| 294 | <T>Could not load T3 Connect environments</T> |
| 295 | </p> |
| 296 | <p className="mt-1 text-xs text-muted-foreground">{environmentsState.error}</p> |
| 297 | </div> |
| 298 | ) : null} |
| 299 | |
| 300 | {isInitialLoad ? ( |
| 301 | <p className="border-t py-4 text-xs text-muted-foreground" role="status"> |
| 302 | <T>Loading environments…</T> |
| 303 | </p> |
| 304 | ) : environments.length > 0 ? ( |
| 305 | <ul className="border-t"> |
| 306 | {environments.map((environment) => ( |
| 307 | <T3ConnectEnvironmentRow |
| 308 | key={environment.environmentId} |
| 309 | environment={environment} |
| 310 | confirmationOpen={confirmingEnvironmentId === environment.environmentId} |
| 311 | mutationPending={deregisteringEnvironmentId !== null} |
| 312 | onConfirmationChange={(open) => |
| 313 | setConfirmingEnvironmentId(open ? environment.environmentId : null) |
| 314 | } |
| 315 | onDeregister={(selected) => void handleDeregister(selected)} |
| 316 | /> |
| 317 | ))} |
| 318 | </ul> |
| 319 | ) : environmentsState.error ? null : ( |
| 320 | <div className="border-t"> |
| 321 | <Empty size="compact"> |
| 322 | <EmptyMedia variant="icon"> |
| 323 | <ServerIcon /> |
| 324 | </EmptyMedia> |
| 325 | <EmptyHeader> |
| 326 | <EmptyTitle> |
| 327 | <T>No T3 Connect environments</T> |
| 328 | </EmptyTitle> |
| 329 | <EmptyDescription> |
| 330 | <T> |
| 331 | Link an environment from its local Settings to make it available through T3 |
| 332 | Connect. |
| 333 | </T> |
| 334 | </EmptyDescription> |
| 335 | </EmptyHeader> |
| 336 | </Empty> |
| 337 | </div> |
| 338 | )} |
| 339 | </div> |
| 340 | </ClerkUserProfilePage> |
| 341 | ); |
| 342 | } |