nostr-hooks
Version:
React hooks for developing Nostr clients
52 lines • 1.64 kB
JavaScript
import { useEffect, useState } from 'react';
import { useNdk } from '../use-ndk';
/**
* Custom hook to fetch a user profile.
*
* @param [profileParams] - Optional parameters to fetch the profile.
* @returns An object containing the user profile, null if the profile is not found,
* or undefined if the profile is being fetched.
* The status of the profile fetch is also returned.
*
* @example
* const { profile } = useProfile({ nip05: 'example@domain.com' });
*/
export const useProfile = (profileParams) => {
const [profile, setProfile] = useState();
const [status, setStatus] = useState('idle');
const { ndk } = useNdk();
useEffect(() => {
setStatus('idle');
if (!profileParams)
return;
if (profileParams.constructor === Object && Object.keys(profileParams).length === 0)
return;
if (!profileParams.nip05 && !profileParams.pubkey && !profileParams.npub)
return;
if (!ndk)
return;
setStatus('loading');
ndk
.getUser(profileParams)
.fetchProfile()
.then((profile) => {
setProfile(profile);
setStatus(profile ? 'success' : 'not-found');
})
.catch(() => {
setProfile(undefined);
setStatus('error');
});
}, [
setProfile,
setStatus,
profileParams?.nip05,
profileParams?.pubkey,
profileParams?.npub,
profileParams?.nip46Urls,
profileParams?.relayUrls,
ndk,
]);
return { profile, status };
};
//# sourceMappingURL=index.js.map