Skip to content

Commit

Permalink
refactor: create common hook for getting chain ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jul 3, 2024
1 parent 2f2ceb5 commit 439f3c1
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 80 deletions.
13 changes: 2 additions & 11 deletions packages/react/src/hooks/useAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ChainIdContext } from "../context.js";
import { accountsAtom } from "../stores/accounts.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import { useAtomValue } from "jotai";
import { useContext } from "react";

/**
* Hook for getting currently connected accounts.
Expand All @@ -12,12 +10,5 @@ import { useContext } from "react";
* @returns The currently connected accounts
*/
export function useAccounts(options?: ChainHookOptions) {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain Id provided");
}

return useAtomValue(accountsAtom(chainId));
return useAtomValue(accountsAtom(useChainId(options)));
}
11 changes: 2 additions & 9 deletions packages/react/src/hooks/useBlock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ChainIdContext } from "../context.js";
import {
bestBlockAtomFamily,
finalizedBlockAtomFamily,
} from "../stores/block.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import { useAtomValue } from "jotai";
import { useContext } from "react";

/**
* Hook for fetching information about the latest block.
Expand All @@ -19,12 +17,7 @@ export function useBlock(
tag: "best" | "finalized" = "finalized",
options?: ChainHookOptions,
) {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain Id provided");
}
const chainId = useChainId(options);

return useAtomValue(
tag === "finalized"
Expand Down
15 changes: 15 additions & 0 deletions packages/react/src/hooks/useChainId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ChainIdContext } from "../context.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import { useContext } from "react";

export default function useChainId(options?: ChainHookOptions) {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain Id provided");
}

return chainId;
}
13 changes: 2 additions & 11 deletions packages/react/src/hooks/useChainSpecData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ChainIdContext } from "../context.js";
import { chainSpecDataAtomFamily } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import { useAtomValue } from "jotai";
import { useContext } from "react";

/**
* Hook for fetching the [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html).
Expand All @@ -12,12 +10,5 @@ import { useContext } from "react";
* @returns The [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html)
*/
export function useChainSpecData(options?: ChainHookOptions) {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain Id provided");
}

return useAtomValue(chainSpecDataAtomFamily(chainId));
return useAtomValue(chainSpecDataAtomFamily(useChainId(options)));
}
13 changes: 2 additions & 11 deletions packages/react/src/hooks/useClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ChainIdContext } from "../context.js";
import { clientAtomFamily } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import { useAtomValue } from "jotai";
import { useContext } from "react";

/**
* Hook for getting Polkadot-API client instance.
Expand All @@ -12,12 +10,5 @@ import { useContext } from "react";
* @returns Polkadot-API client
*/
export function useClient(options?: ChainHookOptions) {
const defaultChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? defaultChainId;

if (chainId === undefined) {
throw new ReDotError("No chain ID provided");
}

return useAtomValue(clientAtomFamily(chainId));
return useAtomValue(clientAtomFamily(useChainId(options)));
}
16 changes: 5 additions & 11 deletions packages/react/src/hooks/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ChainIdContext, SignerContext } from "../context.js";
import { SignerContext } from "../context.js";
import { typedApiAtomFamily } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { useAsyncState } from "./useAsyncState.js";
import { MutationError, PENDING } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import type { ChainId, Chains, CommonDescriptor } from "@reactive-dot/core";
import { MutationError, PENDING } from "@reactive-dot/core";
import { useAtomCallback } from "jotai/utils";
import type {
PolkadotSigner,
Expand Down Expand Up @@ -51,7 +52,7 @@ export function useMutation<
txOptions?: TxOptions<ReturnType<TAction>>;
}>,
) {
const contextChainId = useContext(ChainIdContext);
const chainId = useChainId(options);
const contextSigner = useContext(SignerContext);

const [state, setState] = useAsyncState<TxEvent>();
Expand All @@ -70,12 +71,6 @@ export function useMutation<
throw new MutationError("No signer provided");
}

const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new MutationError("No chain ID provided");
}

const api = await get(typedApiAtomFamily(chainId));

const transaction = action(api.tx);
Expand All @@ -95,9 +90,8 @@ export function useMutation<
},
[
action,
contextChainId,
chainId,
contextSigner,
options?.chainId,
options?.signer,
options?.txOptions,
setState,
Expand Down
19 changes: 4 additions & 15 deletions packages/react/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ChainIdContext } from "../context.js";
import {
getQueryInstructionPayloadAtoms,
queryPayloadAtomFamily,
} from "../stores/query.js";
import type { Falsy, FalsyGuard, FlatHead } from "../types.js";
import { flatHead, stringify } from "../utils/vanilla.js";
import type { ChainHookOptions } from "./types.js";
import useChainId from "./useChainId.js";
import {
IDLE,
Query,
QueryError,
type ChainId,
type Chains,
type CommonDescriptor,
Expand All @@ -18,7 +17,7 @@ import {
} from "@reactive-dot/core";
import { atom, useAtomValue } from "jotai";
import { useAtomCallback } from "jotai/utils";
import { useCallback, useContext, useMemo } from "react";
import { useCallback, useMemo } from "react";

/**
* Hook for refreshing cached query.
Expand All @@ -38,16 +37,11 @@ export function useQueryRefresher<
: Chains[Exclude<TChainId, void>],
TChainId extends ChainId | void = void,
>(builder: TQuery, options?: ChainHookOptions) {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;
const chainId = useChainId(options);

const refresh = useAtomCallback(
useCallback(
(_, set) => {
if (chainId === undefined) {
throw new QueryError("No chain ID provided");
}

if (!builder) {
return;
}
Expand Down Expand Up @@ -105,12 +99,7 @@ export function useLazyLoadQueryWithRefresh<
>,
refresh: () => void,
] {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new QueryError("No chain ID provided");
}
const chainId = useChainId(options);

const query = useMemo(
() => (!builder ? undefined : builder(new Query([]))),
Expand Down
15 changes: 3 additions & 12 deletions packages/react/src/hooks/useTypedApi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ChainIdContext } from "../context.js";
import { typedApiAtomFamily } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import type { Chains, ChainId, CommonDescriptor } from "@reactive-dot/core";
import useChainId from "./useChainId.js";
import type { ChainId, Chains, CommonDescriptor } from "@reactive-dot/core";
import { useAtomValue } from "jotai";
import type { TypedApi } from "polkadot-api";
import { useContext } from "react";

/**
* Hook for getting Polkadot-API typed API.
Expand All @@ -18,12 +16,5 @@ export function useTypedApi<TChainId extends ChainId | void = void>(
): TypedApi<
TChainId extends void ? CommonDescriptor : Chains[Exclude<TChainId, void>]
> {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain ID provided");
}

return useAtomValue(typedApiAtomFamily(chainId));
return useAtomValue(typedApiAtomFamily(useChainId(options)));
}

0 comments on commit 439f3c1

Please sign in to comment.