@@ -93,48 +93,94 @@ export const PaidSubscribers: React.FC = () => {
9393 } ;
9494
9595 useEffect ( ( ) => {
96- // Fetch profiles for test subscribers
96+ // Implement hybrid profile fetching: NDK first, fallback to backend data
9797 if ( useDummyData ) {
9898 console . warn ( '[PaidSubscribers] Using dummy data, skipping profile fetch' ) ;
9999 setLoadingProfiles ( false ) ;
100100 return ;
101101 }
102+
102103 const fetchProfiles = async ( ) => {
103104 if ( ! ndkInstance || ! ndkInstance . ndk ) {
104- console . error ( 'NDK instance is not initialized' ) ;
105+ console . error ( '[PaidSubscribers] NDK instance is not initialized, using backend data only' ) ;
106+ setLoadingProfiles ( false ) ;
105107 return ;
106108 }
107- //1. map through subscribers and fetch profiles. skip profile if already on map
109+
110+ console . log ( '[PaidSubscribers] Starting hybrid profile fetch for' , subscribers . length , 'subscribers' ) ;
111+
112+ // Process each subscriber with hybrid approach
108113 await Promise . all (
109114 subscribers . map ( async ( subscriber ) => {
110- if (
111- subscriberProfiles . has ( subscriber . pubkey ) &&
112- subscriberProfiles . get ( subscriber . pubkey ) ?. picture &&
113- subscriberProfiles . get ( subscriber . pubkey ) ?. about
114- ) {
115- return subscriberProfiles . get ( subscriber . pubkey ) ;
115+ // Skip if we already have a complete profile in our map
116+ const existingProfile = subscriberProfiles . get ( subscriber . pubkey ) ;
117+ const hasValidProfile = existingProfile && (
118+ ( existingProfile . name && existingProfile . name !== 'Anonymous Subscriber' ) ||
119+ existingProfile . picture ||
120+ existingProfile . about
121+ ) ;
122+
123+ if ( hasValidProfile ) {
124+ console . log ( `[PaidSubscribers] Profile already cached for ${ subscriber . pubkey } :` , {
125+ name : existingProfile . name ,
126+ picture : ! ! existingProfile . picture ,
127+ about : ! ! existingProfile . about
128+ } ) ;
129+ return ;
116130 }
131+
117132 try {
118- if ( ! ndkInstance . ndk ) {
119- console . error ( 'NDK instance is not available' ) ;
120- return null ;
121- }
133+ console . log ( `[PaidSubscribers] Fetching NDK profile for ${ subscriber . pubkey } ` ) ;
134+
135+ // Try to fetch profile from NDK (user's relay + other relays)
122136 const user = await ndkInstance . ndk ?. getUser ( { pubkey : subscriber . pubkey } ) . fetchProfile ( ) ;
123- if ( user ) {
124- // Convert NDKUserProfile to SubscriberProfile and add to map
125- const covertedNDKUserProfile = convertNDKUserProfileToSubscriberProfile ( subscriber . pubkey , user ) ;
126- updateSubscriberProfile ( subscriber . pubkey , covertedNDKUserProfile ) ;
127-
128- return user ;
137+
138+ if ( user && ( user . name || user . picture || user . about ) ) {
139+ // NDK returned a profile - use it as the primary source
140+ console . log ( `[PaidSubscribers] NDK profile found for ${ subscriber . pubkey } :` , {
141+ name : user . name ,
142+ picture : user . picture ,
143+ about : user . about
144+ } ) ;
145+
146+ const ndkProfile = convertNDKUserProfileToSubscriberProfile ( subscriber . pubkey , user ) ;
147+ updateSubscriberProfile ( subscriber . pubkey , ndkProfile ) ;
148+ } else {
149+ // NDK came up empty - fallback to backend data
150+ console . log ( `[PaidSubscribers] NDK profile empty for ${ subscriber . pubkey } , falling back to backend data:` , {
151+ name : subscriber . name ,
152+ picture : subscriber . picture ,
153+ about : subscriber . about
154+ } ) ;
155+
156+ // Use the backend data as-is since NDK had no better information
157+ updateSubscriberProfile ( subscriber . pubkey , {
158+ ...subscriber ,
159+ // Ensure we have fallback values if backend data is also incomplete
160+ name : subscriber . name || 'Anonymous Subscriber' ,
161+ picture : subscriber . picture || '' ,
162+ about : subscriber . about || ''
163+ } ) ;
129164 }
130165 } catch ( error ) {
131- console . error ( `Error fetching profile for ${ subscriber . pubkey } :` , error ) ;
166+ console . error ( `[PaidSubscribers] Error fetching NDK profile for ${ subscriber . pubkey } :` , error ) ;
167+
168+ // Error occurred - fallback to backend data
169+ console . log ( `[PaidSubscribers] NDK error for ${ subscriber . pubkey } , using backend data` ) ;
170+ updateSubscriberProfile ( subscriber . pubkey , {
171+ ...subscriber ,
172+ name : subscriber . name || 'Anonymous Subscriber' ,
173+ picture : subscriber . picture || '' ,
174+ about : subscriber . about || ''
175+ } ) ;
132176 }
133- return null ;
134177 } ) ,
135178 ) ;
179+
180+ console . log ( '[PaidSubscribers] Hybrid profile fetch completed' ) ;
136181 setLoadingProfiles ( false ) ;
137182 } ;
183+
138184 fetchProfiles ( ) ;
139185 } , [ subscribers , ndkInstance ] ) ;
140186
0 commit comments