Skip to content

Commit ebd1265

Browse files
BrechardrodrigoB-CBclaudemaria-rcks
authored
fix(connect): the remove dialog says whether a T3 Connect environment is still on the account (#21706)
Co-authored-by: Rodrigo Brechard <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 32683c9 commit ebd1265

9 files changed

Lines changed: 204 additions & 33 deletions

File tree

‎apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { SymbolView } from "../../components/ui/AppSymbol";
1414
import * as Dialog from "../../components/ui/Dialog";
1515
import { Menu } from "../../components/ui/Menu";
1616
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
17+
import { relayEnvironmentDiscovery } from "../../state/relay";
1718
import { useAtomCommand } from "../../state/use-atom-command";
1819
import {
1920
deregisterManagedRelayEnvironmentCommand,
@@ -46,6 +47,9 @@ export function T3ConnectProfilePage() {
4647
const deregisterEnvironment = useAtomCommand(deregisterManagedRelayEnvironmentCommand, {
4748
reportFailure: false,
4849
});
50+
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refreshAfterMutation, {
51+
reportFailure: false,
52+
});
4953
const [deregisteringEnvironmentId, setDeregisteringEnvironmentId] =
5054
useState<EnvironmentId | null>(null);
5155
const mutationPendingRef = useRef(false);
@@ -75,6 +79,7 @@ export function T3ConnectProfilePage() {
7579
return { accountId, linkedAtById };
7680
});
7781
environmentsState.refresh();
82+
void refreshRelayEnvironments();
7883
return;
7984
}
8085
if (isAtomCommandInterrupted(result)) return;

‎apps/mobile/src/state/use-remote-environment-registry.ts‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Dialog from "../components/ui/Dialog";
22
import { useAtomValue } from "@effect/atom-react";
33
import { useNavigation } from "@react-navigation/native";
4+
import { Discovery } from "@t3tools/client-runtime/relay";
45
import type { EnvironmentId } from "@t3tools/contracts";
56
import * as Cause from "effect/Cause";
67
import { AsyncResult, Atom } from "effect/reactivity";
@@ -17,8 +18,17 @@ import { environmentSession } from "./session";
1718
import { environmentCatalog } from "../connection/catalog";
1819
import { createRemoteEnvironmentProjectionAtoms } from "./remote-environment-projections";
1920
import { serverEnvironment } from "./server";
21+
import { relayEnvironmentDiscovery } from "./relay";
2022
import { connectPairingUrl as connectPairingUrlCommand } from "../connection/onboarding";
2123

24+
const RELAY_REMOVAL_ACCOUNT_NOTE: Record<Discovery.RelayEnvironmentRegistration, string> = {
25+
registered:
26+
"It stays on your T3 Connect account and keeps its host space. Deregister it under T3 Account → T3 Connect to free it.",
27+
unregistered: "It's no longer on your T3 Connect account.",
28+
unknown:
29+
"This doesn't change your T3 Connect account. To deregister it there, use T3 Account → T3 Connect.",
30+
};
31+
2232
const connectionPairingUrlAtom = Atom.make("").pipe(
2333
Atom.keepAlive,
2434
Atom.withLabel("mobile:connection-pairing-url"),
@@ -180,22 +190,20 @@ export function useRemoteConnections() {
180190
},
181191
} as const;
182192
if (environment.isRelayManaged) {
193+
const registration = Discovery.relayEnvironmentRegistration(
194+
appAtomRegistry.get(relayEnvironmentDiscovery.stateValueAtom),
195+
environmentId,
196+
);
197+
const openAccount = {
198+
text: "Open T3 Account",
199+
onPress: () => navigation.navigate("SettingsAuth"),
200+
};
183201
Dialog.alert(
184202
"Remove from this device?",
185-
`Forget ${environment.environmentLabel} and its cached threads on this device.\n\nIt stays on your T3 Connect account and keeps its host space. Deregister it under T3 Account → T3 Connect to free it.`,
186-
[
187-
{ text: "Cancel", style: "cancel" },
188-
// SettingsAuth renders nothing in builds without cloud config.
189-
...(hasCloudPublicConfig()
190-
? [
191-
{
192-
text: "Open T3 Account",
193-
onPress: () => navigation.navigate("SettingsAuth"),
194-
},
195-
]
196-
: []),
197-
remove,
198-
],
203+
`Forget ${environment.environmentLabel} and its cached threads on this device.\n\n${RELAY_REMOVAL_ACCOUNT_NOTE[registration]}`,
204+
registration === "unregistered" || !hasCloudPublicConfig()
205+
? [{ text: "Cancel", style: "cancel" }, remove]
206+
: [{ text: "Cancel", style: "cancel" }, openAccount, remove],
199207
);
200208
return;
201209
}

