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

T3ConnectUserProfilePage.tsx

260 lines9.9 KB

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