Skip to content

Commit 5bb6192

Browse files
authored
fix(desktop): remediate all onboarding dead paths (D1-D7) (#1856)
Signed-off-by: Will Pfleger <[email protected]>
1 parent 7e009a8 commit 5bb6192

21 files changed

Lines changed: 2188 additions & 365 deletions

‎desktop/src/app/App.tsx‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { useCommunityInit } from "@/features/communities/useCommunityInit";
2727
import { useNestNotifications } from "@/features/communities/useNestNotifications";
2828
import { useCommunities } from "@/features/communities/useCommunities";
2929
import { WelcomeSetup } from "@/features/communities/ui/WelcomeSetup";
30+
import { CommunityApplyErrorScreen } from "@/features/communities/ui/CommunityApplyErrorScreen";
31+
import { CommunityChangeOverlay } from "@/features/communities/ui/CommunityChangeOverlay";
3032
import { createBuzzQueryClient } from "@/shared/api/queryClient";
3133
import { isSharedIdentity as isSharedIdentityCmd } from "@/shared/api/tauri";
3234
import { listenForDeepLinks } from "@/shared/deep-link";
@@ -274,19 +276,15 @@ function CommunityQueryProvider({ children }: { children: ReactNode }) {
274276
}
275277

276278
function AppReady({
277-
canBackToCommunitySetup,
278279
isCompletingFirstRunCommunity,
279280
isSharedIdentity,
280281
isCommunitySwitch,
281282
onFirstRunCommunitySettled,
282-
onBackToCommunitySetup,
283283
}: {
284-
canBackToCommunitySetup: boolean;
285284
isCompletingFirstRunCommunity: boolean;
286285
isSharedIdentity: boolean;
287286
isCommunitySwitch: boolean;
288287
onFirstRunCommunitySettled: () => void;
289-
onBackToCommunitySetup: () => void;
290288
}) {
291289
const onboarding = useAppOnboardingState(isSharedIdentity);
292290

@@ -316,11 +314,9 @@ function AppReady({
316314
return (
317315
<OnboardingFlow
318316
actions={onboarding.flow.actions}
319-
canBackToCommunitySetup={canBackToCommunitySetup}
320317
identityLost={onboarding.identityLost}
321318
initialProfile={onboarding.flow.initialProfile}
322319
key={onboarding.currentPubkey ?? "anonymous"}
323-
onBackToCommunitySetup={onBackToCommunitySetup}
324320
/>
325321
);
326322
}
@@ -360,16 +356,12 @@ export function App() {
360356
activeCommunity,
361357
reinitKey,
362358
addCommunity,
363-
clearCommunities,
364359
switchCommunity,
365360
reconnectCommunity,
366361
} = useCommunities();
367362
const [isCompletingFirstRunCommunity, setIsCompletingFirstRunCommunity] =
368363
useState(false);
369-
const [canBackToCommunitySetup, setCanBackToCommunitySetup] = useState(false);
370-
const [welcomeTransitionMode, setWelcomeTransitionMode] = useState<
371-
"initial" | "backward"
372-
>("initial");
364+
const [isCommunityChangeOpen, setIsCommunityChangeOpen] = useState(false);
373365

374366
useEffect(() => {
375367
const unlisten = listenForDeepLinks({
@@ -408,22 +400,13 @@ export function App() {
408400

409401
const handleSetupComplete = useCallback(
410402
(community: Community) => {
411-
setWelcomeTransitionMode("initial");
412403
setIsCompletingFirstRunCommunity(true);
413-
setCanBackToCommunitySetup(true);
414404
const communityId = addCommunity(community);
415405
switchCommunity(communityId);
416406
},
417407
[addCommunity, switchCommunity],
418408
);
419409

420-
const handleBackToCommunitySetup = useCallback(() => {
421-
setWelcomeTransitionMode("backward");
422-
setIsCompletingFirstRunCommunity(false);
423-
setCanBackToCommunitySetup(false);
424-
clearCommunities();
425-
}, [clearCommunities]);
426-
427410
const handleFirstRunCommunitySettled = useCallback(() => {
428411
setIsCompletingFirstRunCommunity(false);
429412
}, []);
@@ -442,12 +425,29 @@ export function App() {
442425
return (
443426
<WelcomeSetup
444427
defaultRelayUrl={community.defaultRelayUrl}
445-
initialTransitionMode={welcomeTransitionMode}
446428
onComplete={handleSetupComplete}
447429
/>
448430
);
449431
}
450432

433+
// Surface apply failures so the user can retry or change community.
434+
if ("error" in community && community.error) {
435+
return (
436+
<>
437+
<CommunityApplyErrorScreen
438+
error={community.error}
439+
onChangeCommunity={() => setIsCommunityChangeOpen(true)}
440+
onRetry={reconnectCommunity}
441+
/>
442+
{isCommunityChangeOpen ? (
443+
<CommunityChangeOverlay
444+
onClose={() => setIsCommunityChangeOpen(false)}
445+
/>
446+
) : null}
447+
</>
448+
);
449+
}
450+
451451
// Wait for this exact community config to be applied to the backend before
452452
// rendering anything that connects to the relay. The appliedKey check avoids
453453
// a one-render race where React sees the new active community while the Tauri
@@ -471,13 +471,11 @@ export function App() {
471471
return (
472472
<CommunityQueryProvider key={communityKey}>
473473
<AppReady
474-
canBackToCommunitySetup={canBackToCommunitySetup}
475474
isCompletingFirstRunCommunity={isCompletingFirstRunCommunity}
476475
isCommunitySwitch={isCommunitySwitch}
477476
key={communityKey}
478477
isSharedIdentity={sharedIdentity}
479478
onFirstRunCommunitySettled={handleFirstRunCommunitySettled}
480-
onBackToCommunitySetup={handleBackToCommunitySetup}
481479
/>
482480
{showBootSplashOverlay ? (
483481
<div
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Unit tests for relay URL normalization and probe helpers (D4).
3+
*
4+
* normalizeRelayUrl is pure and fully testable. probeRelayReachable requires
5+
* a WebSocket runtime — its contract (cancel-safe, timeout-bounded, close-
6+
* on-all-exits) is verified by the E2E onboarding specs on CI shards.
7+
*/
8+
import assert from "node:assert/strict";
9+
import test from "node:test";
10+
11+
import { normalizeRelayUrl } from "./relayProbe.ts";
12+
13+
// ---------------------------------------------------------------------------
14+
// Valid inputs
15+
// ---------------------------------------------------------------------------
16+
17+
test("normalizeRelayUrl_wss_passthrough", () => {
18+
assert.equal(
19+
normalizeRelayUrl("wss://relay.example.com"),
20+
"wss://relay.example.com",
21+
);
22+
});
23+
24+
test("normalizeRelayUrl_ws_passthrough", () => {
25+
assert.equal(normalizeRelayUrl("ws://localhost:3000"), "ws://localhost:3000");
26+
});
27+
28+
test("normalizeRelayUrl_https_converts_to_wss", () => {
29+
assert.equal(
30+
normalizeRelayUrl("https://relay.example.com"),
31+
"wss://relay.example.com",
32+
);
33+
});
34+
35+
test("normalizeRelayUrl_http_converts_to_ws", () => {
36+
assert.equal(
37+
normalizeRelayUrl("http://localhost:3000"),
38+
"ws://localhost:3000",
39+
);
40+
});
41+
42+
test("normalizeRelayUrl_strips_trailing_slashes", () => {
43+
assert.equal(
44+
normalizeRelayUrl("wss://relay.example.com///"),
45+
"wss://relay.example.com",
46+
);
47+
});
48+
49+
test("normalizeRelayUrl_trims_whitespace", () => {
50+
assert.equal(
51+
normalizeRelayUrl(" wss://relay.example.com "),
52+
"wss://relay.example.com",
53+
);
54+
});
55+
56+
test("normalizeRelayUrl_https_with_port_converts_to_wss", () => {
57+
assert.equal(
58+
normalizeRelayUrl("https://relay.example.com:8443"),
59+
"wss://relay.example.com:8443",
60+
);
61+
});
62+
63+
test("normalizeRelayUrl_wss_with_path_preserves_path", () => {
64+
assert.equal(
65+
normalizeRelayUrl("wss://relay.example.com/custom"),
66+
"wss://relay.example.com/custom",
67+
);
68+
});
69+
70+
// ---------------------------------------------------------------------------
71+
// Invalid / rejected inputs
72+
// ---------------------------------------------------------------------------
73+
74+
test("normalizeRelayUrl_empty_returns_null", () => {
75+
assert.equal(normalizeRelayUrl(""), null);
76+
assert.equal(normalizeRelayUrl(" "), null);
77+
});
78+
79+
test("normalizeRelayUrl_bare_hostname_returns_null", () => {
80+
assert.equal(normalizeRelayUrl("relay.example.com"), null);
81+
});
82+
83+
test("normalizeRelayUrl_ftp_scheme_returns_null", () => {
84+
assert.equal(normalizeRelayUrl("ftp://relay.example.com"), null);
85+
});
86+
87+
test("normalizeRelayUrl_garbage_returns_null", () => {
88+
assert.equal(normalizeRelayUrl("not a url at all"), null);
89+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Normalize a relay URL to ws(s):// form and probe reachability.
3+
*/
4+
5+
/** Normalize a user-entered relay URL to ws(s):// form. Returns null if invalid. */
6+
export function normalizeRelayUrl(input: string): string | null {
7+
const trimmed = input.trim().replace(/\/+$/, "");
8+
if (!trimmed) return null;
9+
10+
// Already ws(s)://
11+
if (trimmed.startsWith("wss://") || trimmed.startsWith("ws://")) {
12+
try {
13+
new URL(trimmed);
14+
return trimmed;
15+
} catch {
16+
return null;
17+
}
18+
}
19+
20+
// Convert https → wss, http → ws
21+
if (trimmed.startsWith("https://")) {
22+
const wsUrl = `wss://${trimmed.slice(8)}`;
23+
try {
24+
new URL(wsUrl);
25+
return wsUrl;
26+
} catch {
27+
return null;
28+
}
29+
}
30+
if (trimmed.startsWith("http://")) {
31+
const wsUrl = `ws://${trimmed.slice(7)}`;
32+
try {
33+
new URL(wsUrl);
34+
return wsUrl;
35+
} catch {
36+
return null;
37+
}
38+
}
39+
40+
return null;
41+
}
42+
43+
/**
44+
* Probe whether a WebSocket relay is reachable. Opens a connection with a
45+
* timeout; resolves `true` if the socket opens, `false` on timeout/error.
46+
* The socket is always closed before returning.
47+
*/
48+
export function probeRelayReachable(
49+
wsUrl: string,
50+
timeoutMs = 4000,
51+
): { promise: Promise<boolean>; cancel: () => void } {
52+
let socket: WebSocket | null = null;
53+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
54+
let settled = false;
55+
56+
const promise = new Promise<boolean>((resolve) => {
57+
function settle(result: boolean) {
58+
if (settled) return;
59+
settled = true;
60+
if (timeoutId !== null) clearTimeout(timeoutId);
61+
try {
62+
socket?.close();
63+
} catch {
64+
/* ignore */
65+
}
66+
socket = null;
67+
resolve(result);
68+
}
69+
70+
try {
71+
socket = new WebSocket(wsUrl);
72+
socket.onopen = () => settle(true);
73+
socket.onerror = () => settle(false);
74+
socket.onclose = () => settle(false);
75+
timeoutId = setTimeout(() => settle(false), timeoutMs);
76+
} catch {
77+
settle(false);
78+
}
79+
});
80+
81+
return {
82+
promise,
83+
cancel() {
84+
if (settled) return;
85+
settled = true;
86+
if (timeoutId !== null) clearTimeout(timeoutId);
87+
try {
88+
socket?.close();
89+
} catch {
90+
/* ignore */
91+
}
92+
socket = null;
93+
},
94+
};
95+
}

0 commit comments

Comments
 (0)