Skip to content

Commit 775cf67

Browse files
wesbillmanPinky
andauthored
fix(desktop): recover first community joins (#2087)
Signed-off-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent 5a656c9 commit 775cf67

7 files changed

Lines changed: 265 additions & 96 deletions

File tree

‎desktop/src/app/App.tsx‎

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import { WelcomeSetup } from "@/features/communities/ui/WelcomeSetup";
3939
import { CommunityApplyErrorScreen } from "@/features/communities/ui/CommunityApplyErrorScreen";
4040
import { CommunityChangeOverlay } from "@/features/communities/ui/CommunityChangeOverlay";
4141
import { createBuzzQueryClient } from "@/shared/api/queryClient";
42-
import { getMyRelayMembershipLookup } from "@/shared/api/relayMembers";
4342
import { isSharedIdentity as isSharedIdentityCmd } from "@/shared/api/tauri";
4443
import {
4544
type AddCommunityDeepLinkPayload,
@@ -62,16 +61,6 @@ const BOOT_SPLASH_MIN_VISIBLE_MS = 1_200;
6261
const BOOT_SPLASH_FADE_MS = 200;
6362
const INITIAL_RENDER_READY_EVENT = "initial-render-ready";
6463

65-
function isRelayMembershipDeniedError(error: unknown): boolean {
66-
if (!(error instanceof Error)) return false;
67-
return [
68-
"You must be a relay member",
69-
"relay_membership_required",
70-
"restricted: not a relay member",
71-
"invalid: you are not a relay member",
72-
].some((message) => error.message.includes(message));
73-
}
74-
7564
type BootSplashPhase = "holding" | "fading" | "done";
7665

7766
function useInitialRenderReady() {
@@ -275,13 +264,19 @@ function CommunityApp({
275264
}) {
276265
const {
277266
activeCommunity,
267+
communities,
278268
reinitKey,
279269
addCommunity,
270+
clearCommunities,
271+
removeCommunity,
280272
switchCommunity,
281273
reconnectCommunity,
282274
} = useCommunities();
283275
const communityOnboarding = useCommunityOnboarding();
276+
const connectingTransactionRef = useRef<string | null>(null);
284277
const [isCommunityChangeOpen, setIsCommunityChangeOpen] = useState(false);
278+
const [resumeFirstCommunityJoin, setResumeFirstCommunityJoin] =
279+
useState(false);
285280

286281
// Surface nest-related backend events (repos-dir errors, legacy migration)
287282
// as toasts. Mounted before useCommunityInit so the listeners are registered
@@ -311,10 +306,16 @@ function CommunityApp({
311306
const handleCommunityOnboardingConnect = useCallback(() => {
312307
const transaction = communityOnboarding.transaction;
313308
if (transaction?.stage !== "connecting") return;
309+
if (connectingTransactionRef.current === transaction.id) return;
310+
connectingTransactionRef.current = transaction.id;
314311
if (transaction.communityId) {
315312
switchCommunity(transaction.communityId);
316313
return;
317314
}
315+
const previousCommunityId = activeCommunity?.id;
316+
const relayAlreadyExists = communities.some(
317+
(community) => community.relayUrl === transaction.relayUrl,
318+
);
318319
const id = addCommunity({
319320
id: crypto.randomUUID(),
320321
name: transaction.communityName,
@@ -323,58 +324,70 @@ function CommunityApp({
323324
reposDir: transaction.reposDir,
324325
addedAt: new Date().toISOString(),
325326
});
326-
communityOnboarding.update({ communityId: id, error: undefined });
327+
communityOnboarding.update({
328+
communityId: id,
329+
previousCommunityId,
330+
addedCommunity: !relayAlreadyExists,
331+
error: undefined,
332+
});
327333
switchCommunity(id);
328334
reconnectCommunity();
329-
}, [addCommunity, communityOnboarding, reconnectCommunity, switchCommunity]);
335+
}, [
336+
activeCommunity?.id,
337+
addCommunity,
338+
communities,
339+
communityOnboarding,
340+
reconnectCommunity,
341+
switchCommunity,
342+
]);
343+
344+
const handleCommunityOnboardingCancel = useCallback(() => {
345+
const transaction = communityOnboarding.transaction;
346+
communityOnboarding.clear();
347+
348+
if (!transaction?.communityId) return;
349+
if (!transaction.addedCommunity) {
350+
if (transaction.previousCommunityId) {
351+
switchCommunity(transaction.previousCommunityId);
352+
}
353+
return;
354+
}
355+
if (communities.length === 1) {
356+
if (transaction.source === "first-community") {
357+
setResumeFirstCommunityJoin(true);
358+
}
359+
clearCommunities();
360+
return;
361+
}
362+
removeCommunity(transaction.communityId);
363+
if (transaction.previousCommunityId) {
364+
switchCommunity(transaction.previousCommunityId);
365+
}
366+
}, [
367+
clearCommunities,
368+
communities.length,
369+
communityOnboarding,
370+
removeCommunity,
371+
switchCommunity,
372+
]);
330373

331374
const bootSplashPhase = useBootSplashHold();
332375

333376
const transaction = communityOnboarding.transaction;
377+
useEffect(() => {
378+
if (transaction?.stage !== "connecting") {
379+
connectingTransactionRef.current = null;
380+
}
381+
}, [transaction?.stage]);
334382
const targetIsReady =
335383
transaction?.communityId === activeCommunity?.id &&
336384
community.isReady &&
337385
community.appliedKey === communityKey;
338386
useEffect(() => {
339-
if (
340-
transaction?.stage !== "connecting" ||
341-
transaction.error ||
342-
!targetIsReady
343-
) {
344-
return;
387+
if (transaction?.stage === "connecting" && targetIsReady) {
388+
communityOnboarding.update({ stage: "profile", error: undefined });
345389
}
346-
347-
let cancelled = false;
348-
void getMyRelayMembershipLookup()
349-
.then(({ membership, snapshotFound }) => {
350-
if (cancelled) return;
351-
if (snapshotFound && membership === null) {
352-
communityOnboarding.update({
353-
error:
354-
"You have not been added to this community yet. Ask the host to add your public key, then try again.",
355-
});
356-
return;
357-
}
358-
communityOnboarding.update({ stage: "profile", error: undefined });
359-
})
360-
.catch((error: unknown) => {
361-
if (cancelled) return;
362-
communityOnboarding.update({
363-
error: isRelayMembershipDeniedError(error)
364-
? "You have not been added to this community yet. Ask the host to add your public key, then try again."
365-
: "Could not check community access. Check the URL and try again.",
366-
});
367-
});
368-
369-
return () => {
370-
cancelled = true;
371-
};
372-
}, [
373-
communityOnboarding.update,
374-
targetIsReady,
375-
transaction?.error,
376-
transaction?.stage,
377-
]);
390+
}, [communityOnboarding.update, targetIsReady, transaction?.stage]);
378391
// During "entering" the transaction stays alive as a curtain: the app mounts
379392
// underneath (already pointed at the Welcome channel route) while the
380393
// onboarding screen covers it, then fades once Welcome reports ready.
@@ -398,6 +411,7 @@ function CommunityApp({
398411
appContent = (
399412
<WelcomeSetup
400413
defaultRelayUrl={community.defaultRelayUrl}
414+
initialPage={resumeFirstCommunityJoin ? "join" : undefined}
401415
onBack={onBackToMachineConfig}
402416
/>
403417
);
@@ -465,6 +479,7 @@ function CommunityApp({
465479
}
466480
>
467481
<CommunityOnboardingFlow
482+
onCancel={handleCommunityOnboardingCancel}
468483
onConnect={handleCommunityOnboardingConnect}
469484
/>
470485
</div>

‎desktop/src/features/communities/ui/WelcomeSetup.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type WelcomeTransitionMode = "initial" | OnboardingTransitionDirection;
3232

3333
type WelcomeSetupProps = {
3434
defaultRelayUrl: string;
35+
initialPage?: WelcomeSetupPage;
3536
initialTransitionMode?: WelcomeTransitionMode;
3637
onBack: () => void;
3738
};
@@ -50,10 +51,11 @@ function isLocalDevRelayUrl(relayUrl: string) {
5051

5152
export function WelcomeSetup({
5253
defaultRelayUrl,
54+
initialPage = "welcome",
5355
initialTransitionMode = "initial",
5456
onBack,
5557
}: WelcomeSetupProps) {
56-
const [page, setPage] = React.useState<WelcomeSetupPage>("welcome");
58+
const [page, setPage] = React.useState<WelcomeSetupPage>(initialPage);
5759
const [transitionMode, setTransitionMode] =
5860
React.useState<WelcomeTransitionMode>(initialTransitionMode);
5961
const [npub, setNpub] = React.useState("");

‎desktop/src/features/onboarding/communityOnboarding.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type CommunityOnboardingTransaction = {
4242
*/
4343
policyReceipt?: string;
4444
communityId?: string;
45+
previousCommunityId?: string;
46+
addedCommunity?: boolean;
4547
createdAt: string;
4648
updatedAt: string;
4749
error?: string;
@@ -56,6 +58,8 @@ export type CommunityOnboardingTransactionPatch = Partial<
5658
| "stage"
5759
| "relayUrl"
5860
| "communityId"
61+
| "previousCommunityId"
62+
| "addedCommunity"
5963
| "communityName"
6064
| "error"
6165
| "acknowledged"

‎desktop/src/features/onboarding/ui/CommunityOnboardingFlow.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ function AvatarCircle({
113113
}
114114

115115
export function CommunityOnboardingFlow({
116+
onCancel,
116117
onConnect,
117118
}: {
119+
onCancel: () => void;
118120
onConnect: () => void;
119121
}) {
120122
const { transaction, update, clear } = useCommunityOnboarding();
@@ -372,7 +374,7 @@ export function CommunityOnboardingFlow({
372374
) : null}
373375
<Button
374376
className="rounded-full bg-foreground/10 px-5 hover:bg-foreground/15"
375-
onClick={clear}
377+
onClick={onCancel}
376378
variant="ghost"
377379
>
378380
Cancel

‎desktop/src/features/onboarding/ui/OnboardingFlow.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
persistCurrentIdentity,
1414
} from "@/shared/api/tauriIdentity";
1515
import { useSystemColorScheme } from "@/shared/theme/useSystemColorScheme";
16-
import { forceFreshOnboarding } from "@/features/onboarding/devFreshOnboarding";
1716
import { Button } from "@/shared/ui/button";
1817
import { StartupWindowDragRegion } from "@/shared/ui/StartupWindowDragRegion";
1918
import { AvatarStep } from "./AvatarStep";
@@ -513,7 +512,7 @@ export function OnboardingFlow({
513512
}}
514513
direction={transitionDirection}
515514
state={profileStepState}
516-
usesExistingIdentity={forceFreshOnboarding}
515+
usesExistingIdentity
517516
/>
518517
) : currentPage === "key-import" ? (
519518
<OnboardingSlideTransition

‎desktop/tests/e2e/onboarding-avatar-skip.spec.ts‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { hexToBytes } from "@noble/hashes/utils.js";
21
import { expect, test } from "@playwright/test";
3-
import { nsecEncode } from "nostr-tools/nip19";
42

53
import { installMockBridge, TEST_IDENTITIES } from "../helpers/bridge";
64
import { waitForAnimations } from "../helpers/animations";
@@ -82,36 +80,19 @@ test("avatar Next button still requires an avatar to be chosen", async ({
8280
// B4: Routing tests
8381
// ---------------------------------------------------------------------------
8482

85-
test("import-key path skips backup and goes directly to avatar", async ({
86-
page,
87-
}) => {
88-
// Import tyler's OWN key (same pubkey = no component remount) so the
89-
// identityWasImported flag persists in the same component instance.
83+
test("normal profile setup keeps the existing identity", async ({ page }) => {
9084
await seedActiveIdentity(page, BLANK_TYLER_IDENTITY);
9185
await installMockBridge(page, undefined, { skipOnboardingSeed: true });
9286
await page.goto("/");
9387

94-
// Profile page — click "Use existing key" to open the key import form.
95-
await expect(page.getByTestId("onboarding-page-1")).toBeVisible();
96-
await page.getByTestId("onboarding-import-key").click();
97-
await expect(
98-
page.getByRole("heading", { name: "Use your existing key" }),
99-
).toBeVisible();
100-
101-
// Enter tyler's own nsec (same pubkey → no remount, identityWasImported stays true).
102-
const tylerNsec = nsecEncode(hexToBytes(TEST_IDENTITIES.tyler.privateKey));
103-
await page.getByTestId("nostr-import-nsec-input").fill(tylerNsec);
104-
await expect(page.getByTestId("nostr-import-npub-preview")).toBeVisible();
105-
await page.getByTestId("nostr-import-submit").click();
106-
107-
// After import, the flow returns to profile with identityWasImported=true.
10888
await expect(page.getByTestId("onboarding-page-1")).toBeVisible();
89+
await expect(page.getByTestId("onboarding-import-key")).toHaveCount(0);
90+
await expect(page.getByText("Create an identity key")).toHaveCount(0);
91+
10992
await page.getByTestId("onboarding-display-name").fill("Morty QA");
11093
await page.getByTestId("onboarding-next").click();
11194

112-
// Backup page must NOT appear — avatar comes next on the import path.
11395
await expect(page.getByTestId("onboarding-page-avatar")).toBeVisible();
114-
await expect(page.getByTestId("onboarding-page-backup")).not.toBeVisible();
11596
});
11697

11798
test("Back from the community avatar step returns to profile", async ({

0 commit comments

Comments
 (0)