import type { ProgramAccount } from "@saberhq/token-utils"; import type { PublicKey } from "@solana/web3.js"; import mapValues from "lodash.mapvalues"; import type { UseQueryOptions } from "react-query"; import type { ProgramAccountParser, ProgramAccountParsers, } from "./programAccounts"; import { makeProgramAccountParsers } from "./programAccounts"; import type { BatchedParsedAccountQueryData, BatchedParsedAccountQueryKeys, BatchParsedAccountQueryResult, } from "./useBatchedParsedAccounts"; import { useBatchedParsedAccounts } from "./useBatchedParsedAccounts"; import type { ParsedAccountQueryResult } from "./useParsedAccount"; import { useParsedAccount, useParsedAccounts } from "./useParsedAccount"; /** * React hooks for program account parsers. */ export type ProgramParserHooks = { /** * Uses the data of a single key. */ useSingleData: ( key: PublicKey | null | undefined, options?: Omit< UseQueryOptions | null | undefined>, "queryFn" | "queryKey" > ) => ParsedAccountQueryResult; /** * Uses the data of multiple keys. */ useData: ( keys: (PublicKey | null | undefined)[], options?: Omit< UseQueryOptions | null | undefined>, "queryFn" | "queryKey" > ) => ParsedAccountQueryResult[]; /** * Uses the data of multiple keys, batched into a single call. */ useBatchedData: ( keys: BatchedParsedAccountQueryKeys, options?: Omit< UseQueryOptions>, "queryFn" | "queryKey" > ) => BatchParsedAccountQueryResult; }; /** * Makes hooks for parsers. * @param parsers * @returns */ export const makeProgramParserHooks = ( parsers: ProgramAccountParsers ): { [K in A]: ProgramParserHooks; } => { const sailParsers = makeProgramAccountParsers(parsers); const hooks = mapValues( sailParsers, ( parser: ProgramAccountParser ): ProgramParserHooks => ({ useSingleData: ( key: PublicKey | null | undefined, options?: Omit< UseQueryOptions | null | undefined>, "queryFn" | "queryKey" > ): ParsedAccountQueryResult => useParsedAccount(key, parser, options), useData: ( keys: (PublicKey | null | undefined)[], options?: Omit< UseQueryOptions | null | undefined>, "queryFn" | "queryKey" > ): ParsedAccountQueryResult[] => useParsedAccounts(keys, parser, options), useBatchedData: ( keys: BatchedParsedAccountQueryKeys, options?: Omit< UseQueryOptions>, "queryFn" | "queryKey" > ): BatchParsedAccountQueryResult => useBatchedParsedAccounts(keys, parser, options), }) ); return hooks as unknown as { [K in A]: ProgramParserHooks; }; };