From c396c3d95fa5643d7feca7776b7d164bae368280 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 2 Jul 2024 18:07:20 +0300 Subject: [PATCH] Add useProgram hook --- utils/gear-hooks/package.json | 2 + utils/gear-hooks/src/hooks/index.ts | 10 ++++- utils/gear-hooks/src/hooks/sails/index.ts | 4 ++ .../gear-hooks/src/hooks/sails/use-program.ts | 38 +++++++++++++++++++ utils/gear-hooks/src/index.ts | 4 ++ yarn.lock | 2 + 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 utils/gear-hooks/src/hooks/sails/index.ts create mode 100644 utils/gear-hooks/src/hooks/sails/use-program.ts diff --git a/utils/gear-hooks/package.json b/utils/gear-hooks/package.json index 0783a297df..017805ab79 100644 --- a/utils/gear-hooks/package.json +++ b/utils/gear-hooks/package.json @@ -24,6 +24,7 @@ "@gear-js/api": "0.38.1", "@polkadot/api": "11.0.2", "@polkadot/extension-dapp": "0.46.5", + "@tanstack/react-query": "5.29.0", "react": "18.2.0", "react-dom": "18.2.0" }, @@ -43,6 +44,7 @@ "@polkadot/types": "11.0.2", "@rollup/plugin-commonjs": "25.0.5", "@rollup/plugin-node-resolve": "15.2.3", + "@tanstack/react-query": "5.29.0", "@types/react": "18.2.28", "@types/react-dom": "18.2.13", "@types/react-transition-group": "4.4.7", diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 02e8a1de8e..90bf6c57ab 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -36,6 +36,7 @@ import { UseSendMessageWithGasOptions, SendMessageWithGasOptions, } from './handlers'; +import { useProgram, UseProgramParameters } from './sails'; export { useReadFullState, @@ -70,6 +71,13 @@ export { useIsAnyAccountVoucherActive, useIssuedVouchers, useAccountIssuedVouchers, + useProgram, }; -export type { SendMessageOptions, UseSendMessageOptions, UseSendMessageWithGasOptions, SendMessageWithGasOptions }; +export type { + SendMessageOptions, + UseSendMessageOptions, + UseSendMessageWithGasOptions, + SendMessageWithGasOptions, + UseProgramParameters, +}; diff --git a/utils/gear-hooks/src/hooks/sails/index.ts b/utils/gear-hooks/src/hooks/sails/index.ts new file mode 100644 index 0000000000..3fcf53104e --- /dev/null +++ b/utils/gear-hooks/src/hooks/sails/index.ts @@ -0,0 +1,4 @@ +import { useProgram, UseProgramParameters } from './use-program'; + +export { useProgram }; +export type { UseProgramParameters }; diff --git a/utils/gear-hooks/src/hooks/sails/use-program.ts b/utils/gear-hooks/src/hooks/sails/use-program.ts new file mode 100644 index 0000000000..eaa5bec921 --- /dev/null +++ b/utils/gear-hooks/src/hooks/sails/use-program.ts @@ -0,0 +1,38 @@ +import { GearApi, HexString } from '@gear-js/api'; +import { UseQueryOptions, useQuery } from '@tanstack/react-query'; +import { useContext } from 'react'; + +import { ApiContext } 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 } = useContext(ApiContext); + + 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 }; diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index d671640f09..a97936c932 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -37,6 +37,8 @@ import { UseSendMessageOptions, UseSendMessageWithGasOptions, SendMessageWithGasOptions, + useProgram, + UseProgramParameters, } from './hooks'; import { withoutCommas, getVaraAddress, getTypedEntries } from './utils'; @@ -106,6 +108,7 @@ export { useIsAnyAccountVoucherActive, useIssuedVouchers, useAccountIssuedVouchers, + useProgram, DEFAULT_OPTIONS, DEFAULT_INFO_OPTIONS, DEFAULT_ERROR_OPTIONS, @@ -130,4 +133,5 @@ export type { Entries, UseSendMessageWithGasOptions, SendMessageWithGasOptions, + UseProgramParameters, }; diff --git a/yarn.lock b/yarn.lock index f4ae7b03bb..3d451da14d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2604,6 +2604,7 @@ __metadata: "@rollup/plugin-commonjs": 25.0.5 "@rollup/plugin-node-resolve": 15.2.3 "@substrate/connect": 0.8.10 + "@tanstack/react-query": 5.29.0 "@types/react": 18.2.28 "@types/react-dom": 18.2.13 "@types/react-transition-group": 4.4.7 @@ -2621,6 +2622,7 @@ __metadata: "@gear-js/api": 0.38.1 "@polkadot/api": 11.0.2 "@polkadot/extension-dapp": 0.46.5 + "@tanstack/react-query": 5.29.0 react: 18.2.0 react-dom: 18.2.0 languageName: unknown