@kheopskit/react 5.3.0 → 6.0.0-rc.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # @kheopskit/react
2
2
 
3
- React bindings for [Kheopskit](https://github.com/kheopskit/kheopskit) — list
4
- wallets and accounts across Polkadot, Ethereum and Solana, with injected wallets
3
+ React bindings for [Kheopskit](https://github.com/kheopskit/kheopskit). They
4
+ list wallets and accounts across Polkadot, Ethereum and Solana, with injected wallets
5
5
  and WalletConnect (Reown AppKit).
6
6
 
7
7
  The framework-agnostic core lives in
@@ -13,10 +13,17 @@ https://github.com/kheopskit/kheopskit
13
13
  > (and WalletConnect) optional peer dependencies. See
14
14
  > [MIGRATING_TO_V4.md](../core/MIGRATING_TO_V4.md).
15
15
 
16
+ > **Upgrading from v5?** v6 makes `connect()` shared, idempotent and
17
+ > abortable, types its rejections, adds `status`, `isDiscovering`,
18
+ > `isPlaceholder` and `signRaw`, and moves WalletConnect to a plugin. See
19
+ > [MIGRATING_TO_V6.md](https://github.com/kheopskit/kheopskit/blob/main/packages/core/MIGRATING_TO_V6.md).
20
+ > The [core README](https://github.com/kheopskit/kheopskit/blob/main/packages/core/README.md#wallet-lifecycle)
21
+ > explains the wallet lifecycle that `useWallets()` reflects.
22
+
16
23
  ## Install
17
24
 
18
- `rxjs` is always required; every platform SDK (and WalletConnect) is an optional
19
- peer dependency — install only what you use:
25
+ `rxjs` is always required. Every platform SDK (and WalletConnect) is an
26
+ optional peer dependency, so install only what you use:
20
27
 
21
28
  ```bash
22
29
  pnpm add @kheopskit/core @kheopskit/react rxjs
@@ -27,10 +34,38 @@ pnpm add @solana/kit @wallet-standard/app @wallet-standard/base # Solana
27
34
  pnpm add @reown/appkit # WalletConnect (optional)
28
35
  ```
29
36
 
37
+ WalletConnect is a plugin from `@kheopskit/core/walletconnect`; only a dapp that
38
+ imports it bundles AppKit:
39
+
40
+ ```ts
41
+ import { createKheopskit } from "@kheopskit/react";
42
+ import { polkadot } from "@kheopskit/core/polkadot";
43
+ import { walletConnect } from "@kheopskit/core/walletconnect";
44
+ import { defineChain } from "@reown/appkit/networks";
45
+
46
+ const polkadotNetwork = defineChain({
47
+ id: "91b171bb158e2d3848fa23a9f1c25182",
48
+ name: "Polkadot",
49
+ nativeCurrency: { name: "Polkadot", symbol: "DOT", decimals: 10 },
50
+ rpcUrls: { default: { http: ["https://rpc.polkadot.io"] } },
51
+ chainNamespace: "polkadot",
52
+ caipNetworkId: "polkadot:91b171bb158e2d3848fa23a9f1c25182",
53
+ });
54
+
55
+ export const { KheopskitProvider, useWallets, useAccounts } = createKheopskit({
56
+ platforms: [polkadot()],
57
+ walletConnect: walletConnect({
58
+ projectId: "<your Reown project id>",
59
+ metadata: { name: "My Dapp", description: "", url: "https://my.dapp", icons: [] },
60
+ networks: [polkadotNetwork],
61
+ }),
62
+ });
63
+ ```
64
+
30
65
  ## Usage
31
66
 
32
67
  Recommended: bind your platform tuple once with `createKheopskit` and get a
33
- provider plus hooks already typed to those platforms — no generic to repeat.
68
+ provider plus hooks already typed to those platforms, with no generic to repeat.
34
69
 
35
70
  ```ts
36
71
  // kheopskit.ts
@@ -77,6 +112,134 @@ const platforms = [polkadot(), ethereum(), solana()] as const;
77
112
  const { accounts } = useWallets<typeof platforms>();
78
113
  ```
79
114
 
115
+ ## Hooks
116
+
117
+ | Hook | Returns | Re-renders |
118
+ |------|---------|------------|
119
+ | `useWallets()` | the whole state: `wallets`, `accounts`, `config`, `isHydrating`, `isDiscovering` | on every state change |
120
+ | `useAccounts()` | `useWallets().accounts` | on every state change |
121
+ | `useWallet(walletId)` | the wallet with this id, or `undefined` | when that wallet changes |
122
+ | `useAccount(accountId)` | the account with this id, or `undefined` | when that account changes |
123
+ | `useConnect(walletId)` | `{ connect, disconnect, status, error }` for one wallet | when that wallet changes |
124
+
125
+ `useWallet` and `useAccount` compare the item's fields, SDK handles by identity
126
+ and any two functions as equal. Another wallet connecting does not re-render
127
+ them.
128
+
129
+ A connect button with `useConnect`:
130
+
131
+ ```tsx
132
+ import type { WalletId } from "@kheopskit/core";
133
+ import { useConnect } from "@kheopskit/react";
134
+
135
+ const ConnectButton = ({ walletId }: { walletId: WalletId }) => {
136
+ const { connect, disconnect, status, error } = useConnect(walletId);
137
+ if (status === "connected") return <button onClick={disconnect}>Disconnect</button>;
138
+ return (
139
+ <>
140
+ <button disabled={status === "connecting"} onClick={connect}>
141
+ Connect
142
+ </button>
143
+ {error?.code === "USER_REJECTED" && <p>You refused the connection.</p>}
144
+ </>
145
+ );
146
+ };
147
+ ```
148
+
149
+ - `connect()` resolves once the state shows the wallet connected.
150
+ - If the component unmounts or `walletId` changes while `connect()` waits for
151
+ the extension, the hook aborts the connect. The wallet stays disconnected
152
+ and is not remembered, and `connect()` resolves.
153
+ - A `connect()` that starts after the component unmounted, such as from an
154
+ async handler that resumes later, or through a `connect` captured before
155
+ `walletId` changed, rejects `ABORTED` without asking the extension.
156
+ - A Suspense fallback shown over the component, or a StrictMode remount, aborts
157
+ nothing. Only a real unmount, an `<Activity>` hide or a `walletId` change
158
+ does.
159
+ - Every other failure rejects with the wallet's `KheopskitError`, such as
160
+ `USER_REJECTED`, and also shows in `error`. When the state lists no wallet
161
+ with this id, `connect` rejects with a plain `Error`.
162
+ - `connect` and `disconnect` keep their identity while `walletId` stays the
163
+ same. A `useEffect(() => { connect() }, [connect])` connects on mount and
164
+ again after every `<Activity>` reveal.
165
+
166
+ Every action a hook hands out acts on the instance's pipeline when it is
167
+ called: `connect` and `disconnect` from `useConnect`, from a `useWallet` wallet
168
+ and from a `useWallets()` wallet. The pipeline runs while anything holds it: a
169
+ mounted and visible provider of the instance, or a subscriber of its
170
+ `kheopskit$`. A wallet's `connect` or `disconnect`, and `useConnect`'s
171
+ `disconnect`, act on it while anything holds it, also after the provider that
172
+ handed them out unmounted or was hidden. Once the last holder leaves, the
173
+ pipeline stops; an action called then rejects `ABORTED` and starts no pipeline.
174
+ An `<Activity>` hide of the only provider stops the pipeline and a reveal
175
+ restarts it. An action called in between, from a layout effect, a mount effect
176
+ or a click during the reveal, waits for the restarted pipeline's first emission
177
+ and then acts on the live wallet. When the live state lists no wallet with the
178
+ action's id, the action rejects with a plain `Error`, not a `KheopskitError`.
179
+ An action pending when the pipeline stops
180
+ with an error or completes rejects `ABORTED` with that reason. Account actions
181
+ such as `signRaw` bind to the extension, not to the pipeline, and work either
182
+ way. Only `useConnect`'s `connect` is scoped to its component, as described
183
+ above.
184
+
185
+ The hooks from `createKheopskit` are typed to its platforms. Standalone, pass the
186
+ platform tuple: `useWallet<typeof platforms>(walletId)`. `useConnect` is the
187
+ same for every platform.
188
+
189
+ ## One instance for React and RxJS code
190
+
191
+ `createKheopskit` also returns `kheopskit$`, the state stream its provider
192
+ subscribes to, and `store`, the store that stream persists into. Code outside
193
+ React, such as an RxJS pipeline or a sign-in flow, subscribes to the same
194
+ `kheopskit$`. While the provider is mounted, both share one pipeline: one wallet
195
+ discovery, one reconnect attempt and one extension prompt.
196
+
197
+ ```ts
198
+ // kheopskit.ts
199
+ export const { KheopskitProvider, useWallets, useWallet, kheopskit$, store } =
200
+ createKheopskit({ platforms: [polkadot()] });
201
+ ```
202
+
203
+ ```ts
204
+ // session.ts
205
+ import { distinctUntilChanged, map } from "rxjs";
206
+ import { kheopskit$ } from "./kheopskit";
207
+
208
+ export const addresses$ = kheopskit$.pipe(
209
+ map(({ accounts }) =>
210
+ accounts.filter((account) => !account.isPlaceholder).map((account) => account.address),
211
+ ),
212
+ distinctUntilChanged((a, b) => a.join() === b.join()),
213
+ );
214
+ ```
215
+
216
+ `kheopskit$` starts with its first subscriber and stops when the last one
217
+ leaves. The mounted provider counts as a subscriber. The provider never runs it
218
+ on the server. It renders each server request from that request's
219
+ `ssrCookies`. A module-level subscription in code the server evaluates does run
220
+ it there. It emits one state without wallets, persists nothing, leaves no timer
221
+ behind and does not change what the provider renders for a request.
222
+
223
+ Without `options.store`, `createKheopskit` creates the store on first use in
224
+ the browser. The provider claims it when it first renders there. The store is
225
+ backed by cookies when the provider renders with `ssrCookies`, and by
226
+ localStorage otherwise. Effects of components inside the provider run after
227
+ that render, so they use the provider's store. In an SSR app, code that reads
228
+ `store` or subscribes to `kheopskit$` before the provider renders, such as
229
+ module-scope code or a parent's effect that runs before the provider mounts,
230
+ would get the localStorage store. Pass the store in that case:
231
+
232
+ ```ts
233
+ import { createKheopskitStore } from "@kheopskit/core";
234
+
235
+ export const { KheopskitProvider, kheopskit$, store } = createKheopskit(config, {
236
+ store: createKheopskitStore({ ssrCookies: "" }),
237
+ });
238
+ ```
239
+
240
+ `createKheopskit` never calls `store.destroy()`, so the store survives a
241
+ provider remount. Call it yourself when the app no longer needs kheopskit.
242
+
80
243
  ## SSR
81
244
 
82
245
  Pass the request cookie header as `ssrCookies` to hydrate wallet state on the
@@ -93,10 +256,75 @@ const ssrCookies = cookieStore
93
256
  <KheopskitProvider ssrCookies={ssrCookies}>{children}</KheopskitProvider>;
94
257
  ```
95
258
 
96
- > While `state.isHydrating` is `true`, wallets/accounts are cached placeholders
97
- > carrying only the base fields — SDK fields (`signer`, `client`,
98
- > provider/extension handles) are absent until hydration completes. Guard access
99
- > behind `!isHydrating`.
259
+ > While `state.isHydrating` is `true`, wallets and accounts restored from the
260
+ > cache are placeholders with `isPlaceholder: true` and no SDK handle
261
+ > (`txCreator`, `signer`, `client`, `provider`, `extension`). Narrow on
262
+ > `isPlaceholder` before you read one, as in `if (account.isPlaceholder) return;`.
263
+ > `isHydrating` can still disable a button.
264
+
265
+ ## Testing
266
+
267
+ Render the real provider with a store backed by memory and a fake extension from
268
+ [`@kheopskit/core/testing`](../core/README.md#testing):
269
+
270
+ ```tsx
271
+ import { createKheopskitStore, type KheopskitStore } from "@kheopskit/core";
272
+ import { polkadot } from "@kheopskit/core/polkadot";
273
+ import {
274
+ createFakePolkadotExtension,
275
+ createMemoryStorage,
276
+ type FakePolkadotExtension,
277
+ } from "@kheopskit/core/testing";
278
+ import { createKheopskit, useConnect } from "@kheopskit/react";
279
+ import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
280
+ import { afterEach, beforeEach, expect, it, vi } from "vitest";
281
+
282
+ let talisman: FakePolkadotExtension;
283
+ let store: KheopskitStore;
284
+
285
+ beforeEach(() => {
286
+ vi.useFakeTimers();
287
+ talisman = createFakePolkadotExtension({ name: "talisman" });
288
+ store = createKheopskitStore({ storage: createMemoryStorage() });
289
+ });
290
+
291
+ afterEach(() => {
292
+ cleanup();
293
+ talisman.uninstall();
294
+ store.destroy();
295
+ vi.useRealTimers();
296
+ });
297
+
298
+ const ConnectButton = () => {
299
+ const { connect, status } = useConnect("polkadot:talisman");
300
+ return (
301
+ <button type="button" onClick={connect}>
302
+ {status}
303
+ </button>
304
+ );
305
+ };
306
+
307
+ it("connects the wallet the user picks", async () => {
308
+ const { KheopskitProvider } = createKheopskit({ platforms: [polkadot()] }, { store });
309
+ render(
310
+ <KheopskitProvider>
311
+ <ConnectButton />
312
+ </KheopskitProvider>,
313
+ );
314
+ await act(() => vi.advanceTimersByTimeAsync(1000));
315
+
316
+ fireEvent.click(screen.getByRole("button"));
317
+ await act(() => vi.advanceTimersByTimeAsync(1000));
318
+
319
+ expect(screen.getByRole("button").textContent).toBe("connected");
320
+ expect(talisman.enableCalls()).toHaveLength(1);
321
+ });
322
+ ```
323
+
324
+ The `afterEach` is the whole teardown. `cleanup()` unmounts the provider, which
325
+ stops kheopskit's timers, `uninstall()` removes the fake, and `destroy()` ends
326
+ the store's storage subscription. The standalone `KheopskitProvider` takes the
327
+ same store as a `store` prop.
100
328
 
101
329
  ## License
102
330
 
package/dist/index.d.mts CHANGED
@@ -1,34 +1,56 @@
1
- import { KheopskitConfig, KheopskitPlatform, KheopskitState } from "@kheopskit/core";
1
+ import { KheopskitConfig, KheopskitError, KheopskitPlatform, KheopskitState, KheopskitStore, WalletAccountId, WalletId, WalletStatus } from "@kheopskit/core";
2
2
  import { FC, PropsWithChildren } from "react";
3
+ import { Observable } from "rxjs";
3
4
  //#region src/createKheopskit.d.ts
4
5
  export type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
5
6
  /** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */
6
7
  platforms: P;
7
8
  };
9
+ export type CreateKheopskitOptions = {
10
+ /**
11
+ * Store the shared instance reads the cache from and persists into. Pass one
12
+ * when code outside React reads `store` or subscribes `kheopskit$` before
13
+ * the provider renders in an SSR app, and in tests.
14
+ */
15
+ store?: KheopskitStore;
16
+ };
8
17
  /**
9
- * Binds a platform tuple once and returns a `KheopskitProvider` plus hooks
10
- * (`useWallets`, `useAccounts`) already typed to those platforms — so you don't
11
- * repeat `useWallets<typeof platforms>()` in every component.
18
+ * Binds a platform tuple once and returns one kheopskit instance: a
19
+ * `KheopskitProvider`, hooks already typed to those platforms, and the
20
+ * `kheopskit$` stream and `store` the provider uses, for code outside React.
21
+ * Subscribing `kheopskit$` while the provider is mounted runs no second
22
+ * pipeline, and holds the one pipeline for every hook action of the instance.
23
+ *
24
+ * Without `options.store`, the store is created on first use in the browser:
25
+ * backed by cookies when the provider renders first with `ssrCookies`, by
26
+ * localStorage otherwise. The caller owns `store.destroy()`.
12
27
  *
13
28
  * @example
14
29
  * ```tsx
15
30
  * // kheopskit.ts
16
- * export const { KheopskitProvider, useWallets, useAccounts } = createKheopskit({
17
- * platforms: [polkadot(), ethereum(), solana()],
18
- * });
31
+ * export const { KheopskitProvider, useWallets, useWallet, kheopskit$ } =
32
+ * createKheopskit({ platforms: [polkadot(), ethereum(), solana()] });
19
33
  *
20
34
  * // anywhere
21
35
  * const { accounts } = useWallets(); // accounts are platform-precise, no generic
22
36
  * ```
23
37
  */
24
- export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
38
+ export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>, options?: CreateKheopskitOptions) => {
25
39
  KheopskitProvider: FC<PropsWithChildren<{
26
40
  ssrCookies?: string;
27
41
  }>>;
42
+ /** The state stream the provider subscribes to, shared with code outside React. */
43
+ kheopskit$: Observable<KheopskitState<P>>;
44
+ /** The store `kheopskit$` reads the cache from and persists into. */
45
+ store: KheopskitStore;
28
46
  /** Current state, typed to the bound platform tuple. */
29
- useWallets: () => import("@kheopskit/core").KheopskitState<P>;
47
+ useWallets: () => KheopskitState<P>;
30
48
  /** Current accounts, typed to the bound platform tuple. */
31
49
  useAccounts: () => import("@kheopskit/core").AccountOf<P[number]>[];
50
+ /** The wallet with this id, typed to the bound platform tuple. */
51
+ useWallet: (walletId: WalletId) => import("@kheopskit/core").WalletOf<P[number]> | import("@kheopskit/core").WalletConnectWallet | undefined;
52
+ /** The account with this id, typed to the bound platform tuple. */
53
+ useAccount: (accountId: WalletAccountId) => import("@kheopskit/core").AccountOf<P[number]> | undefined;
32
54
  };
33
55
  //#endregion
34
56
  //#region src/KheopskitProvider.d.ts
@@ -61,9 +83,34 @@ export type KheopskitProviderProps = PropsWithChildren & {
61
83
  * ```
62
84
  */
63
85
  ssrCookies?: string;
86
+ /**
87
+ * Store the provider reads the cache from and persists into, on the client.
88
+ * Defaults to a store per mount, backed by cookies when `ssrCookies` is set
89
+ * and by localStorage otherwise. The server snapshot always comes from
90
+ * `ssrCookies`.
91
+ *
92
+ * @example
93
+ * ```tsx
94
+ * // tests
95
+ * const store = createKheopskitStore({ storage: createMemoryStorage() });
96
+ * render(<KheopskitProvider config={config} store={store}>…</KheopskitProvider>);
97
+ * ```
98
+ */
99
+ store?: KheopskitStore;
64
100
  };
65
101
  export declare const KheopskitProvider: FC<KheopskitProviderProps>;
66
102
  //#endregion
103
+ //#region src/useAccount.d.ts
104
+ /**
105
+ * Returns the account with this id, or `undefined` while the state lists none.
106
+ * Re-renders only when that account changes: its name, its placeholder flag,
107
+ * a platform field such as an Ethereum `chainId`, or an SDK handle.
108
+ *
109
+ * Pass the platform tuple as a type argument for the SDK-precise account type,
110
+ * or use the pre-typed hook from {@link createKheopskit}.
111
+ */
112
+ export declare const useAccount: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>(accountId: WalletAccountId) => KheopskitState<P>["accounts"][number] | undefined;
113
+ //#endregion
67
114
  //#region src/useAccounts.d.ts
68
115
  /**
69
116
  * Convenience hook returning just the accounts from kheopskit state. Pass the
@@ -73,9 +120,69 @@ export declare const KheopskitProvider: FC<KheopskitProviderProps>;
73
120
  */
74
121
  export declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
75
122
  //#endregion
123
+ //#region src/useConnect.d.ts
124
+ export type UseConnectResult = {
125
+ /**
126
+ * Connects the wallet through the pipeline that runs when it is called, and
127
+ * waits for that pipeline's first emission when an `<Activity>` reveal has
128
+ * just restarted it. Resolves once the state shows the wallet connected.
129
+ * Unmounting the component or changing `walletId` aborts a pending connect,
130
+ * which then resolves without connecting. A connect that starts after
131
+ * unmount, or through a `connect` captured before `walletId` changed,
132
+ * rejects `ABORTED` without asking the wallet. Rejects with the wallet's
133
+ * `KheopskitError` otherwise, such as `USER_REJECTED`, and with an `Error`
134
+ * when the state lists no wallet with this id.
135
+ */
136
+ connect: () => Promise<void>;
137
+ /**
138
+ * Disconnects the wallet through the instance's pipeline, like a `useWallet`
139
+ * wallet's `disconnect`, also after this component unmounted. Resolves once
140
+ * the state shows it disconnected.
141
+ */
142
+ disconnect: () => Promise<void>;
143
+ /** `undefined` while the state lists no wallet with this id. */
144
+ status: WalletStatus | undefined;
145
+ /** The last connect the platform refused or failed. */
146
+ error: KheopskitError | undefined;
147
+ };
148
+ /**
149
+ * Connect and disconnect actions for one wallet, with its status and error.
150
+ * `connect` and `disconnect` keep their identity while `walletId` is the same,
151
+ * so `useEffect(() => { connect() }, [connect])` connects on mount and after
152
+ * every `<Activity>` reveal.
153
+ *
154
+ * @example
155
+ * ```tsx
156
+ * const ConnectButton = ({ walletId }: { walletId: WalletId }) => {
157
+ * const { connect, disconnect, status } = useConnect(walletId);
158
+ * if (status === "connected") return <button onClick={disconnect}>Disconnect</button>;
159
+ * return <button disabled={status === "connecting"} onClick={connect}>Connect</button>;
160
+ * };
161
+ * ```
162
+ */
163
+ export declare const useConnect: (walletId: WalletId) => UseConnectResult;
164
+ //#endregion
165
+ //#region src/useWallet.d.ts
166
+ /**
167
+ * Returns the wallet with this id, or `undefined` while the state lists none.
168
+ * Re-renders only when that wallet changes: its status, connection, error,
169
+ * name or any other field. Its `connect` and `disconnect` act on the instance's
170
+ * pipeline when they are called, also right after an `<Activity>` reveal
171
+ * restarts it and while another provider or a `kheopskit$` subscriber holds
172
+ * it, and reject `ABORTED` while nothing holds it.
173
+ *
174
+ * Pass the platform tuple as a type argument for the SDK-precise wallet type,
175
+ * or use the pre-typed hook from {@link createKheopskit}.
176
+ */
177
+ export declare const useWallet: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>(walletId: WalletId) => KheopskitState<P>["wallets"][number] | undefined;
178
+ //#endregion
76
179
  //#region src/useWallets.d.ts
77
180
  /**
78
- * Returns the current kheopskit state (wallets, accounts, config, isHydrating).
181
+ * Returns the current kheopskit state (wallets, accounts, config, isHydrating)
182
+ * and re-renders on every change. To follow one wallet or account, prefer
183
+ * {@link useWallet} or {@link useAccount}. Each wallet's `connect` and
184
+ * `disconnect` act on the instance's pipeline when they are called, while
185
+ * anything holds it.
79
186
  *
80
187
  * Pass the platform tuple as a type argument to recover SDK-precise account and
81
188
  * wallet types — `useWallets<typeof platforms>()`. React contexts can't be
package/dist/index.d.ts CHANGED
@@ -1,34 +1,56 @@
1
- import { KheopskitConfig, KheopskitPlatform, KheopskitState } from "@kheopskit/core";
1
+ import { KheopskitConfig, KheopskitError, KheopskitPlatform, KheopskitState, KheopskitStore, WalletAccountId, WalletId, WalletStatus } from "@kheopskit/core";
2
2
  import { FC, PropsWithChildren } from "react";
3
+ import { Observable } from "rxjs";
3
4
  //#region src/createKheopskit.d.ts
4
5
  export type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
5
6
  /** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */
6
7
  platforms: P;
7
8
  };
9
+ export type CreateKheopskitOptions = {
10
+ /**
11
+ * Store the shared instance reads the cache from and persists into. Pass one
12
+ * when code outside React reads `store` or subscribes `kheopskit$` before
13
+ * the provider renders in an SSR app, and in tests.
14
+ */
15
+ store?: KheopskitStore;
16
+ };
8
17
  /**
9
- * Binds a platform tuple once and returns a `KheopskitProvider` plus hooks
10
- * (`useWallets`, `useAccounts`) already typed to those platforms — so you don't
11
- * repeat `useWallets<typeof platforms>()` in every component.
18
+ * Binds a platform tuple once and returns one kheopskit instance: a
19
+ * `KheopskitProvider`, hooks already typed to those platforms, and the
20
+ * `kheopskit$` stream and `store` the provider uses, for code outside React.
21
+ * Subscribing `kheopskit$` while the provider is mounted runs no second
22
+ * pipeline, and holds the one pipeline for every hook action of the instance.
23
+ *
24
+ * Without `options.store`, the store is created on first use in the browser:
25
+ * backed by cookies when the provider renders first with `ssrCookies`, by
26
+ * localStorage otherwise. The caller owns `store.destroy()`.
12
27
  *
13
28
  * @example
14
29
  * ```tsx
15
30
  * // kheopskit.ts
16
- * export const { KheopskitProvider, useWallets, useAccounts } = createKheopskit({
17
- * platforms: [polkadot(), ethereum(), solana()],
18
- * });
31
+ * export const { KheopskitProvider, useWallets, useWallet, kheopskit$ } =
32
+ * createKheopskit({ platforms: [polkadot(), ethereum(), solana()] });
19
33
  *
20
34
  * // anywhere
21
35
  * const { accounts } = useWallets(); // accounts are platform-precise, no generic
22
36
  * ```
23
37
  */
24
- export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
38
+ export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>, options?: CreateKheopskitOptions) => {
25
39
  KheopskitProvider: FC<PropsWithChildren<{
26
40
  ssrCookies?: string;
27
41
  }>>;
42
+ /** The state stream the provider subscribes to, shared with code outside React. */
43
+ kheopskit$: Observable<KheopskitState<P>>;
44
+ /** The store `kheopskit$` reads the cache from and persists into. */
45
+ store: KheopskitStore;
28
46
  /** Current state, typed to the bound platform tuple. */
29
- useWallets: () => import("@kheopskit/core").KheopskitState<P>;
47
+ useWallets: () => KheopskitState<P>;
30
48
  /** Current accounts, typed to the bound platform tuple. */
31
49
  useAccounts: () => import("@kheopskit/core").AccountOf<P[number]>[];
50
+ /** The wallet with this id, typed to the bound platform tuple. */
51
+ useWallet: (walletId: WalletId) => import("@kheopskit/core").WalletOf<P[number]> | import("@kheopskit/core").WalletConnectWallet | undefined;
52
+ /** The account with this id, typed to the bound platform tuple. */
53
+ useAccount: (accountId: WalletAccountId) => import("@kheopskit/core").AccountOf<P[number]> | undefined;
32
54
  };
33
55
  //#endregion
34
56
  //#region src/KheopskitProvider.d.ts
@@ -61,9 +83,34 @@ export type KheopskitProviderProps = PropsWithChildren & {
61
83
  * ```
62
84
  */
63
85
  ssrCookies?: string;
86
+ /**
87
+ * Store the provider reads the cache from and persists into, on the client.
88
+ * Defaults to a store per mount, backed by cookies when `ssrCookies` is set
89
+ * and by localStorage otherwise. The server snapshot always comes from
90
+ * `ssrCookies`.
91
+ *
92
+ * @example
93
+ * ```tsx
94
+ * // tests
95
+ * const store = createKheopskitStore({ storage: createMemoryStorage() });
96
+ * render(<KheopskitProvider config={config} store={store}>…</KheopskitProvider>);
97
+ * ```
98
+ */
99
+ store?: KheopskitStore;
64
100
  };
65
101
  export declare const KheopskitProvider: FC<KheopskitProviderProps>;
66
102
  //#endregion
103
+ //#region src/useAccount.d.ts
104
+ /**
105
+ * Returns the account with this id, or `undefined` while the state lists none.
106
+ * Re-renders only when that account changes: its name, its placeholder flag,
107
+ * a platform field such as an Ethereum `chainId`, or an SDK handle.
108
+ *
109
+ * Pass the platform tuple as a type argument for the SDK-precise account type,
110
+ * or use the pre-typed hook from {@link createKheopskit}.
111
+ */
112
+ export declare const useAccount: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>(accountId: WalletAccountId) => KheopskitState<P>["accounts"][number] | undefined;
113
+ //#endregion
67
114
  //#region src/useAccounts.d.ts
68
115
  /**
69
116
  * Convenience hook returning just the accounts from kheopskit state. Pass the
@@ -73,9 +120,69 @@ export declare const KheopskitProvider: FC<KheopskitProviderProps>;
73
120
  */
74
121
  export declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
75
122
  //#endregion
123
+ //#region src/useConnect.d.ts
124
+ export type UseConnectResult = {
125
+ /**
126
+ * Connects the wallet through the pipeline that runs when it is called, and
127
+ * waits for that pipeline's first emission when an `<Activity>` reveal has
128
+ * just restarted it. Resolves once the state shows the wallet connected.
129
+ * Unmounting the component or changing `walletId` aborts a pending connect,
130
+ * which then resolves without connecting. A connect that starts after
131
+ * unmount, or through a `connect` captured before `walletId` changed,
132
+ * rejects `ABORTED` without asking the wallet. Rejects with the wallet's
133
+ * `KheopskitError` otherwise, such as `USER_REJECTED`, and with an `Error`
134
+ * when the state lists no wallet with this id.
135
+ */
136
+ connect: () => Promise<void>;
137
+ /**
138
+ * Disconnects the wallet through the instance's pipeline, like a `useWallet`
139
+ * wallet's `disconnect`, also after this component unmounted. Resolves once
140
+ * the state shows it disconnected.
141
+ */
142
+ disconnect: () => Promise<void>;
143
+ /** `undefined` while the state lists no wallet with this id. */
144
+ status: WalletStatus | undefined;
145
+ /** The last connect the platform refused or failed. */
146
+ error: KheopskitError | undefined;
147
+ };
148
+ /**
149
+ * Connect and disconnect actions for one wallet, with its status and error.
150
+ * `connect` and `disconnect` keep their identity while `walletId` is the same,
151
+ * so `useEffect(() => { connect() }, [connect])` connects on mount and after
152
+ * every `<Activity>` reveal.
153
+ *
154
+ * @example
155
+ * ```tsx
156
+ * const ConnectButton = ({ walletId }: { walletId: WalletId }) => {
157
+ * const { connect, disconnect, status } = useConnect(walletId);
158
+ * if (status === "connected") return <button onClick={disconnect}>Disconnect</button>;
159
+ * return <button disabled={status === "connecting"} onClick={connect}>Connect</button>;
160
+ * };
161
+ * ```
162
+ */
163
+ export declare const useConnect: (walletId: WalletId) => UseConnectResult;
164
+ //#endregion
165
+ //#region src/useWallet.d.ts
166
+ /**
167
+ * Returns the wallet with this id, or `undefined` while the state lists none.
168
+ * Re-renders only when that wallet changes: its status, connection, error,
169
+ * name or any other field. Its `connect` and `disconnect` act on the instance's
170
+ * pipeline when they are called, also right after an `<Activity>` reveal
171
+ * restarts it and while another provider or a `kheopskit$` subscriber holds
172
+ * it, and reject `ABORTED` while nothing holds it.
173
+ *
174
+ * Pass the platform tuple as a type argument for the SDK-precise wallet type,
175
+ * or use the pre-typed hook from {@link createKheopskit}.
176
+ */
177
+ export declare const useWallet: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>(walletId: WalletId) => KheopskitState<P>["wallets"][number] | undefined;
178
+ //#endregion
76
179
  //#region src/useWallets.d.ts
77
180
  /**
78
- * Returns the current kheopskit state (wallets, accounts, config, isHydrating).
181
+ * Returns the current kheopskit state (wallets, accounts, config, isHydrating)
182
+ * and re-renders on every change. To follow one wallet or account, prefer
183
+ * {@link useWallet} or {@link useAccount}. Each wallet's `connect` and
184
+ * `disconnect` act on the instance's pipeline when they are called, while
185
+ * anything holds it.
79
186
  *
80
187
  * Pass the platform tuple as a type argument to recover SDK-precise account and
81
188
  * wallet types — `useWallets<typeof platforms>()`. React contexts can't be