Skip to content

Commit cac134c

Browse files
Address second round of PR review feedback
- Detect signed-out at the session token step in relay discovery so real relay auth failures surface as errors again - Surface discovery errors/offline in the connect lists (web error state with retry; mobile error card also shown alongside connected rows) - Request mobile onboarding after account cleanup finishes so the sheet never lists the previous account's environments - Ignore pending/incomplete Clerk snapshots in the web sign-in observer - Wait for the primary connection target before opening the wizard when the session can publish, so the publish step can't be skipped - Prefill publish toggles only when the link belongs to the onboarding account - Block Done/stepper (not just dismissal) while a link request is in flight - Let unlink proceed without a relay token - Replace to Home when the onboarding route opens without cloud config and there is nothing to go back to Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent eb78963 commit cac134c

7 files changed

Lines changed: 117 additions & 37 deletions

File tree

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ function CloudAuthBridge(props: { readonly children: ReactNode }) {
7272
// cold start observes undefined → account and must not re-prompt) requests
7373
// the T3 Connect onboarding sheet — account transitions clear the
7474
// connected environments, so each new session starts with no devices to
75-
// reach. Sign-out drops any not-yet-presented request instead.
76-
if (previousObservedAccount !== undefined && previousObservedAccount !== nextAccount) {
77-
if (nextAccount !== null) {
78-
requestConnectOnboarding(nextAccount);
79-
} else {
80-
clearConnectOnboardingRequest();
81-
}
75+
// reach. The request itself is issued after the cleanup transition inside
76+
// activateSession, so the sheet never lists the previous account's
77+
// environments; sign-out drops any not-yet-presented request instead.
78+
const isAccountTransition =
79+
previousObservedAccount !== undefined && previousObservedAccount !== nextAccount;
80+
if (isAccountTransition && nextAccount === null) {
81+
clearConnectOnboardingRequest();
8282
}
8383

8484
const queueAccountCleanup = (
@@ -128,6 +128,9 @@ function CloudAuthBridge(props: { readonly children: ReactNode }) {
128128
}
129129
previousTokenProviderRef.current = { userId, provider: tokenProvider };
130130
activateCloudRelayAccount(userId, tokenProvider);
131+
if (isAccountTransition) {
132+
requestConnectOnboarding(userId);
133+
}
131134
};
132135
const activateAfterTransition = (transition: Promise<void>) => {
133136
void (async () => {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NativeHeaderToolbar } from "../../native/StackHeader";
22
import { useAuth } from "@clerk/expo";
3-
import { useNavigation } from "@react-navigation/native";
3+
import { StackActions, useNavigation } from "@react-navigation/native";
44
import { useCallback, useEffect, useState } from "react";
55
import { Pressable, RefreshControl, ScrollView, View } from "react-native";
66
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -27,8 +27,13 @@ export function ConnectOnboardingRouteScreen() {
2727
// The route is deep-linkable; without cloud config the sheet would present
2828
// empty with no chrome to dismiss it, so bail back out instead.
2929
useEffect(() => {
30-
if (!hasCloudPublicConfig() && navigation.canGoBack()) {
30+
if (hasCloudPublicConfig()) {
31+
return;
32+
}
33+
if (navigation.canGoBack()) {
3134
navigation.goBack();
35+
} else {
36+
navigation.dispatch(StackActions.replace("Home"));
3237
}
3338
}, [navigation]);
3439

‎apps/mobile/src/features/connection/CloudEnvironmentRows.tsx‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,17 @@ export function CloudEnvironmentRows(props: {
124124
Loading linked cloud environments.
125125
</Text>
126126
</View>
127-
) : controller.relayDiscovery.error ? (
127+
) : controller.relayDiscovery.error ? null : (
128+
<View collapsable={false} className="rounded-[24px] bg-card p-5">
129+
<Text className="text-sm leading-normal text-foreground-muted">
130+
No additional linked cloud environments.
131+
</Text>
132+
</View>
133+
)}
134+
135+
{/* Rendered alongside any connected rows — a failed discovery must not
136+
hide behind an otherwise-healthy list. */}
137+
{controller.relayDiscovery.error && !controller.relayDiscovery.isRefreshing ? (
128138
<View collapsable={false} className="gap-3 rounded-[24px] bg-card p-5">
129139
<Text className="text-base font-t3-bold text-foreground">
130140
Could not load T3 Connect environments
@@ -135,22 +145,15 @@ export function CloudEnvironmentRows(props: {
135145
) : null}
136146
<Pressable
137147
accessibilityRole="button"
138-
disabled={controller.relayDiscovery.isRefreshing}
139148
onPress={() => {
140149
void controller.refreshRelayEnvironments();
141150
}}
142-
className="self-start rounded-full bg-subtle px-3.5 py-2 active:opacity-70 disabled:opacity-50"
151+
className="self-start rounded-full bg-subtle px-3.5 py-2 active:opacity-70"
143152
>
144153
<Text className="text-xs font-t3-bold text-foreground">Try again</Text>
145154
</Pressable>
146155
</View>
147-
) : (
148-
<View collapsable={false} className="rounded-[24px] bg-card p-5">
149-
<Text className="text-sm leading-normal text-foreground-muted">
150-
No additional linked cloud environments.
151-
</Text>
152-
</View>
153-
)}
156+
) : null}
154157
</View>
155158
);
156159
}

‎apps/web/src/cloud/useCloudLinkController.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,18 @@ export function useCloudLinkController() {
8585
return false;
8686
}
8787
const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions()));
88-
if (tokenResult._tag === "Failure") {
89-
reportUpdateFailure(squashAtomCommandFailure(tokenResult));
90-
return false;
91-
}
92-
const clerkToken = tokenResult.value;
9388
const wantsLink = desired.managedTunnel || desired.publish;
9489

