UNPKG

@saberhq/sail

Version:

Account caching and batched loading for React-based Solana applications.

61 lines 1.7 kB
import { PublicKey } from "@solana/web3.js"; import { useMemo } from "react"; import { serializeKeys } from "../utils"; /** * Parses a Pubkey. * @param raw The string or Pubkey representation of the key. * @returns */ export const usePubkey = (raw) => { return useMemo(() => { if (raw === undefined) { return raw; } if (!raw) { return null; } if (raw instanceof PublicKey) { return raw; } try { return new PublicKey(raw); } catch (e) { return null; } }, [raw]); }; /** * Uses a {@link PublicKey}, ensuring the reference does not change if the key changes. * @param raw The string or Pubkey representation of the key. * @returns */ export const usePubkeyMemo = (raw) => { const keyNoMemo = usePubkey(raw); return useMemo(() => { return keyNoMemo; // eslint-disable-next-line react-hooks/exhaustive-deps }, [keyNoMemo === null || keyNoMemo === void 0 ? void 0 : keyNoMemo.toString()]); }; /** * Loads and parses multiple {@link PublicKey}s, preserving the reference. */ export const usePubkeysMemo = (keys) => { return useMemo(() => { return keys.map((k) => { if (typeof k === "string") { try { return new PublicKey(k); } catch (e) { return null; } } else { return k; } }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [JSON.stringify(serializeKeys(keys))]); }; //# sourceMappingURL=usePubkey.js.map