import { useContext, useSyncExternalStore } from "react"; import { HexKey, CachedMetadata } from "@snort/system"; import { SnortContext } from "./context"; /** * Gets a profile from cache or requests it from the relays */ export function useUserProfile(pubKey?: HexKey): CachedMetadata | undefined { const system = useContext(SnortContext); return useSyncExternalStore( h => { if (pubKey) { const handler = (keys: Array) => { if (keys.includes(pubKey)) { h(); } }; system.profileLoader.cache.on("change", handler); system.profileLoader.TrackKeys(pubKey); return () => { system.profileLoader.cache.off("change", handler); system.profileLoader.UntrackKeys(pubKey); }; } return () => { // noop }; }, () => system.profileLoader.cache.getFromCache(pubKey), ); }