import { GearApi, HexString } from '@gear-js/api'; import { UseQueryOptions, useQuery } from '@tanstack/react-query'; import { useApi } from 'context'; type Program = { new (api: GearApi, programId?: HexString): T; }; type QueryOptions = Omit, 'queryKey' | 'queryFn'>; type UseProgramParameters = { library: Program; id: HexString | undefined; query?: QueryOptions; }; function useProgram({ library, id, query }: UseProgramParameters) { const { api, isApiReady } = useApi(); const getProgram = () => { if (!isApiReady) throw new Error('API is not initialized'); if (!id) throw new Error('Program ID is not found'); return new library(api, id); }; return useQuery({ ...query, queryKey: ['program', id, api?.provider.endpoint], queryFn: getProgram, enabled: isApiReady && Boolean(id) && (query?.enabled ?? true), }); } export { useProgram }; export type { UseProgramParameters };