@@ -39,7 +39,6 @@ import { WelcomeSetup } from "@/features/communities/ui/WelcomeSetup";
3939import { CommunityApplyErrorScreen } from "@/features/communities/ui/CommunityApplyErrorScreen" ;
4040import { CommunityChangeOverlay } from "@/features/communities/ui/CommunityChangeOverlay" ;
4141import { createBuzzQueryClient } from "@/shared/api/queryClient" ;
42- import { getMyRelayMembershipLookup } from "@/shared/api/relayMembers" ;
4342import { isSharedIdentity as isSharedIdentityCmd } from "@/shared/api/tauri" ;
4443import {
4544 type AddCommunityDeepLinkPayload ,
@@ -62,16 +61,6 @@ const BOOT_SPLASH_MIN_VISIBLE_MS = 1_200;
6261const BOOT_SPLASH_FADE_MS = 200 ;
6362const 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-
7564type BootSplashPhase = "holding" | "fading" | "done" ;
7665
7766function 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 >
0 commit comments