| 1 | import { findErrorTraceId } 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 { useRef, useState } from "react"; |
| 10 | |
| 11 | import { |
| 12 | deregisterManagedRelayEnvironmentCommand, |
| 13 | useManagedRelayEnvironments, |
| 14 | } from "../../cloud/managedRelayState"; |
| 15 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 16 | import { Button } from "../ui/button"; |
| 17 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
| 18 | import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty"; |
| 19 | import { toastManager } from "../ui/toast"; |
| 20 | import { |
| 21 | ClerkUserProfilePage, |
| 22 | ClerkUserProfileRefreshButton, |
| 23 | ClerkUserProfileRow, |
| 24 | } from "./ClerkUserProfilePage"; |
| 25 | |
| 26 | const linkedAtFormatter = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }); |
| 27 | |
| 28 | function linkedAtLabel(value: string): string { |
| 29 | const linkedAt = new Date(value); |
| 30 | return Number.isNaN(linkedAt.getTime()) |
| 31 | ? "Link date unavailable" |
| 32 | : `Linked ${linkedAtFormatter.format(linkedAt)}`; |
| 33 | } |
| 34 | |
| 35 | function endpointLabel(environment: RelayClientEnvironmentRecord): string { |
| 36 | return environment.endpoint.providerKind === "cloudflare_tunnel" |
| 37 | ? "Managed tunnel" |
| 38 | : "Activity publishing only"; |
| 39 | } |
| 40 | |
| 41 | export function T3ConnectEnvironmentRow(props: { |
| 42 | readonly environment: RelayClientEnvironmentRecord; |
| 43 | readonly confirmationOpen: boolean; |
| 44 | readonly mutationPending: boolean; |
| 45 | readonly onConfirmationChange: (open: boolean) => void; |
| 46 | readonly onDeregister: (environment: RelayClientEnvironmentRecord) => void; |
| 47 | }) { |
| 48 | const { environment } = props; |
| 49 | return ( |
| 50 | <ClerkUserProfileRow icon={<ServerIcon className="size-4" />}> |
| 51 | <Collapsible open={props.confirmationOpen} onOpenChange={props.onConfirmationChange}> |
| 52 | <div className="flex items-start gap-4"> |
| 53 | <div className="min-w-0 flex-1"> |
| 54 | <h3 className="truncate text-sm leading-4.5 font-medium text-foreground"> |
| 55 | {environment.label} |
| 56 | </h3> |
| 57 | <p className="mt-1 text-xs leading-4.5 text-muted-foreground"> |
| 58 | {linkedAtLabel(environment.linkedAt)} · {endpointLabel(environment)} |
| 59 | </p> |
| 60 | </div> |
| 61 | <CollapsibleTrigger |
| 62 | render={ |
| 63 | <Button size="sm" variant="destructive-outline" disabled={props.mutationPending}> |
| 64 | Deregister |
| 65 | </Button> |
| 66 | } |
| 67 | /> |
| 68 | </div> |
| 69 | |
| 70 | <CollapsiblePanel> |
| 71 | <div className="pt-3"> |
| 72 | <div |
| 73 | className="rounded-lg border border-input bg-muted/32 px-5 py-4 shadow-xs/5" |
| 74 | role="group" |
| 75 | aria-label={`Confirm deregistration of ${environment.label}`} |
| 76 | > |
| 77 | <h4 className="text-sm leading-4.5 font-semibold text-foreground"> |
| 78 | Deregister server |
| 79 | </h4> |
| 80 | <p className="mt-1 text-xs leading-4.5 text-muted-foreground"> |
| 81 | “{environment.label}” will be removed from this account. |
| 82 | </p> |
| 83 | <p className="mt-4 max-w-xl text-xs leading-4.5 text-muted-foreground"> |
| 84 | T3 Connect access will be revoked, any managed tunnel will be removed, and a host |
| 85 | space will become available. Local connections on your devices are not changed. |
| 86 | </p> |
| 87 | <div className="mt-4 flex justify-end gap-2"> |
| 88 | <Button |
| 89 | size="sm" |
| 90 | variant="ghost" |
| 91 | disabled={props.mutationPending} |
| 92 | onClick={() => props.onConfirmationChange(false)} |
| 93 | > |
| 94 | Cancel |
| 95 | </Button> |
| 96 | <Button |
| 97 | size="sm" |
| 98 | variant="destructive" |
| 99 | disabled={props.mutationPending} |
| 100 | onClick={() => props.onDeregister(environment)} |
| 101 | > |
| 102 | {props.mutationPending ? "Deregistering…" : "Deregister"} |
| 103 | </Button> |
| 104 | </div> |
| 105 | </div> |
| 106 | </div> |
| 107 | </CollapsiblePanel> |
| 108 | </Collapsible> |
| 109 | </ClerkUserProfileRow> |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | export function T3ConnectUserProfilePage() { |
| 114 | const environmentsState = useManagedRelayEnvironments(); |
| 115 | const deregisterEnvironment = useAtomCommand(deregisterManagedRelayEnvironmentCommand, { |
| 116 | reportFailure: false, |
| 117 | }); |
| 118 | const [deregisteringEnvironmentId, setDeregisteringEnvironmentId] = |
| 119 | useState<EnvironmentId | null>(null); |
| 120 | const [confirmingEnvironmentId, setConfirmingEnvironmentId] = useState<EnvironmentId | null>( |
| 121 | null, |
| 122 | ); |
| 123 | const mutationPendingRef = useRef(false); |
| 124 | const [removedEnvironments, setRemovedEnvironments] = useState<{ |
| 125 | readonly accountId: string | null; |
| 126 | readonly linkedAtById: ReadonlyMap<EnvironmentId, string>; |
| 127 | }>({ accountId: null, linkedAtById: new Map() }); |
| 128 | |
| 129 | const handleDeregister = async (environment: RelayClientEnvironmentRecord) => { |
| 130 | const accountId = environmentsState.accountId; |
| 131 | if (!accountId || mutationPendingRef.current) return; |
| 132 | |
| 133 | mutationPendingRef.current = true; |
| 134 | setDeregisteringEnvironmentId(environment.environmentId); |
| 135 | const result = await deregisterEnvironment({ |
| 136 | accountId, |
| 137 | environmentId: environment.environmentId, |
| 138 | }); |
| 139 | mutationPendingRef.current = false; |
| 140 | setDeregisteringEnvironmentId(null); |
| 141 | |
| 142 | if (result._tag === "Success") { |
| 143 | setConfirmingEnvironmentId(null); |
| 144 | setRemovedEnvironments((current) => { |
| 145 | const linkedAtById = new Map(current.accountId === accountId ? current.linkedAtById : []); |
| 146 | linkedAtById.set(environment.environmentId, environment.linkedAt); |
| 147 | return { accountId, linkedAtById }; |
| 148 | }); |
| 149 | environmentsState.refresh(); |
| 150 | toastManager.add({ |
| 151 | type: "success", |
| 152 | title: "Server deregistered", |
| 153 | description: "T3 Connect access was revoked and a host space is now available.", |
| 154 | }); |
| 155 | return; |
| 156 | } |
| 157 | if (isAtomCommandInterrupted(result)) return; |
| 158 | |
| 159 | const cause = squashAtomCommandFailure(result); |
| 160 | const message = cause instanceof Error ? cause.message : "Could not deregister the server."; |
| 161 | const traceId = findErrorTraceId(cause); |
| 162 | console.error("[t3-connect] Could not deregister environment", { |
| 163 | environmentId: environment.environmentId, |
| 164 | message, |
| 165 | traceId, |
| 166 | cause, |
| 167 | }); |
| 168 | toastManager.add({ |
| 169 | type: "error", |
| 170 | title: "Could not deregister server", |
| 171 | description: message, |
| 172 | data: traceId |
| 173 | ? { |
| 174 | secondaryActionProps: { |
| 175 | children: "Copy trace ID", |
| 176 | onClick: () => void navigator.clipboard?.writeText(traceId), |
| 177 | }, |
| 178 | } |
| 179 | : undefined, |
| 180 | }); |
| 181 | }; |
| 182 | |
| 183 | const removedEnvironmentLinkedAt = |
| 184 | removedEnvironments.accountId === environmentsState.accountId |
| 185 | ? removedEnvironments.linkedAtById |
| 186 | : new Map<EnvironmentId, string>(); |
| 187 | const environments = (environmentsState.data ?? []).filter( |
| 188 | (environment) => |
| 189 | removedEnvironmentLinkedAt.get(environment.environmentId) !== environment.linkedAt, |
| 190 | ); |
| 191 | const isInitialLoad = |
| 192 | !environmentsState.accountId || (environmentsState.data === null && !environmentsState.error); |
| 193 | |
| 194 | return ( |
| 195 | <ClerkUserProfilePage |
| 196 | title="T3 Connect" |
| 197 | description="Environments registered to your account. Connections on this device are managed in Settings." |
| 198 | action={ |
| 199 | <ClerkUserProfileRefreshButton |
| 200 | disabled={deregisteringEnvironmentId !== null} |
| 201 | isPending={environmentsState.isPending} |
| 202 | onClick={environmentsState.refresh} |
| 203 | /> |
| 204 | } |
| 205 | > |
| 206 | <div> |
| 207 | {environmentsState.error ? ( |
| 208 | <div className="mb-4 border-t border-destructive/35 py-3 text-xs" role="alert"> |
| 209 | <p className="font-medium text-destructive-foreground"> |
| 210 | Could not load T3 Connect environments |
| 211 | </p> |
| 212 | <p className="mt-1 text-xs text-muted-foreground">{environmentsState.error}</p> |
| 213 | </div> |
| 214 | ) : null} |
| 215 | |
| 216 | {isInitialLoad ? ( |
| 217 | <p className="border-t py-4 text-xs text-muted-foreground" role="status"> |
| 218 | Loading environments… |
| 219 | </p> |
| 220 | ) : environments.length > 0 ? ( |
| 221 | <ul className="border-t"> |
| 222 | {environments.map((environment) => ( |
| 223 | <T3ConnectEnvironmentRow |
| 224 | key={environment.environmentId} |
| 225 | environment={environment} |
| 226 | confirmationOpen={confirmingEnvironmentId === environment.environmentId} |
| 227 | mutationPending={deregisteringEnvironmentId !== null} |
| 228 | onConfirmationChange={(open) => |
| 229 | setConfirmingEnvironmentId(open ? environment.environmentId : null) |
| 230 | } |
| 231 | onDeregister={(selected) => void handleDeregister(selected)} |
| 232 | /> |
| 233 | ))} |
| 234 | </ul> |
| 235 | ) : environmentsState.error ? null : ( |
| 236 | <div className="border-t"> |
| 237 | <Empty size="compact"> |
| 238 | <EmptyMedia variant="icon"> |
| 239 | <ServerIcon /> |
| 240 | </EmptyMedia> |
| 241 | <EmptyHeader> |
| 242 | <EmptyTitle>No T3 Connect environments</EmptyTitle> |
| 243 | <EmptyDescription> |
| 244 | Link an environment from its local Settings to make it available through T3 |
| 245 | Connect. |
| 246 | </EmptyDescription> |
| 247 | </EmptyHeader> |
| 248 | </Empty> |
| 249 | </div> |
| 250 | )} |
| 251 | </div> |
| 252 | </ClerkUserProfilePage> |
| 253 | ); |
| 254 | } |