@snort/system-react package

This commit is contained in:
2023-06-16 20:31:33 +01:00
parent 28052e98b9
commit 939084167a
47 changed files with 334 additions and 122 deletions

View 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)
);
}