-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9e7de0
commit c396c3d
Showing
6 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters