@@ -81,8 +81,11 @@ export function loadCommunities(): Community[] {
8181 }
8282}
8383
84- export function saveCommunities ( communities : Community [ ] ) : void {
85- setLocalStorageItemWithRecovery ( COMMUNITIES_KEY , JSON . stringify ( communities ) ) ;
84+ export function saveCommunities ( communities : Community [ ] ) : boolean {
85+ return setLocalStorageItemWithRecovery (
86+ COMMUNITIES_KEY ,
87+ JSON . stringify ( communities ) ,
88+ ) ;
8689}
8790
8891export function clearCommunityStorage ( storage : Storage = localStorage ) : void {
@@ -97,8 +100,8 @@ export function loadActiveCommunityId(): string | null {
97100 return localStorage . getItem ( ACTIVE_COMMUNITY_KEY ) ;
98101}
99102
100- export function saveActiveCommunityId ( id : string ) : void {
101- setLocalStorageItemWithRecovery ( ACTIVE_COMMUNITY_KEY , id ) ;
103+ export function saveActiveCommunityId ( id : string ) : boolean {
104+ return setLocalStorageItemWithRecovery ( ACTIVE_COMMUNITY_KEY , id ) ;
102105}
103106
104107export function normalizeRelayUrl ( url : string ) : string {
@@ -108,13 +111,29 @@ export function normalizeRelayUrl(url: string): string {
108111 return url ;
109112}
110113
114+ function isLocalRelayHost ( hostname : string ) : boolean {
115+ return [ "localhost" , "127.0.0.1" , "[::1]" , "0.0.0.0" ] . includes ( hostname ) ;
116+ }
117+
118+ export function shouldAutoConnectDefaultRelay ( relayUrl : string ) : boolean {
119+ try {
120+ const parsed = new URL ( relayUrl ) ;
121+ return (
122+ ( parsed . protocol === "ws:" || parsed . protocol === "wss:" ) &&
123+ ! isLocalRelayHost ( parsed . hostname )
124+ ) ;
125+ } catch {
126+ return false ;
127+ }
128+ }
129+
111130export function deriveCommunityName ( relayUrl : string ) : string {
112131 try {
113132 const url = new URL (
114133 relayUrl . replace ( "ws://" , "http://" ) . replace ( "wss://" , "https://" ) ,
115134 ) ;
116135 const host = url . hostname ;
117- if ( host === "localhost" || host === "127.0.0.1" ) {
136+ if ( isLocalRelayHost ( host ) ) {
118137 return "Local Dev" ;
119138 }
120139 const parts = host . split ( "." ) ;
@@ -136,17 +155,40 @@ export function initFirstCommunity(
136155 relayUrl : string ,
137156 pubkey : string ,
138157 name ?: string ,
139- ) : Community {
158+ ) : Community | null {
140159 const normalizedUrl = normalizeRelayUrl ( relayUrl ) ;
141160 const trimmedName = name ?. trim ( ) ;
142161 const community : Community = {
143162 id : crypto . randomUUID ( ) ,
144163 name : trimmedName || deriveCommunityName ( normalizedUrl ) ,
145164 relayUrl : normalizedUrl ,
165+ // Compiled default relays must admit the first token-less connection; there
166+ // is no invite-token prompt on this auto-connect path.
146167 pubkey,
147168 addedAt : new Date ( ) . toISOString ( ) ,
148169 } ;
149- saveCommunities ( [ community ] ) ;
150- saveActiveCommunityId ( community . id ) ;
170+ const previousActiveCommunityId = localStorage . getItem ( ACTIVE_COMMUNITY_KEY ) ;
171+ const didSaveActiveCommunity = saveActiveCommunityId ( community . id ) ;
172+ if ( ! didSaveActiveCommunity ) {
173+ return null ;
174+ }
175+
176+ if ( ! saveCommunities ( [ community ] ) ) {
177+ // A failed setItem leaves the existing communities value untouched. Roll
178+ // back only the active-ID write so inconsistent pre-existing data is never
179+ // destroyed while recovering from a quota failure.
180+ try {
181+ if ( previousActiveCommunityId === null ) {
182+ localStorage . removeItem ( ACTIVE_COMMUNITY_KEY ) ;
183+ } else {
184+ localStorage . setItem ( ACTIVE_COMMUNITY_KEY , previousActiveCommunityId ) ;
185+ }
186+ } catch {
187+ // Best effort: persistence is already unavailable, and callers will stay
188+ // on setup instead of reloading.
189+ }
190+ return null ;
191+ }
192+
151193 return community ;
152194}
0 commit comments