diff --git a/apps/docs/docs/getting-started/connect-wallets.mdx b/apps/docs/docs/getting-started/connect-wallets.mdx index f208423d..ffffe338 100644 --- a/apps/docs/docs/getting-started/connect-wallets.mdx +++ b/apps/docs/docs/getting-started/connect-wallets.mdx @@ -81,7 +81,7 @@ import { useConnectWallet, } from "@reactive-dot/react"; -const Wallets = () => { +function Wallets() { const wallets = useWallets(); const connectedWallets = useConnectedWallets(); @@ -114,7 +114,7 @@ const Wallets = () => { ); -}; +} export default Wallet; ``` @@ -124,7 +124,7 @@ export default Wallet; ```tsx title="Accounts.tsx" import { useAccounts } from "@reactive-dot/react"; -const Accounts = () => { +function Accounts() { const accounts = useAccounts(); return ( @@ -142,7 +142,7 @@ const Accounts = () => { ); -}; +} export default Accounts; ``` diff --git a/apps/docs/docs/getting-started/multichain.md b/apps/docs/docs/getting-started/multichain.md index cf19007f..7864f735 100644 --- a/apps/docs/docs/getting-started/multichain.md +++ b/apps/docs/docs/getting-started/multichain.md @@ -73,7 +73,7 @@ One active chain at a time. import type { ChainId } from "@reactive-dot/core"; import type { ReDotChainProvider } from "@reactive-dot/react"; -const App = () => { +function App() { const [currentChainId, setCurrentChainId] = useState("polkadot"); return ( @@ -81,7 +81,7 @@ const App = () => { ); -}; +} ``` Multiple active chains at the same time. @@ -90,19 +90,21 @@ Multiple active chains at the same time. // ... import type { ReDotChainProvider } from "@reactive-dot/react"; -const App = () => ( - <> - - - - - - - - - - -); +function App() { + return ( + <> + + + + + + + + + + + ); +} ``` ### Hook @@ -112,9 +114,9 @@ All hooks provide an option to specify which chain to target. ```tsx import { useBlock } from "@reactive-dot/react"; -const Component = () => { +function Component() { const polkadotBlock = useBlock({ chainId: "polkadot" }); const kusamaBlock = useBlock({ chainId: "kusama" }); const westendBlock = useBlock({ chainId: "westend" }); -}; +} ``` diff --git a/apps/docs/docs/getting-started/mutation.md b/apps/docs/docs/getting-started/mutation.md index ec096a84..cffa7592 100644 --- a/apps/docs/docs/getting-started/mutation.md +++ b/apps/docs/docs/getting-started/mutation.md @@ -19,9 +19,11 @@ There are multiple way to select the account used for signing. ```tsx import { ReDotSignerProvider } from "@reactive-dot/react"; -const App = () => ( - {/* ... */} -); +function App() { + return ( + {/* ... */} + ); +} ``` ### 2. Passing signer to the hook @@ -58,7 +60,7 @@ import { IDLE, MutationError, PENDING } from "@reactive-dot/core"; import { useMutation } from "@reactive-dot/react"; import { Binary } from "polkadot-api"; -const Component = () => { +function Component() { const [transactionState, submit] = useMutation((tx) => tx.System.remark({ remark: Binary.fromText("Hello from reactive-dot!") }), ); @@ -80,5 +82,5 @@ const Component = () => { ); } -}; +} ``` diff --git a/apps/docs/docs/getting-started/query.md b/apps/docs/docs/getting-started/query.md index a6fe734a..0497e9d5 100644 --- a/apps/docs/docs/getting-started/query.md +++ b/apps/docs/docs/getting-started/query.md @@ -11,15 +11,15 @@ The [`useLazyLoadQuery`](/api/react/function/useLazyLoadQuery) hook allow you to [`useLazyLoadQuery`](/api/react/function/useLazyLoadQuery) utilize React's Suspense API for data fetching & error handling. ```tsx -const ActiveEra = () => { +function ActiveEra() { const activeEra = useLazyLoadQuery((builder) => builder.readStorage("Staking", "ActiveEra", []), ); return
Active era: {activeEra}
; -}; +} -const App = () => { +function App() { return ( @@ -27,7 +27,7 @@ const App = () => { ); -}; +} ``` ## Fetching multiple data @@ -35,7 +35,7 @@ const App = () => { Fetching multiple data can be done by chaining queries together, [`useLazyLoadQuery`](/api/react/function/useLazyLoadQuery) (with TypeScript) will automatically infer that you want to fetch multiple data concurrently & will return an array of data instead. ```tsx -const MultiQuery = () => { +function MultiQuery() { const [expectedBlockTime, epochDuration, proposalCount] = useLazyLoadQuery( (builder) => builder @@ -56,7 +56,7 @@ const MultiQuery = () => {
{proposalCount}
); -}; +} ``` Multiple queries of the same type can also be fetched using [`callApis`](/api/core/class/Query#callApis) & [`readStorages`](/api/core/class/Query#readStorages). @@ -82,7 +82,7 @@ const [rewards, metadatum] = useLazyLoadQuery((builder) => Result of a query can be used as variables in subsequent queries. ```tsx -const Query = () => { +function Query() { const pools = useLazyLoadQuery((builder) => builder.readStorageEntries("NominationPools", "BondedPools", []), ); @@ -105,7 +105,7 @@ const Query = () => { ); -}; +} ``` ## Conditional query @@ -147,7 +147,7 @@ Certain query, like runtime API calls & reading of storage entries doesn't creat ```tsx import { useTransition } from "react"; -const QueryWithRefresh = () => { +function QueryWithRefresh() { const [isPending, startTransition] = useTransition(); const [pendingRewards, refreshPendingRewards] = useLazyLoadQueryWithRefresh( (builder) => @@ -167,13 +167,13 @@ const QueryWithRefresh = () => { ); -}; +} ``` The above will refresh all refreshable data in the query. If you want to target specific data to refresh, a separate [`useQueryRefresher`](/api/react/function/useQueryRefresher) hook can be used. ```tsx -const QueryWithRefresh = () => { +function QueryWithRefresh() { const [isPending, startTransition] = useTransition(); const [account1Rewards, account2Rewards] = useLazyLoadQuery((builder) => builder @@ -198,7 +198,7 @@ const QueryWithRefresh = () => { ); -}; +} ``` ## Retry failed query @@ -209,14 +209,18 @@ Error from queries can be caught and reset using `ErrorBoundary` & [`useResetQue import { useResetQueryError } from "@reactive-dot/react"; import { ErrorBoundary, type FallbackProps } from "react-error-boundary"; -const ErrorFallback = (props: FallbackProps) => ( -
-
Oops, something went wrong!
- -
-); +function ErrorFallback(props: FallbackProps) { + return ( +
+
Oops, something went wrong!
+ +
+ ); +} -const AppErrorBoundary = () => { +function AppErrorBoundary() { const resetQueryError = useResetQueryError(); return ( @@ -232,5 +236,5 @@ const AppErrorBoundary = () => { {/* ... */} ); -}; +} ``` diff --git a/apps/docs/docs/getting-started/setup.mdx b/apps/docs/docs/getting-started/setup.mdx index bd87e161..d0c70d03 100644 --- a/apps/docs/docs/getting-started/setup.mdx +++ b/apps/docs/docs/getting-started/setup.mdx @@ -142,15 +142,17 @@ import config from "./config"; import { ReDotChainProvider, ReDotProvider } from "@reactive-dot/react"; import { Suspense } from "react"; -const App = () => ( - - {/* `chainId` match the ID previously specified via `polkadot: typeof dot` */} - - {/* Make sure there is at least one Suspense boundary wrapping the app */} - {/* ... */} - - -); +function App() { + return ( + + {/* `chainId` match the ID previously specified via `polkadot: typeof dot` */} + + {/* Make sure there is at least one Suspense boundary wrapping the app */} + {/* ... */} + + + ); +} export default App; ``` @@ -161,7 +163,7 @@ export default App; import { config } from "./config"; import { useAccounts, useLazyLoadQuery } from "@reactive-dot/react"; -const MyComponent = () => { +function MyComponent() { const accounts = useAccounts(); const [timestamp, totalIssuance] = useLazyLoadQuery((builder) => builder @@ -187,7 +189,7 @@ const MyComponent = () => { ); -}; +} export default MyComponent; ``` diff --git a/apps/docs/src/components/HomepageFeatures/index.tsx b/apps/docs/src/components/HomepageFeatures/index.tsx index 2bec8aae..637e65a5 100644 --- a/apps/docs/src/components/HomepageFeatures/index.tsx +++ b/apps/docs/src/components/HomepageFeatures/index.tsx @@ -65,7 +65,7 @@ function Feature({ title, emoji, description }: FeatureItem) { ); } -export default function HomepageFeatures(): JSX.Element { +export default function HomepageFeatures() { return (
diff --git a/apps/docs/src/pages/index.tsx b/apps/docs/src/pages/index.tsx index 924bdd51..9285a93d 100644 --- a/apps/docs/src/pages/index.tsx +++ b/apps/docs/src/pages/index.tsx @@ -28,7 +28,7 @@ function HomepageHeader() { ); } -export default function Home(): JSX.Element { +export default function Home() { return ( diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index efa90086..975f7ab7 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -22,7 +22,7 @@ import { Binary } from "polkadot-api"; import { Suspense, useState, useTransition } from "react"; import { ErrorBoundary, type FallbackProps } from "react-error-boundary"; -const useNativeTokenNumberWithPlanck = (planck: bigint) => { +function useNativeTokenNumberWithPlanck(planck: bigint) { const chainSpecData = useChainSpecData(); return new DenominatedNumber( @@ -30,16 +30,18 @@ const useNativeTokenNumberWithPlanck = (planck: bigint) => { chainSpecData.properties.tokenDecimals, chainSpecData.properties.tokenSymbol, ); -}; +} -const PendingRewards = (props: { address: string; rewards: bigint }) => ( -
  • - {props.address}:{" "} - {useNativeTokenNumberWithPlanck(props.rewards).toLocaleString()} -
  • -); +function PendingRewards(props: { address: string; rewards: bigint }) { + return ( +
  • + {props.address}:{" "} + {useNativeTokenNumberWithPlanck(props.rewards).toLocaleString()} +
  • + ); +} -const PendingPoolRewards = () => { +function PendingPoolRewards() { const accounts = useAccounts(); const [isPending, startTransition] = useTransition(); @@ -82,9 +84,9 @@ const PendingPoolRewards = () => { ); -}; +} -const Query = () => { +function Query() { const block = useBlock(); const [ @@ -169,13 +171,13 @@ const Query = () => {
    ); -}; +} type WalletItemProps = { wallet: Wallet; }; -const WalletItem = (props: WalletItemProps) => { +function WalletItem(props: WalletItemProps) { const connectedWallets = useConnectedWallets(); const [connectingState, connect] = useConnectWallet(props.wallet); @@ -203,9 +205,9 @@ const WalletItem = (props: WalletItemProps) => { )} ); -}; +} -const WalletConnection = () => { +function WalletConnection() { const wallets = useWallets(); return ( @@ -223,9 +225,9 @@ const WalletConnection = () => { ); -}; +} -const Mutation = () => { +function Mutation() { const connectedWallets = useConnectedWallets(); const accounts = useAccounts(); @@ -302,22 +304,27 @@ const Mutation = () => { )} ); -}; +} -const ErrorFallback = (props: FallbackProps) => ( -
    -
    - Oops, something went wrong! -
    - -
    -); +function ErrorFallback(props: FallbackProps) { + return ( +
    +
    + Oops, something went wrong! +
    + +
    + ); +} type ExampleProps = { chainName: string }; -const Example = (props: ExampleProps) => { +function Example(props: ExampleProps) { const resetQueryError = useResetQueryError(); return ( @@ -339,27 +346,27 @@ const Example = (props: ExampleProps) => { ); -}; +} -const App = () => ( - - - - - - - - - Loading Kusama...}> - - - - - Loading Westend...}> - +export default function App() { + return ( + + + - - -); - -export default App; + + + + + Loading Kusama...}> + + + + + Loading Westend...}> + + + + + ); +} diff --git a/packages/core/src/actions/connectWallet.ts b/packages/core/src/actions/connectWallet.ts index 76872e1d..f51b2949 100644 --- a/packages/core/src/actions/connectWallet.ts +++ b/packages/core/src/actions/connectWallet.ts @@ -1,7 +1,7 @@ import type Wallet from "../wallets/wallet.js"; -export const connectWallet = async (wallet: Wallet | Wallet[]) => { +export async function connectWallet(wallet: Wallet | Wallet[]) { const walletsToConnect = Array.isArray(wallet) ? wallet : [wallet]; await Promise.all(walletsToConnect.map((wallet) => wallet.connect())); -}; +} diff --git a/packages/core/src/actions/disconnectWallet.ts b/packages/core/src/actions/disconnectWallet.ts index b1cf2a5a..5f0cf2f3 100644 --- a/packages/core/src/actions/disconnectWallet.ts +++ b/packages/core/src/actions/disconnectWallet.ts @@ -1,7 +1,7 @@ import type Wallet from "../wallets/wallet.js"; -export const disconnectWallet = async (wallet: Wallet | Wallet[]) => { +export async function disconnectWallet(wallet: Wallet | Wallet[]) { const walletsToDisconnect = Array.isArray(wallet) ? wallet : [wallet]; await Promise.all(walletsToDisconnect.map((wallet) => wallet.disconnect())); -}; +} diff --git a/packages/core/src/actions/query.ts b/packages/core/src/actions/query.ts index 4d539bfa..a7f20627 100644 --- a/packages/core/src/actions/query.ts +++ b/packages/core/src/actions/query.ts @@ -5,9 +5,9 @@ import type { import type { CommonDescriptor } from "../config.js"; import type { ChainDefinition, TypedApi } from "polkadot-api"; -export const preflight = ( +export function preflight( instruction: TInstruction, -) => { +) { type Return = TInstruction["instruction"] extends "fetch-constant" ? "promise" : TInstruction["instruction"] extends "call-api" @@ -26,16 +26,16 @@ export const preflight = ( case "read-storage": return "observable" as Return; } -}; +} -export const query = < +export function query< TInstruction extends QueryInstruction, TDescriptor extends ChainDefinition = CommonDescriptor, >( api: TypedApi, instruction: TInstruction, options?: { signal?: AbortSignal }, -): InferInstructionResponse => { +): InferInstructionResponse { switch (instruction.instruction) { case "fetch-constant": // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -63,4 +63,4 @@ export const query = < signal: options?.signal, }) as InferInstructionResponse; } -}; +} diff --git a/packages/core/src/storage.ts b/packages/core/src/storage.ts index e02e6864..4a018d61 100644 --- a/packages/core/src/storage.ts +++ b/packages/core/src/storage.ts @@ -17,23 +17,30 @@ export class PrefixedStorage this.#storage = options.storage; } - getItem = (key: TKey) => this.#storage.getItem(this.#prefixKey(key)); + getItem(key: TKey) { + return this.#storage.getItem(this.#prefixKey(key)); + } - removeItem = (key: TKey) => this.#storage.removeItem(this.#prefixKey(key)); + removeItem(key: TKey) { + return this.#storage.removeItem(this.#prefixKey(key)); + } - setItem = (key: TKey, value: string) => - this.#storage.setItem(this.#prefixKey(key), value); + setItem(key: TKey, value: string) { + return this.#storage.setItem(this.#prefixKey(key), value); + } - join = (path: string) => { + join(path: string) { return new PrefixedStorage( { prefix: `${this.prefix}/${path}`, storage: this.#storage, }, ); - }; + } - #prefixKey = (key: string) => `${this.prefix}/${key}`; + #prefixKey(key: string) { + return `${this.prefix}/${key}`; + } } export const defaultStorage = new PrefixedStorage({ diff --git a/packages/core/src/wallets/initialize-wallets.ts b/packages/core/src/wallets/initialize-wallets.ts index ab36abb6..1dcac8a7 100644 --- a/packages/core/src/wallets/initialize-wallets.ts +++ b/packages/core/src/wallets/initialize-wallets.ts @@ -1,9 +1,9 @@ import { WalletAggregator } from "./aggregator/index.js"; import type Wallet from "./wallet.js"; -export const initializeWallets = async ( +export async function initializeWallets( walletsOrAggregators: Array, -) => { +) { const wallets = ( await Promise.all( walletsOrAggregators.map((walletOrAggregator) => @@ -15,4 +15,4 @@ export const initializeWallets = async ( ).flat(); return Promise.all(wallets.map((wallet) => wallet.initialize())); -}; +} diff --git a/packages/core/src/wallets/wallet-connect/from-pjs-account.ts b/packages/core/src/wallets/wallet-connect/from-pjs-account.ts index 7c5854f6..2bc33c0e 100644 --- a/packages/core/src/wallets/wallet-connect/from-pjs-account.ts +++ b/packages/core/src/wallets/wallet-connect/from-pjs-account.ts @@ -13,7 +13,7 @@ import { } from "@polkadot-api/substrate-bindings"; import { fromHex, mergeUint8, toHex } from "@polkadot-api/utils"; -export const getAddressFormat = (metadata: V15): number => { +export function getAddressFormat(metadata: V15): number { const dynamicBuilder = getDynamicBuilder(metadata); const constant = metadata.pallets @@ -21,7 +21,7 @@ export const getAddressFormat = (metadata: V15): number => { .constants!.find((s) => s.name === "SS58Prefix")!; return dynamicBuilder.buildDefinition(constant.type).dec(constant.value); -}; +} const versionCodec = enhanceEncoder( u8.enc, diff --git a/packages/core/src/wallets/wallet-connect/pjs-signed-extensions-mappers.ts b/packages/core/src/wallets/wallet-connect/pjs-signed-extensions-mappers.ts index a3a99543..7538c86b 100644 --- a/packages/core/src/wallets/wallet-connect/pjs-signed-extensions-mappers.ts +++ b/packages/core/src/wallets/wallet-connect/pjs-signed-extensions-mappers.ts @@ -14,68 +14,77 @@ type SignedExtension = { additionalSigned: Uint8Array; }; -const toPjsHex = (value: number | bigint, minByteLen?: number) => { +function toPjsHex(value: number | bigint, minByteLen?: number) { let inner = value.toString(16); inner = (inner.length % 2 ? "0" : "") + inner; const nPaddedBytes = Math.max(0, (minByteLen || 0) - inner.length / 2); return "0x" + "00".repeat(nPaddedBytes) + inner; -}; +} -export const CheckGenesis = ({ - additionalSigned, -}: SignedExtension): { genesisHash: string } => ({ - genesisHash: toHex(additionalSigned), -}); +export function CheckGenesis({ additionalSigned }: SignedExtension): { + genesisHash: string; +} { + return { + genesisHash: toHex(additionalSigned), + }; +} -export const CheckNonce = ({ - value, -}: SignedExtension): { nonce: HexString } => { +export function CheckNonce({ value }: SignedExtension): { nonce: HexString } { // nonce is a u32 in pjs => 4 bytes return { nonce: toPjsHex(compact.dec(value), 4) }; -}; +} -export const CheckTxVersion = ({ - additionalSigned, -}: SignedExtension): { transactionVersion: HexString } => { +export function CheckTxVersion({ additionalSigned }: SignedExtension): { + transactionVersion: HexString; +} { return { transactionVersion: toPjsHex(u32.dec(additionalSigned), 4) }; -}; +} const assetTxPaymentDec = Struct({ tip: compact, asset: Option(Bytes(Infinity)), }).dec; -export const ChargeAssetTxPayment = ({ - value, -}: SignedExtension): { aseetId?: string; tip?: string } => { +export function ChargeAssetTxPayment({ value }: SignedExtension): { + aseetId?: string; + tip?: string; +} { const { tip, asset } = assetTxPaymentDec(value); return { ...(asset ? { assetId: toHex(asset) } : {}), tip: toPjsHex(tip, 16), }; -}; +} -export const ChargeTransactionPayment = ({ - value, -}: SignedExtension): { tip: HexString } => ({ - tip: toPjsHex(compactBn.dec(value), 16), // u128 => 16 bytes -}); +export function ChargeTransactionPayment({ value }: SignedExtension): { + tip: HexString; +} { + return { + tip: toPjsHex(compactBn.dec(value), 16), // u128 => 16 bytes + }; +} -export const CheckMortality = ( +export function CheckMortality( { value, additionalSigned }: SignedExtension, blockNumber: number, -): { era: HexString; blockHash: HexString; blockNumber: HexString } => ({ - era: toHex(value), - blockHash: toHex(additionalSigned), - blockNumber: toPjsHex(blockNumber, 4), -}); +): { era: HexString; blockHash: HexString; blockNumber: HexString } { + return { + era: toHex(value), + blockHash: toHex(additionalSigned), + blockNumber: toPjsHex(blockNumber, 4), + }; +} -export const CheckSpecVersion = ({ - additionalSigned, -}: SignedExtension): { specVersion: HexString } => ({ - specVersion: toPjsHex(u32.dec(additionalSigned), 4), -}); +export function CheckSpecVersion({ additionalSigned }: SignedExtension): { + specVersion: HexString; +} { + return { + specVersion: toPjsHex(u32.dec(additionalSigned), 4), + }; +} // we create the tx without metadata hash, it's optional for PJS -export const CheckMetadataHash = () => ({}); +export function CheckMetadataHash() { + return {}; +} diff --git a/packages/react/src/context.tsx b/packages/react/src/context.tsx index c43997a3..639c2226 100644 --- a/packages/react/src/context.tsx +++ b/packages/react/src/context.tsx @@ -26,7 +26,7 @@ export type ReDotProviderProps = PropsWithChildren<{ autoReconnectWallets?: boolean; }>; -const ReDotHydrator = (props: ReDotProviderProps) => { +function ReDotHydrator(props: ReDotProviderProps) { useHydrateAtoms( useMemo( () => @@ -52,9 +52,9 @@ const ReDotHydrator = (props: ReDotProviderProps) => { ); return null; -}; +} -const WalletsReconnector = () => { +function WalletsReconnector() { const [_, reconnect] = useReconnectWallets(); useEffect(() => { @@ -62,7 +62,7 @@ const WalletsReconnector = () => { }, [reconnect]); return null; -}; +} /** * React context provider for Reactive DOT. @@ -70,20 +70,22 @@ const WalletsReconnector = () => { * @param props - Component props * @returns React element */ -export const ReDotProvider = ({ +export function ReDotProvider({ autoReconnectWallets = true, ...props -}: ReDotProviderProps) => ( - - - {autoReconnectWallets && ( - - - - )} - {props.children} - -); +}: ReDotProviderProps) { + return ( + + + {autoReconnectWallets && ( + + + + )} + {props.children} + + ); +} export const ChainIdContext = createContext(undefined); @@ -97,11 +99,13 @@ export type ReDotChainProviderProps = PropsWithChildren<{ * @param props - Component props * @returns React element */ -export const ReDotChainProvider = (props: ReDotChainProviderProps) => ( - - {props.children} - -); +export function ReDotChainProvider(props: ReDotChainProviderProps) { + return ( + + {props.children} + + ); +} export const SignerContext = createContext( undefined, @@ -120,8 +124,10 @@ export type ReDotSignerProviderProps = PropsWithChildren<{ * @param props - Component props * @returns React element */ -export const ReDotSignerProvider = (props: ReDotSignerProviderProps) => ( - - {props.children} - -); +export function ReDotSignerProvider(props: ReDotSignerProviderProps) { + return ( + + {props.children} + + ); +} diff --git a/packages/react/src/hooks/useAccounts.ts b/packages/react/src/hooks/useAccounts.ts index 3fa261af..27f298e9 100644 --- a/packages/react/src/hooks/useAccounts.ts +++ b/packages/react/src/hooks/useAccounts.ts @@ -11,7 +11,7 @@ import { useContext } from "react"; * @param options - Additional options * @returns The currently connected accounts */ -export const useAccounts = (options?: ChainHookOptions) => { +export function useAccounts(options?: ChainHookOptions) { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -20,4 +20,4 @@ export const useAccounts = (options?: ChainHookOptions) => { } return useAtomValue(accountsAtom(chainId)); -}; +} diff --git a/packages/react/src/hooks/useAsyncState.ts b/packages/react/src/hooks/useAsyncState.ts index 32dc01aa..e04593be 100644 --- a/packages/react/src/hooks/useAsyncState.ts +++ b/packages/react/src/hooks/useAsyncState.ts @@ -1,7 +1,6 @@ import { IDLE, type AsyncState, type MutationError } from "@reactive-dot/core"; import { useState } from "react"; -export const useAsyncState = < - TResult, - TError extends Error = MutationError, ->() => useState>(IDLE); +export function useAsyncState() { + return useState>(IDLE); +} diff --git a/packages/react/src/hooks/useBlock.ts b/packages/react/src/hooks/useBlock.ts index e91e11b6..974adb8c 100644 --- a/packages/react/src/hooks/useBlock.ts +++ b/packages/react/src/hooks/useBlock.ts @@ -15,10 +15,10 @@ import { useContext } from "react"; * @param options - Additional options * @returns The latest finalized or best block */ -export const useBlock = ( +export function useBlock( tag: "best" | "finalized" = "finalized", options?: ChainHookOptions, -) => { +) { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -31,4 +31,4 @@ export const useBlock = ( ? finalizedBlockAtomFamily(chainId) : bestBlockAtomFamily(chainId), ); -}; +} diff --git a/packages/react/src/hooks/useChainSpecData.ts b/packages/react/src/hooks/useChainSpecData.ts index 7ddffe6f..fb7a5cef 100644 --- a/packages/react/src/hooks/useChainSpecData.ts +++ b/packages/react/src/hooks/useChainSpecData.ts @@ -11,7 +11,7 @@ import { useContext } from "react"; * @param options - Additional options * @returns The [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html) */ -export const useChainSpecData = (options?: ChainHookOptions) => { +export function useChainSpecData(options?: ChainHookOptions) { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -20,4 +20,4 @@ export const useChainSpecData = (options?: ChainHookOptions) => { } return useAtomValue(chainSpecDataAtomFamily(chainId)); -}; +} diff --git a/packages/react/src/hooks/useClient.ts b/packages/react/src/hooks/useClient.ts index cf3f06f6..4703d831 100644 --- a/packages/react/src/hooks/useClient.ts +++ b/packages/react/src/hooks/useClient.ts @@ -11,7 +11,7 @@ import { useContext } from "react"; * @param options - Additional options * @returns Polkadot-API client */ -export const useClient = (options?: ChainHookOptions) => { +export function useClient(options?: ChainHookOptions) { const defaultChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? defaultChainId; @@ -20,4 +20,4 @@ export const useClient = (options?: ChainHookOptions) => { } return useAtomValue(clientAtomFamily(chainId)); -}; +} diff --git a/packages/react/src/hooks/useConnectWallet.ts b/packages/react/src/hooks/useConnectWallet.ts index 71a70413..4a9c5dc8 100644 --- a/packages/react/src/hooks/useConnectWallet.ts +++ b/packages/react/src/hooks/useConnectWallet.ts @@ -11,7 +11,7 @@ import { useCallback } from "react"; * @param wallets - Wallets to connect to, will connect to all available wallets if none is specified * @returns The wallet connection state & connect function */ -export const useConnectWallet = (wallets?: Wallet | Wallet[]) => { +export function useConnectWallet(wallets?: Wallet | Wallet[]) { const hookWallets = wallets; const [state, setState] = useAsyncState(); @@ -37,4 +37,4 @@ export const useConnectWallet = (wallets?: Wallet | Wallet[]) => { connectionState: typeof state, connect: typeof connect, ]; -}; +} diff --git a/packages/react/src/hooks/useDisconnectWallet.ts b/packages/react/src/hooks/useDisconnectWallet.ts index 5155d25e..3ef88eb8 100644 --- a/packages/react/src/hooks/useDisconnectWallet.ts +++ b/packages/react/src/hooks/useDisconnectWallet.ts @@ -11,7 +11,7 @@ import { useCallback } from "react"; * @param wallets - Wallets to disconnect from, will disconnect from all connected wallets if none is specified * @returns The wallet disconnection state & disconnect function */ -export const useDisconnectWallet = (wallets?: Wallet | Wallet[]) => { +export function useDisconnectWallet(wallets?: Wallet | Wallet[]) { const hookWallets = wallets; const [state, setState] = useAsyncState(); @@ -37,4 +37,4 @@ export const useDisconnectWallet = (wallets?: Wallet | Wallet[]) => { disconnectionState: typeof state, disconnect: typeof disconnect, ]; -}; +} diff --git a/packages/react/src/hooks/useMutation.ts b/packages/react/src/hooks/useMutation.ts index 250d8f77..d7cdddce 100644 --- a/packages/react/src/hooks/useMutation.ts +++ b/packages/react/src/hooks/useMutation.ts @@ -30,7 +30,7 @@ type TxOptions> = Parameters< * @param options - Additional options * @returns The current transaction state & submit function */ -export const useMutation = < +export function useMutation< TAction extends ( builder: TypedApi< TChainId extends void ? CommonDescriptor : Chains[Exclude] @@ -50,7 +50,7 @@ export const useMutation = < */ txOptions?: TxOptions>; }>, -) => { +) { const contextChainId = useContext(ChainIdContext); const contextSigner = useContext(SignerContext); @@ -106,4 +106,4 @@ export const useMutation = < ); return [state, submit] as [state: typeof state, submit: typeof submit]; -}; +} diff --git a/packages/react/src/hooks/useQuery.ts b/packages/react/src/hooks/useQuery.ts index e0a00ea6..9bd84821 100644 --- a/packages/react/src/hooks/useQuery.ts +++ b/packages/react/src/hooks/useQuery.ts @@ -27,7 +27,7 @@ import { useCallback, useContext, useMemo } from "react"; * @param options - Additional options * @returns The function to refresh the query */ -export const useQueryRefresher = < +export function useQueryRefresher< TQuery extends | (( builder: Query<[], TDescriptor>, @@ -37,10 +37,7 @@ export const useQueryRefresher = < ? CommonDescriptor : Chains[Exclude], TChainId extends ChainId | void = void, ->( - builder: TQuery, - options?: ChainHookOptions, -) => { +>(builder: TQuery, options?: ChainHookOptions) { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -74,7 +71,7 @@ export const useQueryRefresher = < ); return refresh; -}; +} /** * Hook for querying data from chain, returning the response & a refresher function. @@ -83,7 +80,7 @@ export const useQueryRefresher = < * @param options - Additional options * @returns The data response & a function to refresh it */ -export const useLazyLoadQueryWithRefresh = < +export function useLazyLoadQueryWithRefresh< TQuery extends | (( builder: Query<[], TDescriptor>, @@ -107,7 +104,7 @@ export const useLazyLoadQueryWithRefresh = < typeof IDLE >, refresh: () => void, -] => { +] { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -152,7 +149,7 @@ export const useLazyLoadQueryWithRefresh = < data, refresh, ]; -}; +} /** * Hook for querying data from chain, and returning the response. @@ -161,7 +158,7 @@ export const useLazyLoadQueryWithRefresh = < * @param options - Additional options * @returns The data response */ -export const useLazyLoadQuery = < +export function useLazyLoadQuery< TQuery extends | (( builder: Query<[], TDescriptor>, @@ -182,8 +179,8 @@ export const useLazyLoadQuery = < InferQueryPayload>, Falsy>> >, typeof IDLE - > => { + > { const [data] = useLazyLoadQueryWithRefresh(builder, options); return data; -}; +} diff --git a/packages/react/src/hooks/useReconnectWallets.ts b/packages/react/src/hooks/useReconnectWallets.ts index 5c279243..a476211d 100644 --- a/packages/react/src/hooks/useReconnectWallets.ts +++ b/packages/react/src/hooks/useReconnectWallets.ts @@ -13,7 +13,7 @@ import { useCallback, useState } from "react"; * * @returns The reconnection state and reconnect function */ -export const useReconnectWallets = () => { +export function useReconnectWallets() { const wallets = useWallets(); const [state, setState] = useState>(IDLE); @@ -27,4 +27,4 @@ export const useReconnectWallets = () => { state: typeof state, reconnect: typeof reconnect, ]; -}; +} diff --git a/packages/react/src/hooks/useResetQueryError.ts b/packages/react/src/hooks/useResetQueryError.ts index 80256231..0c265740 100644 --- a/packages/react/src/hooks/useResetQueryError.ts +++ b/packages/react/src/hooks/useResetQueryError.ts @@ -5,4 +5,6 @@ import { resetQueryError } from "../utils/jotai.js"; * * @returns Function to reset caught query error */ -export const useResetQueryError = () => resetQueryError; +export function useResetQueryError() { + return resetQueryError; +} diff --git a/packages/react/src/hooks/useTypedApi.ts b/packages/react/src/hooks/useTypedApi.ts index aa37d217..2a5b7e13 100644 --- a/packages/react/src/hooks/useTypedApi.ts +++ b/packages/react/src/hooks/useTypedApi.ts @@ -13,11 +13,11 @@ import { useContext } from "react"; * @param options - Additional options * @returns Polkadot-API typed API */ -export const useTypedApi = ( +export function useTypedApi( options?: ChainHookOptions, ): TypedApi< TChainId extends void ? CommonDescriptor : Chains[Exclude] -> => { +> { const contextChainId = useContext(ChainIdContext); const chainId = options?.chainId ?? contextChainId; @@ -26,4 +26,4 @@ export const useTypedApi = ( } return useAtomValue(typedApiAtomFamily(chainId)); -}; +} diff --git a/packages/react/src/hooks/useWallets.ts b/packages/react/src/hooks/useWallets.ts index 71cbcc33..5ccdbe49 100644 --- a/packages/react/src/hooks/useWallets.ts +++ b/packages/react/src/hooks/useWallets.ts @@ -6,11 +6,15 @@ import { useAtomValue } from "jotai"; * * @returns Available wallets */ -export const useWallets = () => useAtomValue(walletsAtom); +export function useWallets() { + return useAtomValue(walletsAtom); +} /** * Hook for getting all connected wallets. * * @returns Connected wallets */ -export const useConnectedWallets = () => useAtomValue(connectedWalletsAtom); +export function useConnectedWallets() { + return useAtomValue(connectedWalletsAtom); +} diff --git a/packages/react/src/stores/query.ts b/packages/react/src/stores/query.ts index 638c0d5b..b1b48d71 100644 --- a/packages/react/src/stores/query.ts +++ b/packages/react/src/stores/query.ts @@ -51,11 +51,11 @@ const instructionPayloadAtomFamily = atomFamily( (a, b) => stringify(a) === stringify(b), ); -export const getQueryInstructionPayloadAtoms = ( +export function getQueryInstructionPayloadAtoms( chainId: ChainId, query: Query, -) => - query.instructions.map((instruction) => { +) { + return query.instructions.map((instruction) => { if (!("multi" in instruction)) { return instructionPayloadAtomFamily({ chainId: chainId, @@ -72,6 +72,7 @@ export const getQueryInstructionPayloadAtoms = ( }); }); }); +} // TODO: should be memoized within render function instead // https://github.com/pmndrs/jotai/discussions/1553 diff --git a/packages/react/src/utils/jotai.ts b/packages/react/src/utils/jotai.ts index 4dfa12aa..8f2529a3 100644 --- a/packages/react/src/utils/jotai.ts +++ b/packages/react/src/utils/jotai.ts @@ -29,7 +29,7 @@ export class AtomFamilyError extends QueryError { } } -export const withAtomFamilyErrorCatcher = < +export function withAtomFamilyErrorCatcher< TParam, // eslint-disable-next-line @typescript-eslint/no-explicit-any TRead extends (get: Getter, ...args: unknown[]) => any, @@ -39,7 +39,7 @@ export const withAtomFamilyErrorCatcher = < atomFamily: AtomFamily, param: TParam, atomCreator: TAtomCreator, -): TAtomCreator => { +): TAtomCreator { // @ts-expect-error complex sub-type const atomCatching: TAtomCreator = (read, ...args) => { // @ts-expect-error complex sub-type @@ -75,9 +75,9 @@ export const withAtomFamilyErrorCatcher = < }; return atomCatching; -}; +} -export const resetQueryError = (error: unknown) => { +export function resetQueryError(error: unknown) { if (!(error instanceof Error)) { return; } @@ -89,4 +89,4 @@ export const resetQueryError = (error: unknown) => { if (error.cause instanceof Error) { resetQueryError(error.cause); } -}; +} diff --git a/packages/react/src/utils/vanilla.ts b/packages/react/src/utils/vanilla.ts index a594e26f..a936c90c 100644 --- a/packages/react/src/utils/vanilla.ts +++ b/packages/react/src/utils/vanilla.ts @@ -1,8 +1,8 @@ -const hasObjectPrototype = (o: unknown) => { +function hasObjectPrototype(o: unknown) { return Object.prototype.toString.call(o) === "[object Object]"; -}; +} -const isPlainObject = (value: unknown): value is object => { +function isPlainObject(value: unknown): value is object { if (!hasObjectPrototype(value)) { return false; } @@ -22,9 +22,9 @@ const isPlainObject = (value: unknown): value is object => { // Most likely a plain Object return true; -}; +} -export const stringify = (queryInstruction: T) => { +export function stringify(queryInstruction: T) { return JSON.stringify(queryInstruction, (_, value) => { if (typeof value === "bigint") { return value.toString(); @@ -45,13 +45,13 @@ export const stringify = (queryInstruction: T) => { return value; }); -}; +} -export const flatHead = (value: T): T extends [infer Head] ? Head : T => { +export function flatHead(value: T): T extends [infer Head] ? Head : T { if (Array.isArray(value) && value.length === 1) { return value.at(0); } // @ts-expect-error TODO: fix this return value; -}; +}