@@ -5,9 +5,8 @@ import { CategoryComponents } from '@app/components/header/components/HeaderSear
55import { Btn , InputSearch } from '../HeaderSearch/HeaderSearch.styles' ;
66import { useTranslation } from 'react-i18next' ;
77import { BasePopover } from '@app/components/common/BasePopover/BasePopover' ;
8- import { NDKUserProfile , useNDK } from '@nostr-dev-kit/ndk-hooks' ;
98import usePaidSubscribers from '@app/hooks/usePaidSubscribers' ;
10- import { convertNDKUserProfileToSubscriberProfile } from '@app/utils/utils ' ;
9+ import { useProfileAPI } from '@app/hooks/useProfileAPI ' ;
1110import { InvalidPubkey } from '../../Header.styles' ;
1211
1312import { SubscriberProfile } from '@app/hooks/usePaidSubscribers' ;
@@ -34,7 +33,7 @@ export const SearchDropdown: React.FC<SearchOverlayProps> = ({
3433 const [ subscriberProfile , setSubscriberProfile ] = useState < SubscriberProfile | null > ( null ) ;
3534 const [ invalidPubkey , setInvalidPubkey ] = useState ( false ) ;
3635 const { subscribers } = usePaidSubscribers ( ) ;
37- const ndkInstance = useNDK ( ) ;
36+ const { fetchSingleProfile , loading : profileAPILoading } = useProfileAPI ( ) ;
3837 const { t } = useTranslation ( ) ;
3938
4039 useEffect ( ( ) => {
@@ -44,19 +43,19 @@ export const SearchDropdown: React.FC<SearchOverlayProps> = ({
4443 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4544 const ref = useRef < any > ( null ) ;
4645
47- const fetchProfile = async ( pubkey : string ) : Promise < NDKUserProfile | null > => {
48- if ( ! ndkInstance ) return null ;
49-
46+ const fetchProfile = async ( pubkey : string ) : Promise < SubscriberProfile | null > => {
5047 try {
5148 setFetchingProfile ( true ) ;
52- const profile = await ndkInstance . ndk ?. getUser ( { pubkey : pubkey } ) . fetchProfile ( ) ;
53-
49+ setFetchingFailed ( false ) ;
50+
51+ const profile = await fetchSingleProfile ( pubkey ) ;
52+
5453 if ( profile ) {
5554 setFetchingProfile ( false ) ;
5655 setFetchingFailed ( false ) ;
5756 return profile ;
5857 } else {
59- console . error ( 'Profile not found for pubkey:' , pubkey ) ;
58+ console . log ( 'Profile not found for pubkey:' , pubkey ) ;
6059 setFetchingProfile ( false ) ;
6160 setFetchingFailed ( true ) ;
6261 return null ;
@@ -71,34 +70,35 @@ export const SearchDropdown: React.FC<SearchOverlayProps> = ({
7170
7271 const handleSearchProfile = async ( ) => {
7372 if ( ! query ) return ;
74- //verify that it's a pubkey
73+
74+ // Verify that it's a valid pubkey (hex format)
7575 if ( / ^ [ a - f A - F 0 - 9 ] { 64 } $ / . test ( query ) ) {
7676 setSubscriberDetailModalOpen ( true ) ;
77+ setInvalidPubkey ( false ) ;
7778
78- //See if the pubkey exists in the subscribers
7979 const pubkey = query ;
80- const subscriber = subscribers . find ( ( sub ) => sub . pubkey === query ) ;
81- // If it exists, open the modal with the subscriber details. If name,picture, or about are not set, fetch profile.
82- //if It doesnt exist, fetch the profile from NDK
83- //once fetched, convert it to SubscriberProfile and open the modal
84- if ( subscriber ) {
85- if ( ! subscriber . name || ! subscriber . picture || ! subscriber . about ) {
86- const profile = await fetchProfile ( pubkey ) ;
87- if ( profile ) {
88- const subscriberProfile : SubscriberProfile = convertNDKUserProfileToSubscriberProfile ( pubkey , profile ) ;
89- // Open the modal with the fetched subscriber profile
90- setSubscriberProfile ( subscriberProfile ) ;
91- }
92- }
80+
81+ // First check if the pubkey exists in current subscribers list
82+ const existingSubscriber = subscribers . find ( ( sub ) => sub . pubkey === pubkey ) ;
83+
84+ if ( existingSubscriber && existingSubscriber . name && existingSubscriber . picture ) {
85+ // We already have complete profile data, use it directly
86+ setSubscriberProfile ( existingSubscriber ) ;
87+ setFetchingProfile ( false ) ;
88+ setFetchingFailed ( false ) ;
9389 } else {
90+ // Fetch the profile from API (either new subscriber or incomplete data)
9491 const profile = await fetchProfile ( pubkey ) ;
9592 if ( profile ) {
96- const subscriberProfile : SubscriberProfile = convertNDKUserProfileToSubscriberProfile ( pubkey , profile ) ;
97- // Open the modal with the fetched subscriber profile
98- setSubscriberProfile ( subscriberProfile ) ;
93+ setSubscriberProfile ( profile ) ;
94+ } else {
95+ // If API doesn't have the profile, use existing subscriber data if available
96+ if ( existingSubscriber ) {
97+ setSubscriberProfile ( existingSubscriber ) ;
98+ }
9999 }
100100 }
101- } else {
101+ } else {
102102 setInvalidPubkey ( true ) ;
103103 }
104104 } ;
@@ -152,7 +152,7 @@ export const SearchDropdown: React.FC<SearchOverlayProps> = ({
152152 </ HeaderActionWrapper >
153153 { isSubscriberDetailModalOpen && (
154154 < SubscriberDetailModal
155- loading = { fetchingProfile }
155+ loading = { fetchingProfile || profileAPILoading }
156156 fetchFailed = { fetchingFailed }
157157 isVisible = { isSubscriberDetailModalOpen }
158158 subscriber = { subscriberProfile }
0 commit comments