‎apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { Discovery } from "@t3tools/client-runtime/relay";
2+
import type { EnvironmentId } from "@t3tools/contracts";
13
import { useState } from "react";
24

5+
import { useRelayEnvironmentDiscovery } from "../../state/environments";
36
import {
47
AlertDialog,
58
AlertDialogClose,
@@ -14,17 +17,40 @@ import { useT3ConnectAccountPage } from "./T3ConnectAccountPages";
1417

1518
export function RemoveT3ConnectEnvironmentDialog({
1619
environmentLabel,
20+
environmentId,
1721
onCancel,
1822
onConfirm,
1923
}: {
2024
readonly environmentLabel: string | null;
25+
readonly environmentId: EnvironmentId | null;
2126
readonly onCancel: () => void;
2227
readonly onConfirm: () => void;
2328
}) {
2429
const accountPage = useT3ConnectAccountPage();
25-
const [shownLabel, setShownLabel] = useState(environmentLabel);
26-
if (environmentLabel !== null && environmentLabel !== shownLabel) setShownLabel(environmentLabel);
30+
const discovery = useRelayEnvironmentDiscovery();
31+
const registration = environmentId
32+
? Discovery.relayEnvironmentRegistration(discovery, environmentId)
33+
: "unknown";
34+
const [shown, setShown] = useState({ label: environmentLabel, registration });
35+
if (
36+
environmentLabel !== null &&
37+
(environmentLabel !== shown.label || registration !== shown.registration)
38+
) {
39+
setShown({ label: environmentLabel, registration });
40+
}
2741
const openAccountPage = accountPage.open;
42+
const accountPageLink = openAccountPage ? (
43+
<InlineButton
44+
onClick={() => {
45+
onCancel();
46+
openAccountPage();
47+
}}
48+
>
49+
T3 Connect settings
50+
</InlineButton>
51+
) : (
52+
"T3 Connect settings"
53+
);
2854

2955
return (
3056
<>
@@ -36,25 +62,24 @@ export function RemoveT3ConnectEnvironmentDialog({
3662
>
3763
<AlertDialogPopup>
3864
<AlertDialogHeader>
39-
<AlertDialogTitle>Remove {shownLabel} from this device?</AlertDialogTitle>
65+
<AlertDialogTitle>Remove {shown.label} from this device?</AlertDialogTitle>
4066
<AlertDialogDescription>
4167
This forgets its pairing, credentials, and cached threads here.
4268
</AlertDialogDescription>
4369
<AlertDialogDescription>
44-
It stays on your T3 Connect account and keeps its host space. Deregister it in{" "}
45-
{openAccountPage ? (
46-
<InlineButton
47-
onClick={() => {
48-
onCancel();
49-
openAccountPage();
50-
}}
51-
>
52-
T3 Connect settings
53-
</InlineButton>
70+
{shown.registration === "registered" ? (
71+
<>
72+
It stays on your T3 Connect account and keeps its host space. Deregister it in{" "}
73+
{accountPageLink} to free it.
74+
</>
75+
) : shown.registration === "unregistered" ? (
76+
"It's no longer on your T3 Connect account."
5477
) : (
55-
"T3 Connect settings"
56-
)}{" "}
57-
to free it.
78+
<>
79+
This doesn't change your T3 Connect account. To deregister it there, use{" "}
80+
{accountPageLink}.
81+
</>
82+
)}
5883
</AlertDialogDescription>
5984
</AlertDialogHeader>
6085
<AlertDialogFooter>

‎apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
useManagedRelayEnvironments,
1414
useManagedTunnelUsage,
1515
} from "../../cloud/managedRelayState";
16+
import { relayEnvironmentDiscovery } from "../../state/relay";
1617
import { useAtomCommand } from "../../state/use-atom-command";
1718
import { T3CHAT_SUBSCRIPTION_URL, TunnelUsageMeter, tunnelUpgradeCopy } from "../cloud/TunnelUsage";
1819
import { Button, InlineButton } from "../ui/button";
@@ -165,6 +166,9 @@ export function T3ConnectUserProfilePage() {
165166
const deregisterEnvironment = useAtomCommand(deregisterManagedRelayEnvironmentCommand, {
166167
reportFailure: false,
167168
});
169+
const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refreshAfterMutation, {
170+
reportFailure: false,
171+
});
168172
const [deregisteringEnvironmentId, setDeregisteringEnvironmentId] =
169173
useState<EnvironmentId | null>(null);
170174
const [confirmingEnvironmentId, setConfirmingEnvironmentId] = useState<EnvironmentId | null>(
@@ -197,6 +201,7 @@ export function T3ConnectUserProfilePage() {
197201
return { accountId, linkedAtById };
198202
});
199203
environmentsState.refresh();
204+
void refreshRelayEnvironments();
200205
toastManager.add({
201206
type: "success",
202207
title: "Server deregistered",

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ export function ConnectionsSettings({ view }: { readonly view: "environments" |
15181518
{hasCloudPublicConfig() ? (
15191519
<RemoveT3ConnectEnvironmentDialog
15201520
environmentLabel={pendingT3ConnectRemoval?.label ?? null}
1521+
environmentId={pendingT3ConnectRemoval?.environmentId ?? null}
15211522
onCancel={() => setPendingT3ConnectRemoval(null)}
15221523
onConfirm={() => {
15231524
if (!pendingT3ConnectRemoval) return;

‎docs/user/remote-access.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ shows that it was removed from T3 Connect. To link it again, turn on
374374
command-line host that released its tunnel when it last stopped may link itself
375375
again on its next start; run `t3 connect unlink` on that host to keep it off.
376376
Removing an environment from a device's connection settings only forgets it on
377-
that device; it stays registered to your account.
377+
that device and doesn't change your T3 Connect account. A device keeps a saved
378+
environment after it's deregistered until you remove it from that device.
378379

379380
When idle tunnel cleanup is enabled, T3 Connect removes a linked environment's
380381
tunnel after it stays offline for several minutes. The environment stays linked

‎packages/client-runtime/src/relay/discovery.test.ts‎

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Option from "effect/Option";
1212
import * as Ref from "effect/Ref";
1313
import * as Stream from "effect/Stream";
1414
import * as SubscriptionRef from "effect/SubscriptionRef";
15+
import { Atom, AtomRegistry } from "effect/reactivity";
1516

1617
import * as ManagedRelay from "./managedRelay.ts";
1718
import * as ClientCapabilities from "../platform/capabilities.ts";
@@ -20,6 +21,7 @@ import { ConnectionBlockedError, type NetworkStatus } from "../connection/model.
2021
import * as ConnectionWakeups from "../connection/wakeups.ts";
2122
import * as RelayEnvironmentDiscovery from "./discovery.ts";
2223
import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
24+
import { createRelayEnvironmentDiscoveryAtoms } from "../state/relayDiscovery.ts";
2325

2426
const environments = [
2527
{
@@ -59,6 +61,8 @@ function status(
5961
const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
6062
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>("online");
6163
const listCalls = yield* Ref.make(0);
64+
const listedEnvironments =
65+
yield* Ref.make<ReadonlyArray<RelayClientEnvironmentRecord>>(environments);
6266
const listFailure = yield* Ref.make<ManagedRelay.ManagedRelayClientError | null>(null);
6367
const secondListCall = yield* Deferred.make<void>();
6468
const clerkToken = yield* Ref.make<string | null>("clerk-token");
@@ -100,7 +104,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
100104
if (failure) {
101105
return yield* failure;
102106
}
103-
return environments;
107+
return yield* Ref.get(listedEnvironments);
104108
}),
105109
getEnvironmentStatus: ({ environmentId }) =>
106110
Ref.get(statusRequests).pipe(
@@ -165,6 +169,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
165169
return {
166170
layer,
167171
listCalls,
172+
listedEnvironments,
168173
listFailure,
169174
clerkToken,
170175
networkStatus,
@@ -179,6 +184,51 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
179184
});
180185

181186
describe("RelayEnvironmentDiscovery", () => {
187+
it.effect("fetches a fresh list after removal while an older refresh is checking statuses", () =>
188+
Effect.scoped(
189+
Effect.gen(function* () {
190+
const harness = yield* makeHarness();
191+
yield* Effect.gen(function* () {
192+
const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
193+
const atoms = createRelayEnvironmentDiscoveryAtoms(
194+
Atom.runtime(
195+
Layer.succeed(RelayEnvironmentDiscovery.RelayEnvironmentDiscovery, discovery),
196+
),
197+
);
198+
const registry = AtomRegistry.make();
199+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
200+
201+
const refresh = atoms.refresh.run(registry, undefined);
202+
yield* SubscriptionRef.changes(discovery.state).pipe(
203+
Stream.filter((state) => state.environments.size === environments.length),
204+
Stream.runHead,
205+
);
206+
const coalescedRefresh = atoms.refresh.run(registry, undefined);
207+
yield* Ref.set(harness.listedEnvironments, [environments[1]!]);
208+
const mutationRefresh = atoms.refreshAfterMutation.run(registry, undefined);
209+
210+
const requests = yield* Ref.get(harness.statusRequests);
211+
for (const environment of environments) {
212+
yield* Deferred.succeed(
213+
requests.get(environment.environmentId)!,
214+
status(environment, "online"),
215+
);
216+
}
217+
const results = yield* Effect.promise(() =>
218+
Promise.all([refresh, coalescedRefresh, mutationRefresh]),
219+
);
220+
for (const result of results) {
221+
expect(result._tag).toBe("Success");
222+
}
223+
expect(yield* Ref.get(harness.listCalls)).toBe(2);
224+
const complete = yield* SubscriptionRef.get(discovery.state);
225+
expect([...complete.environments.keys()]).toEqual([environments[1]!.environmentId]);
226+
expect(complete.refreshing).toBe(false);
227+
}).pipe(Effect.provide(harness.layer));
228+
}),
229+
),
230+
);
231+
182232
it.effect("publishes each environment status as soon as that lookup completes", () =>
183233
Effect.gen(function* () {
184234
const harness = yield* makeHarness();
@@ -228,6 +278,38 @@ describe("RelayEnvironmentDiscovery", () => {
228278
}),
229279
);
230280

281+
it.effect("reports an environment as unregistered only once the relay list has loaded", () =>
282+
Effect.gen(function* () {
283+
const harness = yield* makeHarness();
284+
yield* Effect.gen(function* () {
285+
const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
286+
const listed = environments[0]!.environmentId;
287+
const deregistered = "deregistered-environment";
288+
const before = yield* SubscriptionRef.get(discovery.state);
289+
expect(RelayEnvironmentDiscovery.relayEnvironmentRegistration(before, deregistered)).toBe(
290+
"unknown",
291+
);
292+
293+
const requests = yield* Ref.get(harness.statusRequests);
294+
for (const environment of environments) {
295+
yield* Deferred.succeed(
296+
requests.get(environment.environmentId)!,
297+
status(environment, "online"),
298+
);
299+
}
300+
yield* discovery.refresh;
301+
302+
const after = yield* SubscriptionRef.get(discovery.state);
303+
expect(RelayEnvironmentDiscovery.relayEnvironmentRegistration(after, listed)).toBe(
304+
"registered",
305+
);
306+
expect(RelayEnvironmentDiscovery.relayEnvironmentRegistration(after, deregistered)).toBe(
307+
"unregistered",
308+
);
309+
}).pipe(Effect.provide(harness.layer));
310+
}),
311+
);
312+
231313
it.effect(
232314
"preserves discovered rows while offline and refreshes after connectivity returns",
233315
() =>
@@ -350,6 +432,12 @@ describe("RelayEnvironmentDiscovery", () => {
350432
const failed = yield* SubscriptionRef.get(discovery.state);
351433
expect(failed.environments.size).toBe(0);
352434
expect(Option.isSome(failed.error)).toBe(true);
435+
expect(
436+
RelayEnvironmentDiscovery.relayEnvironmentRegistration(
437+
failed,
438+
environments[0]!.environmentId,
439+
),
440+
).toBe("unknown");
353441
}).pipe(Effect.provide(harness.layer));
354442
}),
355443
);
@@ -397,6 +485,12 @@ describe("RelayEnvironmentDiscovery", () => {
397485
expect(state.environments.size).toBe(0);
398486
expect(state.refreshing).toBe(false);
399487
expect(Option.isNone(state.error)).toBe(true);
488+
expect(
489+
RelayEnvironmentDiscovery.relayEnvironmentRegistration(
490+
state,
491+
environments[0]!.environmentId,
492+
),
493+
).toBe("unknown");
400494
}).pipe(Effect.provide(harness.layer));
401495
}),
402496
);
@@ -429,7 +523,14 @@ describe("RelayEnvironmentDiscovery", () => {
429523
yield* Fiber.join(refreshFiber);
430524
yield* Effect.yieldNow;
431525

432-
expect((yield* SubscriptionRef.get(discovery.state)).environments.size).toBe(0);
526+
const signedOut = yield* SubscriptionRef.get(discovery.state);
527+
expect(signedOut.environments.size).toBe(0);
528+
expect(
529+
RelayEnvironmentDiscovery.relayEnvironmentRegistration(
530+
signedOut,
531+
environments[0]!.environmentId,
532+
),
533+
).toBe("unknown");
433534
}).pipe(Effect.provide(harness.layer), Effect.scoped);
434535
}),
435536
);

0 commit comments

Comments
 (0)