Skip to content

Commit

Permalink
Add useProgram hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Jul 4, 2024
1 parent b9e7de0 commit c396c3d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions utils/gear-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion utils/gear-hooks/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
UseSendMessageWithGasOptions,
SendMessageWithGasOptions,
} from './handlers';
import { useProgram, UseProgramParameters } from './sails';

export {
useReadFullState,
Expand Down Expand Up @@ -70,6 +71,13 @@ export {
useIsAnyAccountVoucherActive,
useIssuedVouchers,
useAccountIssuedVouchers,
useProgram,
};

export type { SendMessageOptions, UseSendMessageOptions, UseSendMessageWithGasOptions, SendMessageWithGasOptions };
export type {
SendMessageOptions,
UseSendMessageOptions,
UseSendMessageWithGasOptions,
SendMessageWithGasOptions,
UseProgramParameters,
};
4 changes: 4 additions & 0 deletions utils/gear-hooks/src/hooks/sails/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { useProgram, UseProgramParameters } from './use-program';

export { useProgram };
export type { UseProgramParameters };
38 changes: 38 additions & 0 deletions utils/gear-hooks/src/hooks/sails/use-program.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
new (api: GearApi, programId?: HexString): T;
};

type QueryOptions<T> = Omit<UseQueryOptions<T, undefined, T, (string | undefined)[]>, 'queryKey' | 'queryFn'>;

type UseProgramParameters<T> = {
library: Program<T>;
id: HexString | undefined;
query?: QueryOptions<T>;
};

function useProgram<T>({ library, id, query }: UseProgramParameters<T>) {
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 };
4 changes: 4 additions & 0 deletions utils/gear-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
UseSendMessageOptions,
UseSendMessageWithGasOptions,
SendMessageWithGasOptions,
useProgram,
UseProgramParameters,
} from './hooks';

import { withoutCommas, getVaraAddress, getTypedEntries } from './utils';
Expand Down Expand Up @@ -106,6 +108,7 @@ export {
useIsAnyAccountVoucherActive,
useIssuedVouchers,
useAccountIssuedVouchers,
useProgram,
DEFAULT_OPTIONS,
DEFAULT_INFO_OPTIONS,
DEFAULT_ERROR_OPTIONS,
Expand All @@ -130,4 +133,5 @@ export type {
Entries,
UseSendMessageWithGasOptions,
SendMessageWithGasOptions,
UseProgramParameters,
};
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c396c3d

Please sign in to comment.