Skip to content

Commit 2510a4b

Browse files
matt2eclaudewesbillmanPinky
authored
feat(desktop): confirm invites with an Opening-invite loading gate before machine onboarding (#1950)
Signed-off-by: Matt Toohey <[email protected]> Signed-off-by: Wes <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent 7eea924 commit 2510a4b

11 files changed

Lines changed: 396 additions & 49 deletions

File tree

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default defineConfig({
9393
"**/buzz-theme-screenshots.spec.ts",
9494
"**/channel-sort.spec.ts",
9595
"**/identity-lost.spec.ts",
96+
"**/deep-link-invite.spec.ts",
9697
"**/global-agent-config-screenshots.spec.ts",
9798
"**/doctor-states.spec.ts",
9899
"**/onboarding-avatar-skip.spec.ts",

‎desktop/src/app/App.tsx‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useCommunityOnboarding } from "@/features/onboarding/communityOnboardin
2121
import { CommunityOnboardingFlow } from "@/features/onboarding/ui/CommunityOnboardingFlow";
2222
import { MachineOnboardingFlow } from "@/features/onboarding/ui/MachineOnboardingFlow";
2323
import { OnboardingFlow } from "@/features/onboarding/ui/OnboardingFlow";
24+
import { PendingInviteGate } from "@/features/onboarding/ui/PendingInviteGate";
2425
import { KeyringLockedScreen } from "@/features/onboarding/ui/KeyringLockedScreen";
2526
import { RelaunchRequiredScreen } from "@/features/onboarding/ui/RelaunchRequiredScreen";
2627
import { ResetFailedScreen } from "@/features/onboarding/ui/ResetFailedScreen";
@@ -255,14 +256,6 @@ function CommunityApp({ sharedIdentity }: { sharedIdentity: boolean }) {
255256
const communityOnboarding = useCommunityOnboarding();
256257
const [isCommunityChangeOpen, setIsCommunityChangeOpen] = useState(false);
257258

258-
useEffect(() => {
259-
const unlisten = listenForDeepLinks({
260-
startCommunityOnboarding: communityOnboarding.start,
261-
});
262-
return () => {
263-
void unlisten.then((fn) => fn());
264-
};
265-
}, [communityOnboarding.start]);
266259
// Surface nest-related backend events (repos-dir errors, legacy migration)
267260
// as toasts. Mounted before useCommunityInit so the listeners are registered
268261
// ahead of the first apply_workspace call.
@@ -389,11 +382,26 @@ function CommunityApp({ sharedIdentity }: { sharedIdentity: boolean }) {
389382

390383
function MachineBootstrap({ sharedIdentity }: { sharedIdentity: boolean }) {
391384
const { activeCommunity } = useCommunities();
385+
const communityOnboarding = useCommunityOnboarding();
392386
const machine = useMachineOnboardingState({
393387
hasConfiguredCommunity: activeCommunity !== null,
394388
isSharedIdentity: sharedIdentity,
395389
});
396390

391+
// Deep links are captured here — above the machine-onboarding gate — not in
392+
// CommunityApp. The Rust side queues them; draining into the persisted
393+
// community-onboarding transaction immediately means an invite opened on a
394+
// fresh install is acknowledged on screen while the identity steps are
395+
// still pending, and survives a relaunch in between.
396+
useEffect(() => {
397+
const unlisten = listenForDeepLinks({
398+
startCommunityOnboarding: communityOnboarding.start,
399+
});
400+
return () => {
401+
void unlisten.then((fn) => fn());
402+
};
403+
}, [communityOnboarding.start]);
404+
397405
if (machine.stage === "reset-failed") return <ResetFailedScreen />;
398406
if (machine.stage === "keyring-locked") return <KeyringLockedScreen />;
399407
if (machine.stage === "relaunch-required") return <RelaunchRequiredScreen />;
@@ -402,12 +410,24 @@ function MachineBootstrap({ sharedIdentity }: { sharedIdentity: boolean }) {
402410
return <CommunityApp sharedIdentity={sharedIdentity} />;
403411
}
404412

413+
// A community deep link that arrived before machine onboarding finished is
414+
// persisted immediately and acknowledged here. Invite claiming waits until
415+
// setup completes so it is signed only by the user's final identity.
416+
const transaction = communityOnboarding.transaction;
417+
const isDeepLink =
418+
transaction?.source === "deep-link-join" ||
419+
transaction?.source === "deep-link-connect";
420+
const shouldAcknowledgeDeepLink = isDeepLink && !transaction.acknowledged;
421+
405422
return (
406-
<MachineOnboardingFlow
407-
complete={machine.complete}
408-
identityLost={machine.identityLost}
409-
queryClient={machine.queryClient}
410-
/>
423+
<>
424+
<MachineOnboardingFlow
425+
complete={machine.complete}
426+
identityLost={machine.identityLost}
427+
queryClient={machine.queryClient}
428+
/>
429+
{shouldAcknowledgeDeepLink ? <PendingInviteGate /> : null}
430+
</>
411431
);
412432
}
413433

‎desktop/src/features/onboarding/communityOnboarding.test.mjs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
markCommunityOnboardingComplete,
88
startCommunityOnboarding,
99
updateCommunityOnboardingTransaction,
10+
updateCurrentCommunityOnboardingTransaction,
1011
} from "./communityOnboarding.tsx";
1112

1213
function createMemoryStorage(initial = {}) {
@@ -79,6 +80,53 @@ test("same-relay ingress resumes rather than replacing progress", () => {
7980
assert.equal(resumed.inviteCode, "new-code");
8081
});
8182

83+
test("stale asynchronous updates cannot mutate a replacement transaction", () => {
84+
const storage = createMemoryStorage();
85+
const original = startCommunityOnboarding(
86+
{
87+
source: "deep-link-join",
88+
relayUrl: "wss://relay.example",
89+
inviteCode: "invite-code",
90+
},
91+
storage,
92+
);
93+
const replacement = startCommunityOnboarding(
94+
{ source: "deep-link-connect", relayUrl: "wss://other.example" },
95+
storage,
96+
);
97+
98+
const result = updateCurrentCommunityOnboardingTransaction(
99+
replacement,
100+
{ stage: "connecting", error: "stale error" },
101+
original.id,
102+
storage,
103+
);
104+
105+
assert.equal(result, replacement);
106+
assert.equal(loadCommunityOnboardingTransaction(storage)?.id, replacement.id);
107+
assert.equal(loadCommunityOnboardingTransaction(storage)?.error, undefined);
108+
});
109+
110+
test("acknowledgment persists but resets when the same-relay link reopens", () => {
111+
const storage = createMemoryStorage();
112+
const transaction = startCommunityOnboarding(
113+
{ source: "deep-link-connect", relayUrl: "wss://relay.example" },
114+
storage,
115+
);
116+
assert.equal(transaction.stage, "connecting");
117+
updateCommunityOnboardingTransaction(
118+
transaction,
119+
{ acknowledged: true },
120+
storage,
121+
);
122+
assert.equal(loadCommunityOnboardingTransaction(storage)?.acknowledged, true);
123+
const reopened = startCommunityOnboarding(
124+
{ source: "deep-link-connect", relayUrl: "wss://relay.example" },
125+
storage,
126+
);
127+
assert.equal(reopened.acknowledged, undefined);
128+
});
129+
82130
test("malformed persisted state is ignored and can be cleared", () => {
83131
const storage = createMemoryStorage({
84132
"buzz-community-onboarding-transaction.v1": '{"stage":"profile"}',

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ export type CommunityOnboardingTransaction = {
3333
createdAt: string;
3434
updatedAt: string;
3535
error?: string;
36+
// Deep links are persisted before machine onboarding completes. Set when
37+
// the user dismisses the acknowledgment so it stays dismissed on relaunch.
38+
acknowledged?: boolean;
3639
};
3740

41+
export type CommunityOnboardingTransactionPatch = Partial<
42+
Pick<
43+
CommunityOnboardingTransaction,
44+
"stage" | "communityId" | "communityName" | "error" | "acknowledged"
45+
>
46+
>;
47+
3848
export type StartCommunityOnboardingInput = {
3949
source: CommunityOnboardingSource;
4050
relayUrl: string;
@@ -119,6 +129,9 @@ export function startCommunityOnboarding(
119129
reposDir: input.reposDir ?? existing.reposDir,
120130
updatedAt: now.toISOString(),
121131
error: undefined,
132+
// A freshly opened link deserves fresh feedback — re-present the gate
133+
// even if a previous link for this relay was already dismissed.
134+
acknowledged: undefined,
122135
};
123136
saveCommunityOnboardingTransaction(updated, storage);
124137
return updated;
@@ -143,12 +156,7 @@ export function startCommunityOnboarding(
143156

144157
export function updateCommunityOnboardingTransaction(
145158
transaction: CommunityOnboardingTransaction,
146-
patch: Partial<
147-
Pick<
148-
CommunityOnboardingTransaction,
149-
"stage" | "communityId" | "communityName" | "error"
150-
>
151-
>,
159+
patch: CommunityOnboardingTransactionPatch,
152160
storage: Storage = localStorage,
153161
now = new Date(),
154162
): CommunityOnboardingTransaction {
@@ -157,6 +165,17 @@ export function updateCommunityOnboardingTransaction(
157165
return updated;
158166
}
159167

168+
export function updateCurrentCommunityOnboardingTransaction(
169+
current: CommunityOnboardingTransaction | null,
170+
patch: CommunityOnboardingTransactionPatch,
171+
expectedId: string | undefined,
172+
storage: Storage = localStorage,
173+
now = new Date(),
174+
): CommunityOnboardingTransaction | null {
175+
if (!current || (expectedId && current.id !== expectedId)) return current;
176+
return updateCommunityOnboardingTransaction(current, patch, storage, now);
177+
}
178+
160179
export function markCommunityOnboardingComplete(
161180
pubkey: string,
162181
relayUrl: string,
@@ -177,12 +196,8 @@ type CommunityOnboardingContextValue = {
177196
transaction: CommunityOnboardingTransaction | null;
178197
start: (input: StartCommunityOnboardingInput) => boolean;
179198
update: (
180-
patch: Partial<
181-
Pick<
182-
CommunityOnboardingTransaction,
183-
"stage" | "communityId" | "communityName" | "error"
184-
>
185-
>,
199+
patch: CommunityOnboardingTransactionPatch,
200+
expectedId?: string,
186201
) => void;
187202
clear: () => void;
188203
};
@@ -212,18 +227,9 @@ export function CommunityOnboardingProvider({
212227
[transaction],
213228
);
214229
const update = React.useCallback(
215-
(
216-
patch: Partial<
217-
Pick<
218-
CommunityOnboardingTransaction,
219-
"stage" | "communityId" | "communityName" | "error"
220-
>
221-
>,
222-
) => {
230+
(patch: CommunityOnboardingTransactionPatch, expectedId?: string) => {
223231
setTransaction((current) =>
224-
current
225-
? updateCommunityOnboardingTransaction(current, patch)
226-
: current,
232+
updateCurrentCommunityOnboardingTransaction(current, patch, expectedId),
227233
);
228234
},
229235
[],

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
useCommunityOnboarding,
88
} from "@/features/onboarding/communityOnboarding";
99
import { initializeWelcomeChannel } from "@/features/onboarding/hooks";
10+
import { useClaimInvite } from "@/features/onboarding/useClaimInvite";
1011
import { AvatarUpload } from "@/features/profile/ui/AvatarUpload";
1112
import { ProfileAvatar } from "@/features/profile/ui/ProfileAvatar";
12-
import { claimInvite } from "@/shared/api/invites";
13-
import { inviteErrorMessage } from "@/shared/api/inviteHelpers";
1413
import { updateProfile } from "@/shared/api/tauriProfiles";
1514
import { getIdentity } from "@/shared/api/tauriIdentity";
1615
import { listPersonas } from "@/shared/api/tauriPersonas";
@@ -50,19 +49,11 @@ export function CommunityOnboardingFlow({
5049
.catch(() => setStarterPersonas([]));
5150
}, [transaction?.stage]);
5251

53-
React.useEffect(() => {
54-
if (transaction?.stage !== "claiming" || isPending) return;
55-
setIsPending(true);
56-
void claimInvite(transaction.relayUrl, transaction.inviteCode ?? "")
57-
.then(() => update({ stage: "connecting", error: undefined }))
58-
.catch((error: unknown) => update({ error: inviteErrorMessage(error) }))
59-
.finally(() => setIsPending(false));
60-
}, [isPending, transaction, update]);
52+
useClaimInvite();
6153

6254
React.useEffect(() => {
63-
if (transaction?.stage !== "connecting") return;
64-
onConnect();
65-
}, [onConnect, transaction]);
55+
if (transaction?.stage === "connecting") onConnect();
56+
}, [onConnect, transaction?.stage]);
6657

6758
const retryClaim = () => update({ stage: "claiming", error: undefined });
6859
const relayUrl = transaction?.relayUrl;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useCommunityOnboarding } from "@/features/onboarding/communityOnboarding";
2+
import { Button } from "@/shared/ui/button";
3+
import { FlappingBee } from "@/shared/ui/buzz-logo/FlappingBee";
4+
5+
/**
6+
* Acknowledge a community deep link received before machine onboarding is
7+
* complete. The transaction is already persisted; claiming and connecting
8+
* wait until setup finishes so only the user's final identity is admitted.
9+
*/
10+
export function PendingInviteGate() {
11+
const { transaction, update, clear } = useCommunityOnboarding();
12+
13+
if (!transaction) return null;
14+
15+
return (
16+
<div
17+
className="buzz-onboarding-neutral-theme fixed inset-0 z-50 flex items-center justify-center bg-background px-4 py-8 text-foreground"
18+
data-testid="pending-invite-gate"
19+
>
20+
<div className="flex w-full max-w-[440px] flex-col items-center text-center">
21+
<FlappingBee className="h-auto w-24" />
22+
<h1 className="mt-6 text-3xl font-semibold tracking-tight">
23+
Opening community link
24+
</h1>
25+
<p className="mt-3 text-sm leading-6 text-muted-foreground">
26+
You’ll connect to {transaction.communityName} once setup is finished.
27+
</p>
28+
<div className="mt-8 flex w-full flex-col gap-3">
29+
<Button
30+
className="h-10 w-full"
31+
data-testid="pending-invite-continue"
32+
onClick={() => update({ acknowledged: true })}
33+
type="button"
34+
>
35+
Continue setup
36+
</Button>
37+
<Button
38+
className="h-10 w-full"
39+
data-testid="pending-invite-cancel"
40+
onClick={clear}
41+
type="button"
42+
variant="ghost"
43+
>
44+
Cancel
45+
</Button>
46+
</div>
47+
</div>
48+
</div>
49+
);
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as React from "react";
2+
3+
import { useCommunityOnboarding } from "@/features/onboarding/communityOnboarding";
4+
import { inviteErrorMessage } from "@/shared/api/inviteHelpers";
5+
import { claimInvite } from "@/shared/api/invites";
6+
7+
/**
8+
* Drive the `claiming` stage after machine onboarding completes: claim the
9+
* invite with the user's final identity, then advance to `connecting`.
10+
* Completion is fenced by transaction ID so cancelling or replacing the
11+
* transaction while the request is pending cannot mutate the replacement.
12+
*
13+
* The error guard keeps a failed claim parked on the caller's Retry
14+
* affordance — without it the effect refires on the error-bearing transaction
15+
* and re-claims in a loop.
16+
*/
17+
export function useClaimInvite() {
18+
const { transaction, update } = useCommunityOnboarding();
19+
const [isPending, setIsPending] = React.useState(false);
20+
21+
React.useEffect(() => {
22+
if (transaction?.stage !== "claiming" || transaction.error || isPending) {
23+
return;
24+
}
25+
setIsPending(true);
26+
void claimInvite(transaction.relayUrl, transaction.inviteCode ?? "")
27+
.then(() => {
28+
update({ stage: "connecting", error: undefined }, transaction.id);
29+
})
30+
.catch((error: unknown) =>
31+
update({ error: inviteErrorMessage(error) }, transaction.id),
32+
)
33+
.finally(() => setIsPending(false));
34+
}, [isPending, transaction, update]);
35+
}

‎desktop/src/shared/api/invites.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import { getRelayHttpUrl, signRelayEvent } from "@/shared/api/tauri";
1212

1313
const NIP98_KIND = 27235;
1414

15+
// Bound invite requests so an unreachable relay surfaces as an error in the
16+
// invite-loading UI within seconds instead of hanging for the OS-level
17+
// connect timeout (a minute or more on macOS).
18+
const INVITE_REQUEST_TIMEOUT_MS = 15_000;
19+
1520
export type MintedInvite = {
1621
code: string;
1722
expiresAt: number;
@@ -71,6 +76,7 @@ async function invitePost<T>(
7176
"Content-Type": "application/json",
7277
},
7378
body,
79+
signal: AbortSignal.timeout(INVITE_REQUEST_TIMEOUT_MS),
7480
});
7581
const json = (await response.json().catch(() => ({}))) as Record<
7682
string,

0 commit comments

Comments
 (0)