9590
// A failure after this point may follow a partially applied mutation (e.g.
9691
// the link succeeded but the preference update did not), so every exit —
9792
// success or failure — refreshes the rendered state to whatever the server
9893
// actually holds now.
9994
if (!wantsLink) {
95+
// Unlink works without a relay token — a failed token read must not
96+
// leave the user unable to turn T3 Connect off.
10097
const unlinkResult = await unlinkPrimaryEnvironment({
10198
target,
102-
clerkToken: clerkToken ?? null,
99+
clerkToken: tokenResult._tag === "Success" ? (tokenResult.value ?? null) : null,
103100
});
104101
if (unlinkResult._tag === "Failure") {
105102
if (!isAtomCommandInterrupted(unlinkResult)) {
@@ -109,6 +106,11 @@ export function useCloudLinkController() {
109106
return false;
110107
}
111108
} else {
109+
if (tokenResult._tag === "Failure") {
110+
reportUpdateFailure(squashAtomCommandFailure(tokenResult));
111+
return false;
112+
}
113+
const clerkToken = tokenResult.value;
112114
if (!clerkToken) {
113115
reportUpdateFailure(new Error("Sign in to T3 Connect before enabling this."));
114116
return false;

‎apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ export function CloudEnvironmentConnectRows({
136136
}
137137

138138
if (standalone && visibleEnvironments.length === 0) {
139+
// A failed or offline discovery is not "no environments" — misreporting it
140+
// as empty would read as the user's devices having disappeared.
141+
const discoveryProblem = environmentsState.offline
142+
? "You appear to be offline."
143+
: (Option.getOrNull(environmentsState.error)?.message ?? null);
144+
if (discoveryProblem !== null && !environmentsState.refreshing) {
145+
return (
146+
<div className={ITEM_ROW_CLASSNAME}>
147+
<p className="text-sm font-medium text-destructive">
148+
Could not load T3 Connect environments
149+
</p>
150+
<p className="mt-1 text-xs text-muted-foreground">{discoveryProblem}</p>
151+
<Button
152+
size="sm"
153+
variant="outline"
154+
className="mt-3"
155+
onClick={() => void refreshRelayEnvironments()}
156+
>
157+
Try again
158+
</Button>
159+
</div>
160+
);
161+
}
139162
return empty;
140163
}
141164

‎apps/web/src/components/cloud/ConnectOnboardingDialog.tsx‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export function ConnectOnboardingDialog() {
4747
type OnboardingStep = "publish" | "devices";
4848

4949
function ConfiguredConnectOnboardingDialog() {
50-
const { isLoaded, isSignedIn, userId } = useAuth();
50+
// Mirrors ManagedRelayAuthProvider: a pending Clerk session must not read as
51+
// signed-out, or its later activation would look like a fresh sign-in.
52+
const { isLoaded, isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false });
5153
const [optOutState, setOptOutState] = useLocalStorage(
5254
CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY,
5355
EMPTY_CONNECT_ONBOARDING_OPT_OUT_STATE,
@@ -95,6 +97,10 @@ function ConfiguredConnectOnboardingDialog() {
9597
// load observes undefined → account and must not re-prompt.
9698
useEffect(() => {
9799
if (!isLoaded) return;
100+
// A loaded-but-incomplete snapshot (signed in, user id not yet populated)
101+
// must not be recorded as signed-out — the next render would then look
102+
// like a fresh sign-in on a cold load.
103+
if (isSignedIn && !userId) return;
98104
const previousAccount = observedAccountRef.current;
99105
const nextAccount = isSignedIn && userId ? userId : null;
100106
observedAccountRef.current = nextAccount;
@@ -103,6 +109,12 @@ function ConfiguredConnectOnboardingDialog() {
103109
}
104110
}, [isLoaded, isSignedIn, userId]);
105111

112+
// A manageable session implies a primary environment, so when the scopes
113+
// allow publishing, wait for the connection target too — otherwise the
114+
// wizard could open on the devices step moments before the publish step
115+
// becomes available and freeze there.
116+
const publishStepDecided = !canManageRelay || controller.linkState.target !== null;
117+
106118
// Open once the session scopes resolve so the step set is stable. Accounts
107119
// that chose "Don't show this again" are skipped.
108120
useEffect(() => {
@@ -111,7 +123,7 @@ function ConfiguredConnectOnboardingDialog() {
111123
setRequestedAccount(null);
112124
return;
113125
}
114-
if (!sessionScopesKnown) return;
126+
if (!sessionScopesKnown || !publishStepDecided) return;
115127
setRequestedAccount(null);
116128
prefilledFromLinkStateRef.current = false;
117129
setExposeEnvironment(true);
@@ -124,6 +136,7 @@ function ConfiguredConnectOnboardingDialog() {
124136
controller.linkState.target,
125137
openForAccount,
126138
optOutAccounts,
139+
publishStepDecided,
127140
requestedAccount,
128141
sessionScopesKnown,
129142
]);
@@ -141,19 +154,25 @@ function ConfiguredConnectOnboardingDialog() {
141154

142155
// Toggles default on, but an environment that is already linked should show
143156
// its actual configuration instead of silently proposing to rewrite it.
157+
// Only when the link belongs to the account being onboarded, though — after
158+
// an account switch the cached link state can still describe the previous
159+
// account's setup.
144160
const linkStateData = controller.linkState.data;
145161
useEffect(() => {
146162
if (openForAccount === null || prefilledFromLinkStateRef.current || linkStateData === null) {
147163
return;
148164
}
149165
prefilledFromLinkStateRef.current = true;
150-
if (linkStateData.linked) {
166+
if (linkStateData.linked && linkStateData.cloudUserId === openForAccount) {
151167
setExposeEnvironment(linkStateData.managedTunnelActive ?? linkStateData.linked);
152168
setPublishAgentActivity(linkStateData.publishAgentActivity);
153169
}
154170
}, [linkStateData, openForAccount]);
155171

156172
const complete = () => {
173+
// Keep the wizard up while a link request is in flight so its outcome
174+
// (and any failure) stays visible.
175+
if (isApplying) return;
157176
const account = openForAccount;
158177
setOpenForAccount(null);
159178
if (account !== null && dontShowAgain) {
@@ -206,7 +225,12 @@ function ConfiguredConnectOnboardingDialog() {
206225
place.
207226
</DialogDescription>
208227
{steps.length > 1 ? (
209-
<OnboardingStepper steps={steps} currentStep={step} onStepSelect={setStep} />
228+
<OnboardingStepper
229+
steps={steps}
230+
currentStep={step}
231+
disabled={isApplying}
232+
onStepSelect={setStep}
233+
/>
210234
) : null}
211235
</DialogHeader>
212236
<DialogPanel>
@@ -247,7 +271,9 @@ function ConfiguredConnectOnboardingDialog() {
247271
</Button>
248272
</>
249273
) : (
250-
<Button onClick={complete}>Done</Button>
274+
<Button disabled={isApplying} onClick={complete}>
275+
Done
276+
</Button>
251277
)}
252278
</div>
253279
</DialogFooter>
@@ -264,10 +290,12 @@ const STEP_LABELS: Record<OnboardingStep, string> = {
264290
function OnboardingStepper({
265291
steps,
266292
currentStep,
293+
disabled,
267294
onStepSelect,
268295
}: {
269296
readonly steps: ReadonlyArray<OnboardingStep>;
270297
readonly currentStep: OnboardingStep;
298+
readonly disabled: boolean;
271299
readonly onStepSelect: (step: OnboardingStep) => void;
272300
}) {
273301
const currentIndex = steps.indexOf(currentStep);
@@ -277,6 +305,7 @@ function OnboardingStepper({
277305
<button
278306
key={step}
279307
type="button"
308+
disabled={disabled}
280309
className={cn(
281310
"grid min-w-0 grid-cols-[1rem_minmax(0,1fr)] gap-x-2 rounded-lg border px-3 py-2 text-left",
282311
index === currentIndex

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,27 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
222222
error: Option.none(),
223223
});
224224

225-
const clerkToken = yield* session.clerkToken;
225+
// Signed out is the idle state, not a failure: the proactive refresh on
226+
// credentials-changed also runs on sign-out and must settle back to a
227+
// clean empty list. Only the session-level "no credentials" error is
228+
// benign — relay-side auth failures (expired/invalid tokens) happen
229+
// after this point and must surface as errors.
230+
const tokenResult = yield* Effect.result(session.clerkToken);
231+
if (tokenResult._tag === "Failure") {
232+
const failure = tokenResult.failure;
233+
if (failure._tag === "ConnectionBlockedError" && failure.reason === "authentication") {
234+
if ((yield* Ref.get(accountGeneration)) !== generation) {
235+
return;
236+
}
237+
yield* SubscriptionRef.update(state, (current) => ({
238+
...current,
239+
refreshing: false,
240+
}));
241+
return;
242+
}
243+
return yield* Effect.fail(failure);
244+
}
245+
const clerkToken = tokenResult.success;
226246
if ((yield* Ref.get(accountGeneration)) !== generation) {
227247
return;
228248
}
@@ -279,15 +299,10 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
279299
if ((yield* Ref.get(accountGeneration)) !== generation) {
280300
return;
281301
}
282-
// Signed out is the idle state, not a failure: the proactive
283-
// refresh on credentials-changed also runs on sign-out and must
284-
// settle back to a clean empty list.
285-
const signedOut =
286-
error._tag === "ConnectionBlockedError" && error.reason === "authentication";
287302
yield* SubscriptionRef.update(state, (current) => ({
288303
...current,
289304
refreshing: false,
290-
error: signedOut ? Option.none() : Option.some(error),
305+
error: Option.some(error),
291306
}));
292307
}),
293308
),

0 commit comments

Comments
 (0)