import { useContext, useSyncExternalStore } from "react";
import { HexKey, MetadataCache } from "@snort/system";
import { SnortContext } from "./context";

/**
 * Gets a profile from cache or requests it from the relays
 */
export function useUserProfile(pubKey?: HexKey): MetadataCache | undefined {
  const system = useContext(SnortContext);
  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),
  );
}
