@snort/system-react package
This commit is contained in:
23
packages/system-react/src/useUserProfile.ts
Normal file
23
packages/system-react/src/useUserProfile.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { HexKey, MetadataCache, NostrSystem } from "@snort/system";
|
||||
|
||||
/**
|
||||
* Gets a profile from cache or requests it from the relays
|
||||
*/
|
||||
export function useUserProfile(system: NostrSystem, pubKey?: HexKey): MetadataCache | undefined {
|
||||
return useSyncExternalStore<MetadataCache | undefined>(
|
||||
h => {
|
||||
if (pubKey) {
|
||||
system.ProfileLoader.TrackMetadata(pubKey);
|
||||
}
|
||||
const release = system.ProfileLoader.Cache.hook(h, pubKey);
|
||||
return () => {
|
||||
release();
|
||||
if (pubKey) {
|
||||
system.ProfileLoader.UntrackMetadata(pubKey);
|
||||
}
|
||||
}
|
||||
},
|
||||
() => system.ProfileLoader.Cache.getFromCache(pubKey)
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user