@bigmi/react 0.1.1 → 0.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/esm/context.d.ts +4 -3
- package/dist/esm/context.js +1 -1
- package/dist/esm/context.js.map +1 -1
- package/dist/esm/errors/context.d.ts +1 -1
- package/dist/esm/errors/context.js +1 -1
- package/dist/esm/errors/context.js.map +1 -1
- package/dist/esm/hooks/useAccount.d.ts +5 -2
- package/dist/esm/hooks/useAccount.js +1 -2
- package/dist/esm/hooks/useAccount.js.map +1 -1
- package/dist/esm/hooks/useConfig.d.ts +7 -2
- package/dist/esm/hooks/useConfig.js +0 -1
- package/dist/esm/hooks/useConfig.js.map +1 -1
- package/dist/esm/hooks/useConnect.d.ts +15 -0
- package/dist/esm/hooks/useConnect.js +30 -0
- package/dist/esm/hooks/useConnect.js.map +1 -0
- package/dist/esm/hooks/useConnectors.d.ts +7 -0
- package/dist/esm/hooks/useConnectors.js +10 -0
- package/dist/esm/hooks/useConnectors.js.map +1 -0
- package/dist/esm/hooks/useReconnect.d.ts +1 -1
- package/dist/esm/hooks/useReconnect.js +1 -0
- package/dist/esm/hooks/useReconnect.js.map +1 -1
- package/dist/esm/hooks/useSyncExternalStoreWithTracked.d.ts +1 -1
- package/dist/esm/hooks/useSyncExternalStoreWithTracked.js +36 -35
- package/dist/esm/hooks/useSyncExternalStoreWithTracked.js.map +1 -1
- package/dist/esm/hydrate.d.ts +17 -0
- package/dist/esm/hydrate.js +61 -0
- package/dist/esm/hydrate.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +7 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils/query.d.ts +4 -0
- package/dist/esm/utils/query.js +3 -0
- package/dist/esm/utils/query.js.map +1 -0
- package/package.json +6 -8
- package/package.json.tmp +8 -8
- package/src/context.ts +4 -2
- package/src/errors/context.ts +1 -1
- package/src/hooks/useAccount.ts +16 -9
- package/src/hooks/useConfig.ts +12 -7
- package/src/hooks/useConnect.ts +90 -0
- package/src/hooks/useConnectors.ts +32 -0
- package/src/hooks/useReconnect.ts +4 -2
- package/src/hooks/useSyncExternalStoreWithTracked.ts +62 -52
- package/src/hydrate.ts +83 -0
- package/src/index.ts +2 -0
- package/src/types.ts +9 -0
- package/src/utils/query.ts +31 -0
package/README.md
CHANGED
|
@@ -45,22 +45,24 @@ Here is an example of a basic usage:
|
|
|
45
45
|
```tsx
|
|
46
46
|
import { useConfig } from '@bigmi/react'
|
|
47
47
|
import {
|
|
48
|
-
type
|
|
48
|
+
type UTXOSchema,
|
|
49
49
|
bitcoin,
|
|
50
50
|
getBalance,
|
|
51
51
|
getBlockCount,
|
|
52
52
|
sendUTXOTransaction,
|
|
53
53
|
utxo,
|
|
54
54
|
waitForTransaction,
|
|
55
|
+
createClient,
|
|
56
|
+
fallback,
|
|
57
|
+
rpcSchema
|
|
55
58
|
} from '@bigmi/core'
|
|
56
|
-
import {
|
|
57
|
-
import { useAccount } from 'wagmi'
|
|
59
|
+
import { useAccount } from '@bigmi/react'
|
|
58
60
|
|
|
59
61
|
|
|
60
62
|
// Create a public client for interactions with the Bitcoin
|
|
61
63
|
const publicClient = createClient({
|
|
62
64
|
chain: bitcoin,
|
|
63
|
-
rpcSchema: rpcSchema<
|
|
65
|
+
rpcSchema: rpcSchema<UTXOSchema>(),
|
|
64
66
|
transport: fallback([
|
|
65
67
|
utxo('https://api.blockchair.com', {
|
|
66
68
|
key: 'blockchair',
|
|
@@ -110,7 +112,7 @@ console.log('Transaction confirmed:', transaction);
|
|
|
110
112
|
|
|
111
113
|
// Getting account information inside the React application
|
|
112
114
|
const bigmiConfig = useConfig()
|
|
113
|
-
const account = useAccount(
|
|
115
|
+
const account = useAccount()
|
|
114
116
|
|
|
115
117
|
console.log('Bitcoin account address:', account.address);
|
|
116
118
|
```
|
package/dist/esm/context.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { State } from '@bigmi/client';
|
|
1
2
|
import { type PropsWithChildren } from 'react';
|
|
2
|
-
import type { ResolvedRegister
|
|
3
|
-
export declare const BigmiContext: import("react").Context<import("
|
|
3
|
+
import type { ResolvedRegister } from './types.js';
|
|
4
|
+
export declare const BigmiContext: import("react").Context<import("@bigmi/client").Config | undefined>;
|
|
4
5
|
export type BigmiProviderProps = {
|
|
5
6
|
config: ResolvedRegister['config'];
|
|
6
7
|
initialState?: State | undefined;
|
|
7
8
|
reconnectOnMount?: boolean | undefined;
|
|
8
9
|
};
|
|
9
|
-
export declare function BigmiProvider(parameters: PropsWithChildren<BigmiProviderProps>): import("react").FunctionComponentElement<PropsWithChildren<import("
|
|
10
|
+
export declare function BigmiProvider(parameters: PropsWithChildren<BigmiProviderProps>): import("react").FunctionComponentElement<PropsWithChildren<import("./hydrate.js").HydrateProps>>;
|
package/dist/esm/context.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { createContext, createElement } from 'react';
|
|
3
|
-
import { Hydrate } from '
|
|
3
|
+
import { Hydrate } from './hydrate.js';
|
|
4
4
|
export const BigmiContext = createContext(undefined);
|
|
5
5
|
export function BigmiProvider(parameters) {
|
|
6
6
|
const { children, config } = parameters;
|
package/dist/esm/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAA0B,aAAa,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAE5E,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAEvC,SAAS,CAAC,CAAA;AAQZ,MAAM,UAAU,aAAa,CAC3B,UAAiD;IAEjD,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,UAAU,CAAA;IAEvC,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;IAC/B,OAAO,aAAa,CAClB,OAAO,EACP,UAAU,EACV,aAAa,CAAC,YAAY,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,CACtD,CAAA;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/errors/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/errors/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAKvC,MAAM,OAAO,0BAA2B,SAAQ,SAAS;IAEvD;QACE,KAAK,CAAC,kDAAkD,CAAC,CAAA;QAFlD,SAAI,GAAG,4BAA4B,CAAA;IAG5C,CAAC;CACF"}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import { type Config, type GetAccountReturnType } from '@bigmi/client';
|
|
2
|
+
import type { ResolvedRegister } from '../types.js';
|
|
3
|
+
import { type ConfigParameter } from './useConfig.js';
|
|
4
|
+
export type UseAccountParameters<config extends Config = Config> = ConfigParameter<config>;
|
|
5
|
+
export type UseAccountReturnType<config extends Config = Config> = GetAccountReturnType<config>;
|
|
3
6
|
export declare function useAccount<C extends Config = ResolvedRegister['config']>(parameters?: UseAccountParameters<C>): UseAccountReturnType<C>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { getAccount, watchAccount } from '
|
|
2
|
+
import { getAccount, watchAccount, } from '@bigmi/client';
|
|
3
3
|
import { useConfig } from './useConfig.js';
|
|
4
4
|
import { useSyncExternalStoreWithTracked } from './useSyncExternalStoreWithTracked.js';
|
|
5
|
-
/** https://wagmi.sh/react/api/hooks/useAccount */
|
|
6
5
|
export function useAccount(parameters = {}) {
|
|
7
6
|
const config = useConfig(parameters);
|
|
8
7
|
return useSyncExternalStoreWithTracked((onChange) => watchAccount(config, { onChange }), () => getAccount(config));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAccount.js","sourceRoot":"","sources":["../../../src/hooks/useAccount.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"useAccount.js","sourceRoot":"","sources":["../../../src/hooks/useAccount.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAGL,UAAU,EACV,YAAY,GACb,MAAM,eAAe,CAAA;AAGtB,OAAO,EAAwB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAChE,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAA;AAQtF,MAAM,UAAU,UAAU,CACxB,aAAsC,EAAE;IAExC,MAAM,MAAM,GAAG,SAAS,CAAC,UAAU,CAAC,CAAA;IAEpC,OAAO,+BAA+B,CACpC,CAAC,QAAQ,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,EAChD,GAAG,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CACzB,CAAA;AACH,CAAC"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
import type { Config
|
|
2
|
-
|
|
1
|
+
import type { Config } from '@bigmi/client';
|
|
2
|
+
import type { ResolvedRegister } from '../types.js';
|
|
3
|
+
export type ConfigParameter<config extends Config = Config> = {
|
|
4
|
+
config?: Config | config | undefined;
|
|
5
|
+
};
|
|
6
|
+
export type UseConfigParameters<config extends Config = Config> = ConfigParameter<config>;
|
|
7
|
+
export type UseConfigReturnType<config extends Config = Config> = config;
|
|
3
8
|
export declare function useConfig<C extends Config = ResolvedRegister['config']>(parameters?: UseConfigParameters<C>): UseConfigReturnType<C>;
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import { useContext } from 'react';
|
|
3
3
|
import { BigmiContext } from '../context.js';
|
|
4
4
|
import { BigmiProviderNotFoundError } from '../errors/context.js';
|
|
5
|
-
/** https://wagmi.sh/react/api/hooks/useConfig */
|
|
6
5
|
export function useConfig(parameters = {}) {
|
|
7
6
|
// biome-ignore lint/correctness/useHookAtTopLevel:
|
|
8
7
|
const config = parameters.config ?? useContext(BigmiContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useConfig.js","sourceRoot":"","sources":["../../../src/hooks/useConfig.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"useConfig.js","sourceRoot":"","sources":["../../../src/hooks/useConfig.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAGlC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAA;AAYjE,MAAM,UAAU,SAAS,CACvB,aAAqC,EAAE;IAEvC,mDAAmD;IACnD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,IAAI,UAAU,CAAC,YAAY,CAAC,CAAA;IAC5D,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,0BAA0B,EAAE,CAAA;IACxC,CAAC;IACD,OAAO,MAAgC,CAAA;AACzC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Config, type ConnectData, type ConnectErrorType, type ConnectMutate, type ConnectMutateAsync, type ConnectVariables } from '@bigmi/client';
|
|
2
|
+
import type { Compute } from '@bigmi/core';
|
|
3
|
+
import type { ResolvedRegister } from '../types.js';
|
|
4
|
+
import type { UseMutationParameters, UseMutationReturnType } from '../utils/query.js';
|
|
5
|
+
import { type ConfigParameter } from './useConfig.js';
|
|
6
|
+
import { type UseConnectorsReturnType } from './useConnectors.js';
|
|
7
|
+
export type UseConnectParameters<config extends Config = Config, context = unknown> = Compute<ConfigParameter<config> & {
|
|
8
|
+
mutation?: UseMutationParameters<ConnectData<config>, ConnectErrorType, ConnectVariables<config, config['connectors'][number]>, context> | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
export type UseConnectReturnType<config extends Config = Config, context = unknown> = Compute<UseMutationReturnType<ConnectData<config>, ConnectErrorType, ConnectVariables<config, config['connectors'][number]>, context> & {
|
|
11
|
+
connect: ConnectMutate<config, context>;
|
|
12
|
+
connectAsync: ConnectMutateAsync<config, context>;
|
|
13
|
+
connectors: Compute<UseConnectorsReturnType> | config['connectors'];
|
|
14
|
+
}>;
|
|
15
|
+
export declare function useConnect<config extends Config = ResolvedRegister['config'], context = unknown>(parameters?: UseConnectParameters<config, context>): UseConnectReturnType<config, context>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { connectMutationOptions, } from '@bigmi/client';
|
|
3
|
+
import { useMutation } from '@tanstack/react-query';
|
|
4
|
+
import { useEffect } from 'react';
|
|
5
|
+
import { useConfig } from './useConfig.js';
|
|
6
|
+
import { useConnectors } from './useConnectors.js';
|
|
7
|
+
export function useConnect(parameters = {}) {
|
|
8
|
+
const { mutation } = parameters;
|
|
9
|
+
const config = useConfig(parameters);
|
|
10
|
+
const mutationOptions = connectMutationOptions(config);
|
|
11
|
+
const { mutate, mutateAsync, ...result } = useMutation({
|
|
12
|
+
...mutation,
|
|
13
|
+
...mutationOptions,
|
|
14
|
+
});
|
|
15
|
+
// Reset mutation back to an idle state when the connector disconnects.
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
return config.subscribe(({ status }) => status, (status, previousStatus) => {
|
|
18
|
+
if (previousStatus === 'connected' && status === 'disconnected') {
|
|
19
|
+
result.reset();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}, [config, result.reset]);
|
|
23
|
+
return {
|
|
24
|
+
...result,
|
|
25
|
+
connect: mutate,
|
|
26
|
+
connectAsync: mutateAsync,
|
|
27
|
+
connectors: useConnectors({ config }),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=useConnect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConnect.js","sourceRoot":"","sources":["../../../src/hooks/useConnect.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAOL,sBAAsB,GACvB,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAMjC,OAAO,EAAwB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAChE,OAAO,EAAgC,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAkChF,MAAM,UAAU,UAAU,CAIxB,aAAoD,EAAE;IAEtD,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAA;IAE/B,MAAM,MAAM,GAAG,SAAS,CAAC,UAAU,CAAC,CAAA;IAEpC,MAAM,eAAe,GAAG,sBAAsB,CAAC,MAAM,CAAC,CAAA;IACtD,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,EAAE,GAAG,WAAW,CAAC;QACrD,GAAG,QAAQ;QACX,GAAG,eAAe;KACnB,CAAC,CAAA;IAEF,uEAAuE;IACvE,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,MAAM,CAAC,SAAS,CACrB,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,MAAM,EACtB,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE;YACzB,IAAI,cAAc,KAAK,WAAW,IAAI,MAAM,KAAK,cAAc,EAAE,CAAC;gBAChE,MAAM,CAAC,KAAK,EAAE,CAAA;YAChB,CAAC;QACH,CAAC,CACF,CAAA;IACH,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;IAG1B,OAAO;QACL,GAAI,MAAiB;QACrB,OAAO,EAAE,MAA2B;QACpC,YAAY,EAAE,WAAqC;QACnD,UAAU,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KACtC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Config, type GetConnectorsReturnType } from '@bigmi/client';
|
|
2
|
+
import type { ResolvedRegister } from '../types.js';
|
|
3
|
+
import { type ConfigParameter } from './useConfig.js';
|
|
4
|
+
export type UseConnectorsParameters<config extends Config = Config> = ConfigParameter<config>;
|
|
5
|
+
export type UseConnectorsReturnType<config extends Config = Config> = GetConnectorsReturnType<config>;
|
|
6
|
+
/** https://wagmi.sh/react/api/hooks/useConnectors */
|
|
7
|
+
export declare function useConnectors<config extends Config = ResolvedRegister['config']>(parameters?: UseConnectorsParameters<config>): UseConnectorsReturnType<config>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { getConnectors, watchConnectors, } from '@bigmi/client';
|
|
3
|
+
import { useSyncExternalStore } from 'react';
|
|
4
|
+
import { useConfig } from './useConfig.js';
|
|
5
|
+
/** https://wagmi.sh/react/api/hooks/useConnectors */
|
|
6
|
+
export function useConnectors(parameters = {}) {
|
|
7
|
+
const config = useConfig(parameters);
|
|
8
|
+
return useSyncExternalStore((onChange) => watchConnectors(config, { onChange }), () => getConnectors(config), () => getConnectors(config));
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=useConnectors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConnectors.js","sourceRoot":"","sources":["../../../src/hooks/useConnectors.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAGL,aAAa,EACb,eAAe,GAChB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAA;AAE5C,OAAO,EAAwB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAQhE,qDAAqD;AACrD,MAAM,UAAU,aAAa,CAG3B,aAA8C,EAAE;IAEhD,MAAM,MAAM,GAAG,SAAS,CAAC,UAAU,CAAC,CAAA;IAEpC,OAAO,oBAAoB,CACzB,CAAC,QAAQ,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,EACnD,GAAG,EAAE,CAAC,aAAa,CAAC,MAAM,CAAC,EAC3B,GAAG,EAAE,CAAC,aAAa,CAAC,MAAM,CAAC,CAC5B,CAAA;AACH,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Config } from '@bigmi/client';
|
|
2
2
|
export declare const useReconnect: (config: Config) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReconnect.js","sourceRoot":"","sources":["../../../src/hooks/useReconnect.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"useReconnect.js","sourceRoot":"","sources":["../../../src/hooks/useReconnect.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAe,SAAS,EAAE,MAAM,eAAe,CAAA;AAEtD,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEjC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAc,EAAE,EAAE;IAC7C,SAAS,CAAC,GAAG,EAAE;QACb,SAAS,CAAC,MAAM,CAAC,CAAA;IACnB,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;AACd,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function useSyncExternalStoreWithTracked<
|
|
1
|
+
export declare function useSyncExternalStoreWithTracked<Snapshot extends Selection, Selection = Snapshot>(subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => Snapshot, getServerSnapshot?: (() => Snapshot) | undefined, isEqual?: (a: Selection, b: Selection) => boolean): Snapshot | NonNullable<Selection>;
|
|
@@ -1,43 +1,44 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { deepEqual } from 'wagmi';
|
|
2
|
+
import { deepEqual } from '@bigmi/core';
|
|
3
|
+
import { useMemo, useRef, useSyncExternalStore } from 'react';
|
|
5
4
|
const isPlainObject = (obj) => typeof obj === 'object' && !Array.isArray(obj);
|
|
6
5
|
export function useSyncExternalStoreWithTracked(subscribe, getSnapshot, getServerSnapshot = getSnapshot, isEqual = deepEqual) {
|
|
7
|
-
const trackedKeys = useRef(
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return
|
|
6
|
+
const trackedKeys = useRef(new Set());
|
|
7
|
+
const previousResult = useRef(undefined);
|
|
8
|
+
const snapshotCache = useRef(getSnapshot());
|
|
9
|
+
const snapshot = useSyncExternalStore((onChange) => subscribe(() => {
|
|
10
|
+
snapshotCache.current = getSnapshot();
|
|
11
|
+
onChange();
|
|
12
|
+
}), () => snapshotCache.current, getServerSnapshot);
|
|
13
|
+
const result = useMemo(() => {
|
|
14
|
+
if (!isPlainObject(snapshot)) {
|
|
15
|
+
return snapshot;
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
configurable: false,
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: () => {
|
|
30
|
-
if (!trackedKeys.current.includes(key)) {
|
|
31
|
-
trackedKeys.current.push(key);
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
},
|
|
17
|
+
const trackedResult = { ...snapshot };
|
|
18
|
+
Object.defineProperties(trackedResult, Object.fromEntries(Object.entries(trackedResult).map(([key, value]) => [
|
|
19
|
+
key,
|
|
20
|
+
{
|
|
21
|
+
configurable: false,
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: () => {
|
|
24
|
+
trackedKeys.current.add(key);
|
|
25
|
+
return value;
|
|
35
26
|
},
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
Object.defineProperties(trackedResult, properties);
|
|
27
|
+
},
|
|
28
|
+
])));
|
|
39
29
|
return trackedResult;
|
|
40
|
-
}
|
|
41
|
-
return
|
|
30
|
+
}, [snapshot]);
|
|
31
|
+
return useMemo(() => {
|
|
32
|
+
if (!trackedKeys.current.size || !previousResult.current) {
|
|
33
|
+
previousResult.current = result;
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
const hasChanged = Array.from(trackedKeys.current).some((key) => !isEqual(result[key], previousResult.current[key]));
|
|
37
|
+
if (!hasChanged) {
|
|
38
|
+
return previousResult.current;
|
|
39
|
+
}
|
|
40
|
+
previousResult.current = result;
|
|
41
|
+
return result;
|
|
42
|
+
}, [result, isEqual]);
|
|
42
43
|
}
|
|
43
44
|
//# sourceMappingURL=useSyncExternalStoreWithTracked.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSyncExternalStoreWithTracked.js","sourceRoot":"","sources":["../../../src/hooks/useSyncExternalStoreWithTracked.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useSyncExternalStoreWithTracked.js","sourceRoot":"","sources":["../../../src/hooks/useSyncExternalStoreWithTracked.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAA;AAE7D,MAAM,aAAa,GAAG,CAAC,GAAY,EAA8B,EAAE,CACjE,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AAEhD,MAAM,UAAU,+BAA+B,CAI7C,SAAoD,EACpD,WAA2B,EAC3B,oBAAkD,WAAW,EAC7D,UAAmD,SAAS;IAE5D,MAAM,WAAW,GAAG,MAAM,CAAc,IAAI,GAAG,EAAE,CAAC,CAAA;IAClD,MAAM,cAAc,GAAG,MAAM,CAAwB,SAAS,CAAC,CAAA;IAC/D,MAAM,aAAa,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC,CAAA;IAE3C,MAAM,QAAQ,GAAG,oBAAoB,CACnC,CAAC,QAAQ,EAAE,EAAE,CACX,SAAS,CAAC,GAAG,EAAE;QACb,aAAa,CAAC,OAAO,GAAG,WAAW,EAAE,CAAA;QACrC,QAAQ,EAAE,CAAA;IACZ,CAAC,CAAC,EACJ,GAAG,EAAE,CAAC,aAAa,CAAC,OAAQ,EAC5B,iBAAiB,CAClB,CAAA;IAED,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE;QAC1B,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC7B,OAAO,QAAQ,CAAA;QACjB,CAAC;QAED,MAAM,aAAa,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAA;QACrC,MAAM,CAAC,gBAAgB,CACrB,aAAa,EACb,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;YAClD,GAAG;YACH;gBACE,YAAY,EAAE,KAAK;gBACnB,UAAU,EAAE,IAAI;gBAChB,GAAG,EAAE,GAAG,EAAE;oBACR,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;oBAC5B,OAAO,KAAK,CAAA;gBACd,CAAC;aACF;SACF,CAAC,CACH,CACF,CAAA;QACD,OAAO,aAAa,CAAA;IACtB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEd,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,CAAC;YACzD,cAAc,CAAC,OAAO,GAAG,MAAM,CAAA;YAC/B,OAAO,MAAM,CAAA;QACf,CAAC;QAED,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,IAAI,CACrD,CAAC,GAAG,EAAE,EAAE,CACN,CAAC,OAAO,CACL,MAAoC,CAAC,GAAG,CAAC,EACzC,cAAc,CAAC,OAAqC,CAAC,GAAG,CAAC,CAC3D,CACJ,CAAA;QAED,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO,cAAc,CAAC,OAAO,CAAA;QAC/B,CAAC;QACD,cAAc,CAAC,OAAO,GAAG,MAAM,CAAA;QAC/B,OAAO,MAAM,CAAA;IACf,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;AACvB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Config, type State } from '@bigmi/client';
|
|
2
|
+
import { type ReactElement } from 'react';
|
|
3
|
+
import type { ResolvedRegister } from './types.js';
|
|
4
|
+
export type HydrateProps = {
|
|
5
|
+
config: ResolvedRegister['config'];
|
|
6
|
+
initialState?: State | undefined;
|
|
7
|
+
reconnectOnMount?: boolean | undefined;
|
|
8
|
+
};
|
|
9
|
+
type HydrateParameters = {
|
|
10
|
+
initialState?: State | undefined;
|
|
11
|
+
reconnectOnMount?: boolean | undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare function hydrate(config: Config, parameters: HydrateParameters): {
|
|
14
|
+
onMount(): Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
export declare function Hydrate(parameters: React.PropsWithChildren<HydrateProps>): ReactElement;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { reconnect } from '@bigmi/client';
|
|
3
|
+
import { useEffect, useRef } from 'react';
|
|
4
|
+
export function hydrate(config, parameters) {
|
|
5
|
+
const { initialState, reconnectOnMount } = parameters;
|
|
6
|
+
if (initialState && !config._internal.store.persist.hasHydrated()) {
|
|
7
|
+
config.setState({
|
|
8
|
+
...initialState,
|
|
9
|
+
chainId: config.chains.some((x) => x.id === initialState.chainId)
|
|
10
|
+
? initialState.chainId
|
|
11
|
+
: config.chains[0].id,
|
|
12
|
+
connections: reconnectOnMount ? initialState.connections : new Map(),
|
|
13
|
+
status: reconnectOnMount ? 'reconnecting' : 'disconnected',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
async onMount() {
|
|
18
|
+
if (config._internal.ssr) {
|
|
19
|
+
await config._internal.store.persist.rehydrate();
|
|
20
|
+
}
|
|
21
|
+
if (reconnectOnMount) {
|
|
22
|
+
reconnect(config);
|
|
23
|
+
}
|
|
24
|
+
else if (config.storage) {
|
|
25
|
+
// Reset connections that may have been hydrated from storage.
|
|
26
|
+
config.setState((x) => ({
|
|
27
|
+
...x,
|
|
28
|
+
connections: new Map(),
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function Hydrate(parameters) {
|
|
35
|
+
const { children, config, initialState, reconnectOnMount = true } = parameters;
|
|
36
|
+
const { onMount } = hydrate(config, {
|
|
37
|
+
initialState,
|
|
38
|
+
reconnectOnMount,
|
|
39
|
+
});
|
|
40
|
+
// Hydrate for non-SSR
|
|
41
|
+
if (!config._internal.ssr) {
|
|
42
|
+
onMount();
|
|
43
|
+
}
|
|
44
|
+
// Hydrate for SSR
|
|
45
|
+
const active = useRef(true);
|
|
46
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `queryKey` not required
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!active.current) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!config._internal.ssr) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
onMount();
|
|
55
|
+
return () => {
|
|
56
|
+
active.current = false;
|
|
57
|
+
};
|
|
58
|
+
}, []);
|
|
59
|
+
return children;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=hydrate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hydrate.js","sourceRoot":"","sources":["../../src/hydrate.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAA2B,SAAS,EAAE,MAAM,eAAe,CAAA;AAElE,OAAO,EAAqB,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAe5D,MAAM,UAAU,OAAO,CAAC,MAAc,EAAE,UAA6B;IACnE,MAAM,EAAE,YAAY,EAAE,gBAAgB,EAAE,GAAG,UAAU,CAAA;IAErD,IAAI,YAAY,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;QAClE,MAAM,CAAC,QAAQ,CAAC;YACd,GAAG,YAAY;YACf,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,YAAY,CAAC,OAAO,CAAC;gBAC/D,CAAC,CAAC,YAAY,CAAC,OAAO;gBACtB,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE;YACvB,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE;YACpE,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,cAAc;SAC3D,CAAC,CAAA;IACJ,CAAC;IAED,OAAO;QACL,KAAK,CAAC,OAAO;YACX,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;gBACzB,MAAM,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;YAClD,CAAC;YAED,IAAI,gBAAgB,EAAE,CAAC;gBACrB,SAAS,CAAC,MAAM,CAAC,CAAA;YACnB,CAAC;iBAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBAC1B,8DAA8D;gBAC9D,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;oBACtB,GAAG,CAAC;oBACJ,WAAW,EAAE,IAAI,GAAG,EAAE;iBACvB,CAAC,CAAC,CAAA;YACL,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,UAAiD;IACvE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,gBAAgB,GAAG,IAAI,EAAE,GAAG,UAAU,CAAA;IAE9E,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE;QAClC,YAAY;QACZ,gBAAgB;KACjB,CAAC,CAAA;IAEF,sBAAsB;IACtB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAA;IACX,CAAC;IAED,kBAAkB;IAClB,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAA;IAC3B,mFAAmF;IACnF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,OAAM;QACR,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;YAC1B,OAAM;QACR,CAAC;QACD,OAAO,EAAE,CAAA;QACT,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,OAAO,GAAG,KAAK,CAAA;QACxB,CAAC,CAAA;IACH,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,QAAwB,CAAA;AACjC,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { BigmiContext, BigmiProvider } from './context.js';
|
|
|
2
2
|
export type { BigmiProviderProps } from './context.js';
|
|
3
3
|
export { useConfig } from './hooks/useConfig.js';
|
|
4
4
|
export { useReconnect } from './hooks/useReconnect.js';
|
|
5
|
+
export { useAccount } from './hooks/useAccount.js';
|
|
6
|
+
export { useConnect } from './hooks/useConnect.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { BigmiContext, BigmiProvider } from './context.js';
|
|
2
2
|
export { useConfig } from './hooks/useConfig.js';
|
|
3
3
|
export { useReconnect } from './hooks/useReconnect.js';
|
|
4
|
+
export { useAccount } from './hooks/useAccount.js';
|
|
5
|
+
export { useConnect } from './hooks/useConnect.js';
|
|
4
6
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAE1D,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAE1D,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Compute, UnionStrictOmit } from '@bigmi/core';
|
|
2
|
+
import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
export type UseMutationParameters<data = unknown, error = Error, variables = void, context = unknown> = Compute<Omit<UseMutationOptions<data, error, Compute<variables>, context>, 'mutationFn' | 'mutationKey' | 'throwOnError'>>;
|
|
4
|
+
export type UseMutationReturnType<data = unknown, error = Error, variables = void, context = unknown> = Compute<UnionStrictOmit<UseMutationResult<data, error, variables, context>, 'mutate' | 'mutateAsync'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/utils/query.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bigmi/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-beta.1",
|
|
4
4
|
"description": "React primitives for Bitcoin apps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/esm/index.js",
|
|
@@ -29,15 +29,13 @@
|
|
|
29
29
|
"hooks"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"@bigmi/client": "^0.1.1",
|
|
35
|
-
"@bigmi/core": "^0.1.1"
|
|
32
|
+
"@bigmi/client": "^0.2.0-beta.1",
|
|
33
|
+
"@bigmi/core": "^0.2.0-beta.1"
|
|
36
34
|
},
|
|
37
35
|
"peerDependencies": {
|
|
38
|
-
"react": "
|
|
39
|
-
"react
|
|
40
|
-
"
|
|
36
|
+
"@tanstack/react-query": "^5.74.11",
|
|
37
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
38
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
41
39
|
},
|
|
42
40
|
"exports": {
|
|
43
41
|
".": {
|
package/package.json.tmp
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bigmi/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-beta.1",
|
|
4
4
|
"description": "React primitives for Bitcoin apps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -43,20 +43,20 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@bigmi/client": "workspace:^",
|
|
46
|
-
"@bigmi/core": "workspace:^"
|
|
47
|
-
"use-sync-external-store": "^1.4.0",
|
|
48
|
-
"viem": "^2.22.21"
|
|
46
|
+
"@bigmi/core": "workspace:^"
|
|
49
47
|
},
|
|
50
48
|
"devDependencies": {
|
|
51
|
-
"@
|
|
49
|
+
"@tanstack/react-query": "^5.75.7",
|
|
52
50
|
"cpy-cli": "^5.0.0",
|
|
53
51
|
"madge": "^8.0.0",
|
|
52
|
+
"react": "^19.1.0",
|
|
53
|
+
"react-dom": "^19.1.0",
|
|
54
54
|
"typescript": "^5.7.3"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"react": "
|
|
58
|
-
"react
|
|
59
|
-
"
|
|
57
|
+
"@tanstack/react-query": "^5.74.11",
|
|
58
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
59
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
60
60
|
},
|
|
61
61
|
"files": ["dist/**", "src/**", "!tsconfig.json"]
|
|
62
62
|
}
|
package/src/context.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
'use client'
|
|
2
|
+
import type { State } from '@bigmi/client'
|
|
2
3
|
import { type PropsWithChildren, createContext, createElement } from 'react'
|
|
3
|
-
|
|
4
|
-
import { Hydrate } from '
|
|
4
|
+
|
|
5
|
+
import { Hydrate } from './hydrate.js'
|
|
6
|
+
import type { ResolvedRegister } from './types.js'
|
|
5
7
|
|
|
6
8
|
export const BigmiContext = createContext<
|
|
7
9
|
ResolvedRegister['config'] | undefined
|
package/src/errors/context.ts
CHANGED
package/src/hooks/useAccount.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type GetAccountReturnType,
|
|
6
|
+
getAccount,
|
|
7
|
+
watchAccount,
|
|
8
|
+
} from '@bigmi/client'
|
|
9
|
+
|
|
10
|
+
import type { ResolvedRegister } from '../types.js'
|
|
11
|
+
import { type ConfigParameter, useConfig } from './useConfig.js'
|
|
10
12
|
import { useSyncExternalStoreWithTracked } from './useSyncExternalStoreWithTracked.js'
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
export type UseAccountParameters<config extends Config = Config> =
|
|
15
|
+
ConfigParameter<config>
|
|
16
|
+
|
|
17
|
+
export type UseAccountReturnType<config extends Config = Config> =
|
|
18
|
+
GetAccountReturnType<config>
|
|
19
|
+
|
|
13
20
|
export function useAccount<C extends Config = ResolvedRegister['config']>(
|
|
14
21
|
parameters: UseAccountParameters<C> = {}
|
|
15
22
|
): UseAccountReturnType<C> {
|
package/src/hooks/useConfig.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { useContext } from 'react'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
ResolvedRegister,
|
|
6
|
-
UseConfigParameters,
|
|
7
|
-
UseConfigReturnType,
|
|
8
|
-
} from 'wagmi'
|
|
3
|
+
|
|
4
|
+
import type { Config } from '@bigmi/client'
|
|
9
5
|
import { BigmiContext } from '../context.js'
|
|
10
6
|
import { BigmiProviderNotFoundError } from '../errors/context.js'
|
|
7
|
+
import type { ResolvedRegister } from '../types.js'
|
|
8
|
+
|
|
9
|
+
export type ConfigParameter<config extends Config = Config> = {
|
|
10
|
+
config?: Config | config | undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type UseConfigParameters<config extends Config = Config> =
|
|
14
|
+
ConfigParameter<config>
|
|
15
|
+
|
|
16
|
+
export type UseConfigReturnType<config extends Config = Config> = config
|
|
11
17
|
|
|
12
|
-
/** https://wagmi.sh/react/api/hooks/useConfig */
|
|
13
18
|
export function useConfig<C extends Config = ResolvedRegister['config']>(
|
|
14
19
|
parameters: UseConfigParameters<C> = {}
|
|
15
20
|
): UseConfigReturnType<C> {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type ConnectData,
|
|
6
|
+
type ConnectErrorType,
|
|
7
|
+
type ConnectMutate,
|
|
8
|
+
type ConnectMutateAsync,
|
|
9
|
+
type ConnectVariables,
|
|
10
|
+
connectMutationOptions,
|
|
11
|
+
} from '@bigmi/client'
|
|
12
|
+
import type { Compute } from '@bigmi/core'
|
|
13
|
+
import { useMutation } from '@tanstack/react-query'
|
|
14
|
+
import { useEffect } from 'react'
|
|
15
|
+
import type { ResolvedRegister } from '../types.js'
|
|
16
|
+
import type {
|
|
17
|
+
UseMutationParameters,
|
|
18
|
+
UseMutationReturnType,
|
|
19
|
+
} from '../utils/query.js'
|
|
20
|
+
import { type ConfigParameter, useConfig } from './useConfig.js'
|
|
21
|
+
import { type UseConnectorsReturnType, useConnectors } from './useConnectors.js'
|
|
22
|
+
|
|
23
|
+
export type UseConnectParameters<
|
|
24
|
+
config extends Config = Config,
|
|
25
|
+
context = unknown,
|
|
26
|
+
> = Compute<
|
|
27
|
+
ConfigParameter<config> & {
|
|
28
|
+
mutation?:
|
|
29
|
+
| UseMutationParameters<
|
|
30
|
+
ConnectData<config>,
|
|
31
|
+
ConnectErrorType,
|
|
32
|
+
ConnectVariables<config, config['connectors'][number]>,
|
|
33
|
+
context
|
|
34
|
+
>
|
|
35
|
+
| undefined
|
|
36
|
+
}
|
|
37
|
+
>
|
|
38
|
+
|
|
39
|
+
export type UseConnectReturnType<
|
|
40
|
+
config extends Config = Config,
|
|
41
|
+
context = unknown,
|
|
42
|
+
> = Compute<
|
|
43
|
+
UseMutationReturnType<
|
|
44
|
+
ConnectData<config>,
|
|
45
|
+
ConnectErrorType,
|
|
46
|
+
ConnectVariables<config, config['connectors'][number]>,
|
|
47
|
+
context
|
|
48
|
+
> & {
|
|
49
|
+
connect: ConnectMutate<config, context>
|
|
50
|
+
connectAsync: ConnectMutateAsync<config, context>
|
|
51
|
+
connectors: Compute<UseConnectorsReturnType> | config['connectors']
|
|
52
|
+
}
|
|
53
|
+
>
|
|
54
|
+
|
|
55
|
+
export function useConnect<
|
|
56
|
+
config extends Config = ResolvedRegister['config'],
|
|
57
|
+
context = unknown,
|
|
58
|
+
>(
|
|
59
|
+
parameters: UseConnectParameters<config, context> = {}
|
|
60
|
+
): UseConnectReturnType<config, context> {
|
|
61
|
+
const { mutation } = parameters
|
|
62
|
+
|
|
63
|
+
const config = useConfig(parameters)
|
|
64
|
+
|
|
65
|
+
const mutationOptions = connectMutationOptions(config)
|
|
66
|
+
const { mutate, mutateAsync, ...result } = useMutation({
|
|
67
|
+
...mutation,
|
|
68
|
+
...mutationOptions,
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// Reset mutation back to an idle state when the connector disconnects.
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
return config.subscribe(
|
|
74
|
+
({ status }) => status,
|
|
75
|
+
(status, previousStatus) => {
|
|
76
|
+
if (previousStatus === 'connected' && status === 'disconnected') {
|
|
77
|
+
result.reset()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
}, [config, result.reset])
|
|
82
|
+
|
|
83
|
+
type Return = UseConnectReturnType<config, context>
|
|
84
|
+
return {
|
|
85
|
+
...(result as Return),
|
|
86
|
+
connect: mutate as Return['connect'],
|
|
87
|
+
connectAsync: mutateAsync as Return['connectAsync'],
|
|
88
|
+
connectors: useConnectors({ config }),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type GetConnectorsReturnType,
|
|
6
|
+
getConnectors,
|
|
7
|
+
watchConnectors,
|
|
8
|
+
} from '@bigmi/client'
|
|
9
|
+
import { useSyncExternalStore } from 'react'
|
|
10
|
+
import type { ResolvedRegister } from '../types.js'
|
|
11
|
+
import { type ConfigParameter, useConfig } from './useConfig.js'
|
|
12
|
+
|
|
13
|
+
export type UseConnectorsParameters<config extends Config = Config> =
|
|
14
|
+
ConfigParameter<config>
|
|
15
|
+
|
|
16
|
+
export type UseConnectorsReturnType<config extends Config = Config> =
|
|
17
|
+
GetConnectorsReturnType<config>
|
|
18
|
+
|
|
19
|
+
/** https://wagmi.sh/react/api/hooks/useConnectors */
|
|
20
|
+
export function useConnectors<
|
|
21
|
+
config extends Config = ResolvedRegister['config'],
|
|
22
|
+
>(
|
|
23
|
+
parameters: UseConnectorsParameters<config> = {}
|
|
24
|
+
): UseConnectorsReturnType<config> {
|
|
25
|
+
const config = useConfig(parameters)
|
|
26
|
+
|
|
27
|
+
return useSyncExternalStore(
|
|
28
|
+
(onChange) => watchConnectors(config, { onChange }),
|
|
29
|
+
() => getConnectors(config),
|
|
30
|
+
() => getConnectors(config)
|
|
31
|
+
)
|
|
32
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { type Config, reconnect } from '@bigmi/client'
|
|
4
|
+
|
|
2
5
|
import { useEffect } from 'react'
|
|
3
|
-
import type { Config } from 'wagmi'
|
|
4
6
|
|
|
5
7
|
export const useReconnect = (config: Config) => {
|
|
6
8
|
useEffect(() => {
|
|
@@ -1,66 +1,76 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { deepEqual } from 'wagmi'
|
|
2
|
+
import { deepEqual } from '@bigmi/core'
|
|
3
|
+
import { useMemo, useRef, useSyncExternalStore } from 'react'
|
|
5
4
|
|
|
6
|
-
const isPlainObject = (obj: unknown) =>
|
|
5
|
+
const isPlainObject = (obj: unknown): obj is Record<string, any> =>
|
|
7
6
|
typeof obj === 'object' && !Array.isArray(obj)
|
|
8
7
|
|
|
9
8
|
export function useSyncExternalStoreWithTracked<
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
Snapshot extends Selection,
|
|
10
|
+
Selection = Snapshot,
|
|
12
11
|
>(
|
|
13
12
|
subscribe: (onStoreChange: () => void) => () => void,
|
|
14
|
-
getSnapshot: () =>
|
|
15
|
-
getServerSnapshot:
|
|
16
|
-
isEqual: (a:
|
|
13
|
+
getSnapshot: () => Snapshot,
|
|
14
|
+
getServerSnapshot: (() => Snapshot) | undefined = getSnapshot,
|
|
15
|
+
isEqual: (a: Selection, b: Selection) => boolean = deepEqual
|
|
17
16
|
) {
|
|
18
|
-
const trackedKeys = useRef<string
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
)
|
|
31
|
-
if (!equal) {
|
|
32
|
-
return false
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return true
|
|
36
|
-
}
|
|
37
|
-
return isEqual(a, b)
|
|
38
|
-
}
|
|
17
|
+
const trackedKeys = useRef<Set<string>>(new Set())
|
|
18
|
+
const previousResult = useRef<Selection | undefined>(undefined)
|
|
19
|
+
const snapshotCache = useRef(getSnapshot())
|
|
20
|
+
|
|
21
|
+
const snapshot = useSyncExternalStore(
|
|
22
|
+
(onChange) =>
|
|
23
|
+
subscribe(() => {
|
|
24
|
+
snapshotCache.current = getSnapshot()
|
|
25
|
+
onChange()
|
|
26
|
+
}),
|
|
27
|
+
() => snapshotCache.current!,
|
|
28
|
+
getServerSnapshot
|
|
39
29
|
)
|
|
40
30
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
for (const [key, value] of Object.entries(
|
|
45
|
-
trackedResult as { [key: string]: any }
|
|
46
|
-
)) {
|
|
47
|
-
properties = {
|
|
48
|
-
...properties,
|
|
49
|
-
[key]: {
|
|
50
|
-
configurable: false,
|
|
51
|
-
enumerable: true,
|
|
52
|
-
get: () => {
|
|
53
|
-
if (!trackedKeys.current.includes(key)) {
|
|
54
|
-
trackedKeys.current.push(key)
|
|
55
|
-
}
|
|
56
|
-
return value
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
}
|
|
31
|
+
const result = useMemo(() => {
|
|
32
|
+
if (!isPlainObject(snapshot)) {
|
|
33
|
+
return snapshot
|
|
60
34
|
}
|
|
61
|
-
|
|
35
|
+
|
|
36
|
+
const trackedResult = { ...snapshot }
|
|
37
|
+
Object.defineProperties(
|
|
38
|
+
trackedResult,
|
|
39
|
+
Object.fromEntries(
|
|
40
|
+
Object.entries(trackedResult).map(([key, value]) => [
|
|
41
|
+
key,
|
|
42
|
+
{
|
|
43
|
+
configurable: false,
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: () => {
|
|
46
|
+
trackedKeys.current.add(key)
|
|
47
|
+
return value
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
])
|
|
51
|
+
)
|
|
52
|
+
)
|
|
62
53
|
return trackedResult
|
|
63
|
-
}
|
|
54
|
+
}, [snapshot])
|
|
64
55
|
|
|
65
|
-
return
|
|
56
|
+
return useMemo(() => {
|
|
57
|
+
if (!trackedKeys.current.size || !previousResult.current) {
|
|
58
|
+
previousResult.current = result
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const hasChanged = Array.from(trackedKeys.current).some(
|
|
63
|
+
(key) =>
|
|
64
|
+
!isEqual(
|
|
65
|
+
(result as Record<string, Selection>)[key],
|
|
66
|
+
(previousResult.current as Record<string, Selection>)[key]
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
if (!hasChanged) {
|
|
71
|
+
return previousResult.current
|
|
72
|
+
}
|
|
73
|
+
previousResult.current = result
|
|
74
|
+
return result
|
|
75
|
+
}, [result, isEqual])
|
|
66
76
|
}
|
package/src/hydrate.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { type Config, type State, reconnect } from '@bigmi/client'
|
|
4
|
+
|
|
5
|
+
import { type ReactElement, useEffect, useRef } from 'react'
|
|
6
|
+
|
|
7
|
+
import type { ResolvedRegister } from './types.js'
|
|
8
|
+
|
|
9
|
+
export type HydrateProps = {
|
|
10
|
+
config: ResolvedRegister['config']
|
|
11
|
+
initialState?: State | undefined
|
|
12
|
+
reconnectOnMount?: boolean | undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type HydrateParameters = {
|
|
16
|
+
initialState?: State | undefined
|
|
17
|
+
reconnectOnMount?: boolean | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function hydrate(config: Config, parameters: HydrateParameters) {
|
|
21
|
+
const { initialState, reconnectOnMount } = parameters
|
|
22
|
+
|
|
23
|
+
if (initialState && !config._internal.store.persist.hasHydrated()) {
|
|
24
|
+
config.setState({
|
|
25
|
+
...initialState,
|
|
26
|
+
chainId: config.chains.some((x) => x.id === initialState.chainId)
|
|
27
|
+
? initialState.chainId
|
|
28
|
+
: config.chains[0].id,
|
|
29
|
+
connections: reconnectOnMount ? initialState.connections : new Map(),
|
|
30
|
+
status: reconnectOnMount ? 'reconnecting' : 'disconnected',
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
async onMount() {
|
|
36
|
+
if (config._internal.ssr) {
|
|
37
|
+
await config._internal.store.persist.rehydrate()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (reconnectOnMount) {
|
|
41
|
+
reconnect(config)
|
|
42
|
+
} else if (config.storage) {
|
|
43
|
+
// Reset connections that may have been hydrated from storage.
|
|
44
|
+
config.setState((x) => ({
|
|
45
|
+
...x,
|
|
46
|
+
connections: new Map(),
|
|
47
|
+
}))
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function Hydrate(parameters: React.PropsWithChildren<HydrateProps>) {
|
|
54
|
+
const { children, config, initialState, reconnectOnMount = true } = parameters
|
|
55
|
+
|
|
56
|
+
const { onMount } = hydrate(config, {
|
|
57
|
+
initialState,
|
|
58
|
+
reconnectOnMount,
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// Hydrate for non-SSR
|
|
62
|
+
if (!config._internal.ssr) {
|
|
63
|
+
onMount()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Hydrate for SSR
|
|
67
|
+
const active = useRef(true)
|
|
68
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `queryKey` not required
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!active.current) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
if (!config._internal.ssr) {
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
onMount()
|
|
77
|
+
return () => {
|
|
78
|
+
active.current = false
|
|
79
|
+
}
|
|
80
|
+
}, [])
|
|
81
|
+
|
|
82
|
+
return children as ReactElement
|
|
83
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { BigmiContext, BigmiProvider } from './context.js'
|
|
|
2
2
|
export type { BigmiProviderProps } from './context.js'
|
|
3
3
|
export { useConfig } from './hooks/useConfig.js'
|
|
4
4
|
export { useReconnect } from './hooks/useReconnect.js'
|
|
5
|
+
export { useAccount } from './hooks/useAccount.js'
|
|
6
|
+
export { useConnect } from './hooks/useConnect.js'
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from '@bigmi/client'
|
|
2
|
+
|
|
3
|
+
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
4
|
+
export type Register = {}
|
|
5
|
+
export type ResolvedRegister = {
|
|
6
|
+
config: Register extends { config: infer config extends Config }
|
|
7
|
+
? config
|
|
8
|
+
: Config
|
|
9
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import type { Compute, UnionStrictOmit } from '@bigmi/core'
|
|
4
|
+
import type {
|
|
5
|
+
UseMutationOptions,
|
|
6
|
+
UseMutationResult,
|
|
7
|
+
} from '@tanstack/react-query'
|
|
8
|
+
|
|
9
|
+
export type UseMutationParameters<
|
|
10
|
+
data = unknown,
|
|
11
|
+
error = Error,
|
|
12
|
+
variables = void,
|
|
13
|
+
context = unknown,
|
|
14
|
+
> = Compute<
|
|
15
|
+
Omit<
|
|
16
|
+
UseMutationOptions<data, error, Compute<variables>, context>,
|
|
17
|
+
'mutationFn' | 'mutationKey' | 'throwOnError'
|
|
18
|
+
>
|
|
19
|
+
>
|
|
20
|
+
|
|
21
|
+
export type UseMutationReturnType<
|
|
22
|
+
data = unknown,
|
|
23
|
+
error = Error,
|
|
24
|
+
variables = void,
|
|
25
|
+
context = unknown,
|
|
26
|
+
> = Compute<
|
|
27
|
+
UnionStrictOmit<
|
|
28
|
+
UseMutationResult<data, error, variables, context>,
|
|
29
|
+
'mutate' | 'mutateAsync'
|
|
30
|
+
>
|
|
31
|
+
>
|