11import { DEFAULT_IDENTITY_NAME } from '@/src/common/constants' ;
22import {
33 PolycentricClient ,
4- createIdentityWithDefaultServer ,
54 createPolycentricClient ,
65 types ,
76 v2 ,
7+ type IdentityState ,
88} from '@polycentric/react-native' ;
9-
10- type Identity = v2 . Identity ;
119import {
1210 createContext ,
1311 useCallback ,
@@ -31,7 +29,7 @@ export interface PolycentricContextValue {
3129 isLoading : boolean ;
3230 isReady : boolean ;
3331 error : Error | null ;
34- currentIdentity : Identity | null ;
32+ currentIdentity : IdentityState | null ;
3533 switchIdentity : ( publicKey : types . PublicKey ) => Promise < void > ;
3634}
3735
@@ -107,9 +105,10 @@ function DefaultErrorComponent({ error }: { error: Error }) {
107105
108106async function resolveIdentity (
109107 client : PolycentricClient ,
110- ) : Promise < Identity | null > {
108+ ) : Promise < IdentityState | null > {
109+ if ( ! client . activeIdentityKey ) return null ;
111110 const state = await client . identityManager . getCurrent ( ) ;
112- return state ?? null ;
111+ return state . identityKey ? state : null ;
113112}
114113
115114export function PolycentricProvider ( {
@@ -119,7 +118,9 @@ export function PolycentricProvider({
119118} : PolycentricProviderProps ) {
120119 const [ client , setClient ] = useState < PolycentricClient | null > ( null ) ;
121120 const [ store , setStore ] = useState < PolycentricStoreApi | null > ( null ) ;
122- const [ currentIdentity , setCurrentIdentity ] = useState < Identity | null > ( null ) ;
121+ const [ currentIdentity , setCurrentIdentity ] = useState < IdentityState | null > (
122+ null ,
123+ ) ;
123124 const [ isLoading , setIsLoading ] = useState ( true ) ;
124125 const [ error , setError ] = useState < Error | null > ( null ) ;
125126
@@ -134,18 +135,15 @@ export function PolycentricProvider({
134135
135136 ( async ( ) => {
136137 try {
138+ // PolycentricClient.initialize() guarantees a keypair exists on
139+ // every device. Identity (the published Identity doc) is a separate
140+ // concept — the onboarding gate handles creating or pairing one.
137141 const c = await createPolycentricClient ( {
138142 databaseName : 'polycentric.db' ,
139143 } ) ;
140144
141145 if ( cancelled ) return ;
142146
143- if ( ( await c . keyPairManager . getKeys ( ) ) . length === 0 ) {
144- await createIdentityWithDefaultServer ( c , DEFAULT_SERVER ) ;
145- }
146-
147- if ( cancelled ) return ;
148-
149147 const s = createPolycentricStore ( c ) ;
150148 await s . getState ( ) . refreshIdentities ( ) ;
151149
@@ -154,19 +152,26 @@ export function PolycentricProvider({
154152 setCurrentIdentity ( await resolveIdentity ( c ) ) ;
155153 setIsLoading ( false ) ;
156154
157- void c . sync ( ) . catch ( ( syncError ) => {
158- console . warn ( 'Initial Polycentric sync failed:' , syncError ) ;
159- } ) ;
155+ // Only sync when we already have an identity to sync for.
156+ if ( c . activeIdentityKey ) {
157+ void c . sync ( ) . catch ( ( syncError ) => {
158+ console . warn ( 'Initial Polycentric sync failed:' , syncError ) ;
159+ } ) ;
160+ }
160161
161162 c . events . onKeyPairChanged ( async ( ) => {
162163 if ( cancelled ) return ;
163- if ( ( await c . keyPairManager . getKeys ( ) ) . length === 0 ) {
164- await createIdentityWithDefaultServer ( c , DEFAULT_SERVER ) ;
165- await c . sync ( ) . catch ( ( ) => { } ) ;
166- }
167164 setCurrentIdentity ( await resolveIdentity ( c ) ) ;
168165 await s . getState ( ) . refreshIdentities ( ) ;
169166 } ) ;
167+
168+ // Identity onboarding (create / claim) publishes an Identity event,
169+ // which flows through onContentCreated. Re-resolve so the gate
170+ // flips from onboarding → app once the user completes signup.
171+ c . events . onContentCreated ( async ( ) => {
172+ if ( cancelled ) return ;
173+ setCurrentIdentity ( await resolveIdentity ( c ) ) ;
174+ } ) ;
170175 } catch ( err ) {
171176 if ( ! cancelled ) {
172177 console . error ( 'Failed to initialize PolycentricProvider:' , err ) ;
0 commit comments