@kheopskit/react 5.1.2 → 5.2.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/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { KheopskitConfig, KheopskitPlatform, KheopskitState } from "@kheopskit/core";
2
2
  import { FC, PropsWithChildren } from "react";
3
3
  //#region src/createKheopskit.d.ts
4
- type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
4
+ export type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
5
5
  /** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */
6
6
  platforms: P;
7
7
  };
@@ -21,7 +21,7 @@ type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partia
21
21
  * const { accounts } = useWallets(); // accounts are platform-precise, no generic
22
22
  * ```
23
23
  */
24
- declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
24
+ export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
25
25
  KheopskitProvider: FC<PropsWithChildren<{
26
26
  ssrCookies?: string;
27
27
  }>>;
@@ -32,7 +32,7 @@ declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...
32
32
  };
33
33
  //#endregion
34
34
  //#region src/KheopskitProvider.d.ts
35
- type KheopskitProviderProps = PropsWithChildren & {
35
+ export type KheopskitProviderProps = PropsWithChildren & {
36
36
  /**
37
37
  * Kheopskit configuration.
38
38
  *
@@ -62,7 +62,7 @@ type KheopskitProviderProps = PropsWithChildren & {
62
62
  */
63
63
  ssrCookies?: string;
64
64
  };
65
- declare const KheopskitProvider: FC<KheopskitProviderProps>;
65
+ export declare const KheopskitProvider: FC<KheopskitProviderProps>;
66
66
  //#endregion
67
67
  //#region src/useAccounts.d.ts
68
68
  /**
@@ -71,7 +71,7 @@ declare const KheopskitProvider: FC<KheopskitProviderProps>;
71
71
  * `useAccounts<typeof platforms>()` — or use the pre-typed hook from
72
72
  * {@link createKheopskit}.
73
73
  */
74
- declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
74
+ export declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
75
75
  //#endregion
76
76
  //#region src/useWallets.d.ts
77
77
  /**
@@ -82,7 +82,6 @@ declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly Kh
82
82
  * generic, so without the argument the state is typed with the base
83
83
  * (SDK-free) wallet/account shapes.
84
84
  */
85
- declare const useWallets: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => KheopskitState<P>;
85
+ export declare const useWallets: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => KheopskitState<P>;
86
86
  //#endregion
87
- export { CreateKheopskitConfig, KheopskitProvider, KheopskitProviderProps, createKheopskit, useAccounts, useWallets };
88
87
  //# sourceMappingURL=index.d.mts.map
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { KheopskitConfig, KheopskitPlatform, KheopskitState } from "@kheopskit/core";
2
2
  import { FC, PropsWithChildren } from "react";
3
3
  //#region src/createKheopskit.d.ts
4
- type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
4
+ export type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partial<KheopskitConfig<P>>, "platforms"> & {
5
5
  /** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */
6
6
  platforms: P;
7
7
  };
@@ -21,7 +21,7 @@ type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> = Omit<Partia
21
21
  * const { accounts } = useWallets(); // accounts are platform-precise, no generic
22
22
  * ```
23
23
  */
24
- declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
24
+ export declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]]>(config: CreateKheopskitConfig<P>) => {
25
25
  KheopskitProvider: FC<PropsWithChildren<{
26
26
  ssrCookies?: string;
27
27
  }>>;
@@ -32,7 +32,7 @@ declare const createKheopskit: <const P extends readonly [KheopskitPlatform, ...
32
32
  };
33
33
  //#endregion
34
34
  //#region src/KheopskitProvider.d.ts
35
- type KheopskitProviderProps = PropsWithChildren & {
35
+ export type KheopskitProviderProps = PropsWithChildren & {
36
36
  /**
37
37
  * Kheopskit configuration.
38
38
  *
@@ -62,7 +62,7 @@ type KheopskitProviderProps = PropsWithChildren & {
62
62
  */
63
63
  ssrCookies?: string;
64
64
  };
65
- declare const KheopskitProvider: FC<KheopskitProviderProps>;
65
+ export declare const KheopskitProvider: FC<KheopskitProviderProps>;
66
66
  //#endregion
67
67
  //#region src/useAccounts.d.ts
68
68
  /**
@@ -71,7 +71,7 @@ declare const KheopskitProvider: FC<KheopskitProviderProps>;
71
71
  * `useAccounts<typeof platforms>()` — or use the pre-typed hook from
72
72
  * {@link createKheopskit}.
73
73
  */
74
- declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
74
+ export declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => import("@kheopskit/core").AccountOf<P[number]>[];
75
75
  //#endregion
76
76
  //#region src/useWallets.d.ts
77
77
  /**
@@ -82,7 +82,6 @@ declare const useAccounts: <P extends readonly KheopskitPlatform[] = readonly Kh
82
82
  * generic, so without the argument the state is typed with the base
83
83
  * (SDK-free) wallet/account shapes.
84
84
  */
85
- declare const useWallets: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => KheopskitState<P>;
85
+ export declare const useWallets: <P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]>() => KheopskitState<P>;
86
86
  //#endregion
87
- export { CreateKheopskitConfig, KheopskitProvider, KheopskitProviderProps, createKheopskit, useAccounts, useWallets };
88
87
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/createExternalStore.ts","../src/KheopskitProvider.tsx","../src/useWallets.ts","../src/createKheopskit.tsx","../src/useAccounts.ts"],"sourcesContent":["import type { KheopskitState } from \"@kheopskit/core\";\nimport { createContext } from \"react\";\n\nexport const KheopskitContext = createContext<{\n\tstate: KheopskitState;\n} | null>(null);\n","import type { Observable, Subscription } from \"rxjs\";\n\nexport const createExternalStore = <T>(\n\tobservable$: Observable<T>,\n\tinitialValue: T,\n\tserverValue?: T,\n) => {\n\t// Use null as sentinel to indicate we haven't received first emission yet\n\tlet latestValue: T | null = null;\n\tlet subscription: Subscription | null = null;\n\tlet subscriberCount = 0;\n\tconst listeners = new Set<(value: T) => void>();\n\n\tconst ensureSubscription = () => {\n\t\tif (!subscription || subscription.closed) {\n\t\t\tsubscription = observable$.subscribe((value) => {\n\t\t\t\tlatestValue = value;\n\t\t\t\tfor (const listener of listeners) {\n\t\t\t\t\tlistener(value);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t};\n\n\t// The subscription is created lazily by subscribe() (which React calls from a\n\t// committed passive effect), never during render. React may render a tree and\n\t// throw it away without committing — StrictMode double-invokes the initial\n\t// render, and concurrent/Suspense can discard renders — so subscribing eagerly\n\t// here (createExternalStore runs inside the Provider's render-phase useMemo) would leak\n\t// the discarded store's observable subscription. Until the first emission,\n\t// getSnapshot falls back to initialValue.\n\tconst getSnapshot = () => latestValue ?? initialValue;\n\n\t/**\n\t * Returns the server-side snapshot for SSR hydration.\n\t * This prevents hydration mismatches by providing a consistent\n\t * value during server rendering. Must return the same value as\n\t * what the server rendered.\n\t */\n\tconst getServerSnapshot = () => serverValue ?? initialValue;\n\n\tconst subscribe = (callback: (value: T) => void) => {\n\t\tsubscriberCount++;\n\t\t// Subscribe to the observable BEFORE registering this listener, so a\n\t\t// synchronous replay (BehaviorSubject / shareReplay) updates latestValue\n\t\t// without double-invoking this callback — the current value is emitted once,\n\t\t// explicitly, just below.\n\t\tensureSubscription();\n\t\tlisteners.add(callback);\n\n\t\t// Immediately emit current value (BehaviorSubject semantics)\n\t\tcallback(getSnapshot());\n\n\t\treturn () => {\n\t\t\tsubscriberCount--;\n\t\t\tlisteners.delete(callback);\n\t\t\t// Don't close the observable subscription on unsubscribe\n\t\t\t// Let destroy() handle that when the store is truly being disposed\n\t\t};\n\t};\n\n\tconst destroy = () => {\n\t\t// Only unsubscribe if no one is listening\n\t\t// React StrictMode may call destroy and then immediately resubscribe\n\t\tif (subscriberCount === 0 && subscription) {\n\t\t\tsubscription.unsubscribe();\n\t\t\tsubscription = null;\n\t\t}\n\t};\n\n\treturn {\n\t\tgetSnapshot,\n\t\tgetServerSnapshot,\n\t\tsubscribe,\n\t\tdestroy,\n\t};\n};\n","import {\n\tcreateKheopskitStore,\n\tgetHydratedSnapshot,\n\tgetKheopskit$,\n\ttype KheopskitConfig,\n\ttype KheopskitState,\n\tresolveConfig,\n} from \"@kheopskit/core\";\nimport {\n\ttype FC,\n\ttype PropsWithChildren,\n\tuseEffect,\n\tuseMemo,\n\tuseRef,\n\tuseSyncExternalStore,\n} from \"react\";\nimport { KheopskitContext } from \"./context\";\nimport { createExternalStore } from \"./createExternalStore\";\n\nexport type KheopskitProviderProps = PropsWithChildren & {\n\t/**\n\t * Kheopskit configuration.\n\t *\n\t * @remarks\n\t * Must be a **referentially stable** value — define it once (module scope, a\n\t * `useMemo`, or via {@link createKheopskit}) and pass the same reference. A\n\t * new object literal on every render (`config={{ platforms: [...] }}` inline)\n\t * recreates the underlying store and re-subscribes each render.\n\t */\n\tconfig?: Partial<KheopskitConfig>;\n\t/**\n\t * Cookie string for SSR hydration.\n\t * Pass the request cookie header (e.g., from Next.js headers or TanStack Start)\n\t * to hydrate wallet state on the server.\n\t *\n\t * @remarks\n\t * This value should be stable per render to avoid unnecessary store recreation.\n\t * Compute it once in your server component or layout and pass it down.\n\t *\n\t * @example\n\t * ```tsx\n\t * // Next.js App Router\n\t * const cookieStore = await cookies();\n\t * const ssrCookies = cookieStore.getAll().map(c => `${c.name}=${c.value}`).join('; ');\n\t * return <Providers ssrCookies={ssrCookies}>{children}</Providers>\n\t * ```\n\t */\n\tssrCookies?: string;\n};\n\nexport const KheopskitProvider: FC<KheopskitProviderProps> = ({\n\tchildren,\n\tconfig,\n\tssrCookies,\n}) => {\n\tconst resolvedConfig = useMemo(() => resolveConfig(config), [config]);\n\n\t// Dev-only: warn if the `config` prop reference changes between renders. A new\n\t// object each render recreates the store and re-subscribes the whole pipeline\n\t// (re-hydrating, dropping live connection state) on every render. `config` must\n\t// be referentially stable — define it at module scope, memoize it, or use\n\t// createKheopskit() (which passes a stable reference for you).\n\tconst lastConfigRef = useRef(config);\n\tconst hasMountedRef = useRef(false);\n\tuseEffect(() => {\n\t\tif (\n\t\t\thasMountedRef.current &&\n\t\t\tlastConfigRef.current !== config &&\n\t\t\t(typeof process === \"undefined\" || process.env?.NODE_ENV !== \"production\")\n\t\t) {\n\t\t\tconsole.warn(\n\t\t\t\t\"[kheopskit] KheopskitProvider received a new `config` reference; this \" +\n\t\t\t\t\t\"recreates the store and re-subscribes on every render. Pass a \" +\n\t\t\t\t\t\"referentially stable config (module scope, useMemo, or createKheopskit()).\",\n\t\t\t);\n\t\t}\n\t\thasMountedRef.current = true;\n\t\tlastConfigRef.current = config;\n\t}, [config]);\n\n\t// Create a single store for both reading cached state and powering the observable\n\tconst kheopskitStore = useMemo(\n\t\t() =>\n\t\t\tcreateKheopskitStore({\n\t\t\t\tssrCookies,\n\t\t\t\tstorageKey: resolvedConfig.storageKey,\n\t\t\t}),\n\t\t[ssrCookies, resolvedConfig.storageKey],\n\t);\n\n\t// Read cached state from the store for SSR hydration.\n\t// Icons stay OFF: this snapshot must match the server-rendered markup, and the\n\t// server never sees the localStorage icon cache (see GetHydratedSnapshotOptions).\n\tconst serverValue = useMemo<KheopskitState>(() => {\n\t\tif (ssrCookies === undefined) {\n\t\t\treturn {\n\t\t\t\twallets: [],\n\t\t\t\taccounts: [],\n\t\t\t\tconfig: resolvedConfig,\n\t\t\t\tisHydrating: true,\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\t...getHydratedSnapshot(kheopskitStore, resolvedConfig.platforms),\n\t\t\tconfig: resolvedConfig,\n\t\t\tisHydrating: true,\n\t\t};\n\t}, [ssrCookies, kheopskitStore, resolvedConfig]);\n\n\t// Client-only initial snapshot, read straight from the client cache so a hard\n\t// reload paints the cached wallet/account list on the very first frame instead\n\t// of flashing empty until the live observable produces its first emission —\n\t// which can be asynchronous (e.g. WalletConnect's AppKit is loaded via dynamic\n\t// import, so the underlying combineLatest can't emit synchronously).\n\t//\n\t// We can't derive this from `serverValue`: without SSR cookies that stays empty\n\t// (to keep the server/client hydration markup identical), so the SPA case would\n\t// otherwise render nothing. This snapshot is only ever read on the client via\n\t// getSnapshot, so reading the cache here is safe — and getCachedIcon returns \"\"\n\t// on the server, making the icon enrichment a no-op there.\n\tconst initialValue = useMemo<KheopskitState>(() => {\n\t\treturn {\n\t\t\t...getHydratedSnapshot(kheopskitStore, resolvedConfig.platforms, {\n\t\t\t\ticons: true,\n\t\t\t}),\n\t\t\tconfig: resolvedConfig,\n\t\t\tisHydrating: true,\n\t\t};\n\t}, [kheopskitStore, resolvedConfig]);\n\n\tconst store = useMemo(\n\t\t() =>\n\t\t\tcreateExternalStore(\n\t\t\t\tgetKheopskit$(config, { ssrCookies, store: kheopskitStore }),\n\t\t\t\tinitialValue,\n\t\t\t\tserverValue,\n\t\t\t),\n\t\t[config, ssrCookies, kheopskitStore, initialValue, serverValue],\n\t);\n\n\t// Cleanup store subscriptions when store changes or component unmounts\n\tuseEffect(() => {\n\t\treturn () => store.destroy();\n\t}, [store]);\n\n\tconst state = useSyncExternalStore(\n\t\tstore.subscribe,\n\t\tstore.getSnapshot,\n\t\tstore.getServerSnapshot,\n\t);\n\n\tconst value = useMemo(() => ({ state }), [state]);\n\n\treturn (\n\t\t<KheopskitContext.Provider value={value}>\n\t\t\t{children}\n\t\t</KheopskitContext.Provider>\n\t);\n};\n","import type { KheopskitPlatform, KheopskitState } from \"@kheopskit/core\";\nimport { useContext } from \"react\";\nimport { KheopskitContext } from \"./context\";\n\n/**\n * Returns the current kheopskit state (wallets, accounts, config, isHydrating).\n *\n * Pass the platform tuple as a type argument to recover SDK-precise account and\n * wallet types — `useWallets<typeof platforms>()`. React contexts can't be\n * generic, so without the argument the state is typed with the base\n * (SDK-free) wallet/account shapes.\n */\nexport const useWallets = <\n\tP extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>(): KheopskitState<P> => {\n\tconst ctx = useContext(KheopskitContext);\n\n\tif (!ctx)\n\t\tthrow new Error(\"useWallets can't be used without a KheopskitProvider\");\n\n\treturn ctx.state as unknown as KheopskitState<P>;\n};\n","import type { KheopskitConfig, KheopskitPlatform } from \"@kheopskit/core\";\nimport type { FC, PropsWithChildren } from \"react\";\nimport { KheopskitProvider } from \"./KheopskitProvider\";\nimport { useWallets } from \"./useWallets\";\n\nexport type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> =\n\tOmit<Partial<KheopskitConfig<P>>, \"platforms\"> & {\n\t\t/** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */\n\t\tplatforms: P;\n\t};\n\n/**\n * Binds a platform tuple once and returns a `KheopskitProvider` plus hooks\n * (`useWallets`, `useAccounts`) already typed to those platforms — so you don't\n * repeat `useWallets<typeof platforms>()` in every component.\n *\n * @example\n * ```tsx\n * // kheopskit.ts\n * export const { KheopskitProvider, useWallets, useAccounts } = createKheopskit({\n * platforms: [polkadot(), ethereum(), solana()],\n * });\n *\n * // anywhere\n * const { accounts } = useWallets(); // accounts are platform-precise, no generic\n * ```\n */\nexport const createKheopskit = <\n\tconst P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]],\n>(\n\tconfig: CreateKheopskitConfig<P>,\n) => {\n\tconst Provider: FC<PropsWithChildren<{ ssrCookies?: string }>> = ({\n\t\tchildren,\n\t\tssrCookies,\n\t}) => (\n\t\t<KheopskitProvider config={config} ssrCookies={ssrCookies}>\n\t\t\t{children}\n\t\t</KheopskitProvider>\n\t);\n\tProvider.displayName = \"KheopskitProvider\";\n\n\treturn {\n\t\tKheopskitProvider: Provider,\n\t\t/** Current state, typed to the bound platform tuple. */\n\t\tuseWallets: () => useWallets<P>(),\n\t\t/** Current accounts, typed to the bound platform tuple. */\n\t\tuseAccounts: () => useWallets<P>().accounts,\n\t};\n};\n","import type { KheopskitPlatform } from \"@kheopskit/core\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Convenience hook returning just the accounts from kheopskit state. Pass the\n * platform tuple as a type argument to recover SDK-precise account types —\n * `useAccounts<typeof platforms>()` — or use the pre-typed hook from\n * {@link createKheopskit}.\n */\nexport const useAccounts = <\n\tP extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>() => useWallets<P>().accounts;\n"],"mappings":";;;;;;AAGA,MAAa,oBAAA,GAAA,MAAA,cAAA,CAEH,IAAI;;;ACHd,MAAa,uBACZ,aACA,cACA,gBACI;CAEJ,IAAI,cAAwB;CAC5B,IAAI,eAAoC;CACxC,IAAI,kBAAkB;CACtB,MAAM,4BAAY,IAAI,IAAwB;CAE9C,MAAM,2BAA2B;EAChC,IAAI,CAAC,gBAAgB,aAAa,QACjC,eAAe,YAAY,WAAW,UAAU;GAC/C,cAAc;GACd,KAAK,MAAM,YAAY,WACtB,SAAS,KAAK;EAEhB,CAAC;CAEH;CASA,MAAM,oBAAoB,eAAe;;;;;;;CAQzC,MAAM,0BAA0B,eAAe;CAE/C,MAAM,aAAa,aAAiC;EACnD;EAKA,mBAAmB;EACnB,UAAU,IAAI,QAAQ;EAGtB,S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AAQ;GACR,aAAa;EACd;CACD,GAAG,CAAC,gBAAgB,cAAc,CAAC;CAEnC,MAAM,SAAA,GAAA,MAAA,QAAA,OAEJ,qBAAA,GAAA,gBAAA,cAAA,CACe,QAAQ;EAAE;EAAY,OAAO;CAAe,CAAC,GAC3D,cACA,WACD,GACD;EAAC;EAAQ;EAAY;EAAgB;EAAc;CAAW,CAC/D;CAGA,CAAA,GAAA,MAAA,UAAA,OAAgB;EACf,aAAa,MAAM,QAAQ;CAC5B,GAAG,CAAC,KAAK,CAAC;CAEV,MAAM,SAAA,GAAA,MAAA,qBAAA,CACL,MAAM,WACN,MAAM,aACN,MAAM,iBACP;CAEA,MAAM,SAAA,GAAA,MAAA,QAAA,QAAuB,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC;CAEhD,OACC,iBAAA,GAAA,kBAAA,IAAA,CAAC,iBAAiB,UAAlB;EAAkC;EAChC;CACyB,CAAA;AAE7B;;;;;;;;;;;AClJA,MAAa,mBAEa;CACzB,MAAM,OAAA,GAAA,MAAA,WAAA,CAAiB,gBAAgB;CAEvC,IAAI,CAAC,KACJ,MAAM,IAAI,MAAM,sDAAsD;CAEvE,OAAO,IAAI;AACZ;;;;;;;;;;;;;;;;;;;ACMA,MAAa,mBAGZ,WACI;CACJ,MAAM,YAA4D,EACjE,UACA,iBAEA,iBAAA,GAAA,kBAAA,IAAA,CAAC,mBAAD;EAA2B;EAAoB;EAC7C;CACiB,CAAA;CAEpB,SAAS,cAAc;CAEvB,OAAO;EACN,mBAAmB;;EAEnB,kBAAkB,WAAc;;EAEhC,mBAAmB,WAAc,CAAC,CAAC;CACpC;AACD;;;;;;;;;ACxCA,MAAa,oBAEN,WAAc,CAAC,CAAC"}
1
+ {"version":3,"file":"index.js","names":["createContext","useMemo","resolveConfig","useRef","createKheopskitStore","getHydratedSnapshot","getKheopskit$","useSyncExternalStore","useContext"],"sources":["../src/context.ts","../src/createExternalStore.ts","../src/KheopskitProvider.tsx","../src/useWallets.ts","../src/createKheopskit.tsx","../src/useAccounts.ts"],"sourcesContent":["import type { KheopskitState } from \"@kheopskit/core\";\nimport { createContext } from \"react\";\n\nexport const KheopskitContext = createContext<{\n\tstate: KheopskitState;\n} | null>(null);\n","import type { Observable, Subscription } from \"rxjs\";\n\nexport const createExternalStore = <T>(\n\tobservable$: Observable<T>,\n\tinitialValue: T,\n\tserverValue?: T,\n) => {\n\t// Use null as sentinel to indicate we haven't received first emission yet\n\tlet latestValue: T | null = null;\n\tlet subscription: Subscription | null = null;\n\tlet subscriberCount = 0;\n\tconst listeners = new Set<(value: T) => void>();\n\n\tconst ensureSubscription = () => {\n\t\tif (!subscription || subscription.closed) {\n\t\t\tsubscription = observable$.subscribe((value) => {\n\t\t\t\tlatestValue = value;\n\t\t\t\tfor (const listener of listeners) {\n\t\t\t\t\tlistener(value);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t};\n\n\t// The subscription is created lazily by subscribe() (which React calls from a\n\t// committed passive effect), never during render. React may render a tree and\n\t// throw it away without committing — StrictMode double-invokes the initial\n\t// render, and concurrent/Suspense can discard renders — so subscribing eagerly\n\t// here (createExternalStore runs inside the Provider's render-phase useMemo) would leak\n\t// the discarded store's observable subscription. Until the first emission,\n\t// getSnapshot falls back to initialValue.\n\tconst getSnapshot = () => latestValue ?? initialValue;\n\n\t/**\n\t * Returns the server-side snapshot for SSR hydration.\n\t * This prevents hydration mismatches by providing a consistent\n\t * value during server rendering. Must return the same value as\n\t * what the server rendered.\n\t */\n\tconst getServerSnapshot = () => serverValue ?? initialValue;\n\n\tconst subscribe = (callback: (value: T) => void) => {\n\t\tsubscriberCount++;\n\t\t// Subscribe to the observable BEFORE registering this listener, so a\n\t\t// synchronous replay (BehaviorSubject / shareReplay) updates latestValue\n\t\t// without double-invoking this callback — the current value is emitted once,\n\t\t// explicitly, just below.\n\t\tensureSubscription();\n\t\tlisteners.add(callback);\n\n\t\t// Immediately emit current value (BehaviorSubject semantics)\n\t\tcallback(getSnapshot());\n\n\t\treturn () => {\n\t\t\tsubscriberCount--;\n\t\t\tlisteners.delete(callback);\n\t\t\t// Don't close the observable subscription on unsubscribe\n\t\t\t// Let destroy() handle that when the store is truly being disposed\n\t\t};\n\t};\n\n\tconst destroy = () => {\n\t\t// Only unsubscribe if no one is listening\n\t\t// React StrictMode may call destroy and then immediately resubscribe\n\t\tif (subscriberCount === 0 && subscription) {\n\t\t\tsubscription.unsubscribe();\n\t\t\tsubscription = null;\n\t\t}\n\t};\n\n\treturn {\n\t\tgetSnapshot,\n\t\tgetServerSnapshot,\n\t\tsubscribe,\n\t\tdestroy,\n\t};\n};\n","import {\n\tcreateKheopskitStore,\n\tgetHydratedSnapshot,\n\tgetKheopskit$,\n\ttype KheopskitConfig,\n\ttype KheopskitState,\n\tresolveConfig,\n} from \"@kheopskit/core\";\nimport {\n\ttype FC,\n\ttype PropsWithChildren,\n\tuseEffect,\n\tuseMemo,\n\tuseRef,\n\tuseSyncExternalStore,\n} from \"react\";\nimport { KheopskitContext } from \"./context\";\nimport { createExternalStore } from \"./createExternalStore\";\n\nexport type KheopskitProviderProps = PropsWithChildren & {\n\t/**\n\t * Kheopskit configuration.\n\t *\n\t * @remarks\n\t * Must be a **referentially stable** value — define it once (module scope, a\n\t * `useMemo`, or via {@link createKheopskit}) and pass the same reference. A\n\t * new object literal on every render (`config={{ platforms: [...] }}` inline)\n\t * recreates the underlying store and re-subscribes each render.\n\t */\n\tconfig?: Partial<KheopskitConfig>;\n\t/**\n\t * Cookie string for SSR hydration.\n\t * Pass the request cookie header (e.g., from Next.js headers or TanStack Start)\n\t * to hydrate wallet state on the server.\n\t *\n\t * @remarks\n\t * This value should be stable per render to avoid unnecessary store recreation.\n\t * Compute it once in your server component or layout and pass it down.\n\t *\n\t * @example\n\t * ```tsx\n\t * // Next.js App Router\n\t * const cookieStore = await cookies();\n\t * const ssrCookies = cookieStore.getAll().map(c => `${c.name}=${c.value}`).join('; ');\n\t * return <Providers ssrCookies={ssrCookies}>{children}</Providers>\n\t * ```\n\t */\n\tssrCookies?: string;\n};\n\nexport const KheopskitProvider: FC<KheopskitProviderProps> = ({\n\tchildren,\n\tconfig,\n\tssrCookies,\n}) => {\n\tconst resolvedConfig = useMemo(() => resolveConfig(config), [config]);\n\n\t// Dev-only: warn if the `config` prop reference changes between renders. A new\n\t// object each render recreates the store and re-subscribes the whole pipeline\n\t// (re-hydrating, dropping live connection state) on every render. `config` must\n\t// be referentially stable — define it at module scope, memoize it, or use\n\t// createKheopskit() (which passes a stable reference for you).\n\tconst lastConfigRef = useRef(config);\n\tconst hasMountedRef = useRef(false);\n\tuseEffect(() => {\n\t\tif (\n\t\t\thasMountedRef.current &&\n\t\t\tlastConfigRef.current !== config &&\n\t\t\t(typeof process === \"undefined\" || process.env?.NODE_ENV !== \"production\")\n\t\t) {\n\t\t\tconsole.warn(\n\t\t\t\t\"[kheopskit] KheopskitProvider received a new `config` reference; this \" +\n\t\t\t\t\t\"recreates the store and re-subscribes on every render. Pass a \" +\n\t\t\t\t\t\"referentially stable config (module scope, useMemo, or createKheopskit()).\",\n\t\t\t);\n\t\t}\n\t\thasMountedRef.current = true;\n\t\tlastConfigRef.current = config;\n\t}, [config]);\n\n\t// Create a single store for both reading cached state and powering the observable\n\tconst kheopskitStore = useMemo(\n\t\t() =>\n\t\t\tcreateKheopskitStore({\n\t\t\t\tssrCookies,\n\t\t\t\tstorageKey: resolvedConfig.storageKey,\n\t\t\t}),\n\t\t[ssrCookies, resolvedConfig.storageKey],\n\t);\n\n\t// Read cached state from the store for SSR hydration.\n\t// Icons stay OFF: this snapshot must match the server-rendered markup, and the\n\t// server never sees the localStorage icon cache (see GetHydratedSnapshotOptions).\n\tconst serverValue = useMemo<KheopskitState>(() => {\n\t\tif (ssrCookies === undefined) {\n\t\t\treturn {\n\t\t\t\twallets: [],\n\t\t\t\taccounts: [],\n\t\t\t\tconfig: resolvedConfig,\n\t\t\t\tisHydrating: true,\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\t...getHydratedSnapshot(kheopskitStore, resolvedConfig.platforms),\n\t\t\tconfig: resolvedConfig,\n\t\t\tisHydrating: true,\n\t\t};\n\t}, [ssrCookies, kheopskitStore, resolvedConfig]);\n\n\t// Client-only initial snapshot, read straight from the client cache so a hard\n\t// reload paints the cached wallet/account list on the very first frame instead\n\t// of flashing empty until the live observable produces its first emission —\n\t// which can be asynchronous (e.g. WalletConnect's AppKit is loaded via dynamic\n\t// import, so the underlying combineLatest can't emit synchronously).\n\t//\n\t// We can't derive this from `serverValue`: without SSR cookies that stays empty\n\t// (to keep the server/client hydration markup identical), so the SPA case would\n\t// otherwise render nothing. This snapshot is only ever read on the client via\n\t// getSnapshot, so reading the cache here is safe — and getCachedIcon returns \"\"\n\t// on the server, making the icon enrichment a no-op there.\n\tconst initialValue = useMemo<KheopskitState>(() => {\n\t\treturn {\n\t\t\t...getHydratedSnapshot(kheopskitStore, resolvedConfig.platforms, {\n\t\t\t\ticons: true,\n\t\t\t}),\n\t\t\tconfig: resolvedConfig,\n\t\t\tisHydrating: true,\n\t\t};\n\t}, [kheopskitStore, resolvedConfig]);\n\n\tconst store = useMemo(\n\t\t() =>\n\t\t\tcreateExternalStore(\n\t\t\t\tgetKheopskit$(config, { ssrCookies, store: kheopskitStore }),\n\t\t\t\tinitialValue,\n\t\t\t\tserverValue,\n\t\t\t),\n\t\t[config, ssrCookies, kheopskitStore, initialValue, serverValue],\n\t);\n\n\t// Cleanup store subscriptions when store changes or component unmounts\n\tuseEffect(() => {\n\t\treturn () => store.destroy();\n\t}, [store]);\n\n\tconst state = useSyncExternalStore(\n\t\tstore.subscribe,\n\t\tstore.getSnapshot,\n\t\tstore.getServerSnapshot,\n\t);\n\n\tconst value = useMemo(() => ({ state }), [state]);\n\n\treturn (\n\t\t<KheopskitContext.Provider value={value}>\n\t\t\t{children}\n\t\t</KheopskitContext.Provider>\n\t);\n};\n","import type { KheopskitPlatform, KheopskitState } from \"@kheopskit/core\";\nimport { useContext } from \"react\";\nimport { KheopskitContext } from \"./context\";\n\n/**\n * Returns the current kheopskit state (wallets, accounts, config, isHydrating).\n *\n * Pass the platform tuple as a type argument to recover SDK-precise account and\n * wallet types — `useWallets<typeof platforms>()`. React contexts can't be\n * generic, so without the argument the state is typed with the base\n * (SDK-free) wallet/account shapes.\n */\nexport const useWallets = <\n\tP extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>(): KheopskitState<P> => {\n\tconst ctx = useContext(KheopskitContext);\n\n\tif (!ctx)\n\t\tthrow new Error(\"useWallets can't be used without a KheopskitProvider\");\n\n\treturn ctx.state as unknown as KheopskitState<P>;\n};\n","import type { KheopskitConfig, KheopskitPlatform } from \"@kheopskit/core\";\nimport type { FC, PropsWithChildren } from \"react\";\nimport { KheopskitProvider } from \"./KheopskitProvider\";\nimport { useWallets } from \"./useWallets\";\n\nexport type CreateKheopskitConfig<P extends readonly KheopskitPlatform[]> =\n\tOmit<Partial<KheopskitConfig<P>>, \"platforms\"> & {\n\t\t/** Platform plugins, e.g. `[polkadot(), solana()]`. Required. */\n\t\tplatforms: P;\n\t};\n\n/**\n * Binds a platform tuple once and returns a `KheopskitProvider` plus hooks\n * (`useWallets`, `useAccounts`) already typed to those platforms — so you don't\n * repeat `useWallets<typeof platforms>()` in every component.\n *\n * @example\n * ```tsx\n * // kheopskit.ts\n * export const { KheopskitProvider, useWallets, useAccounts } = createKheopskit({\n * platforms: [polkadot(), ethereum(), solana()],\n * });\n *\n * // anywhere\n * const { accounts } = useWallets(); // accounts are platform-precise, no generic\n * ```\n */\nexport const createKheopskit = <\n\tconst P extends readonly [KheopskitPlatform, ...KheopskitPlatform[]],\n>(\n\tconfig: CreateKheopskitConfig<P>,\n) => {\n\tconst Provider: FC<PropsWithChildren<{ ssrCookies?: string }>> = ({\n\t\tchildren,\n\t\tssrCookies,\n\t}) => (\n\t\t<KheopskitProvider config={config} ssrCookies={ssrCookies}>\n\t\t\t{children}\n\t\t</KheopskitProvider>\n\t);\n\tProvider.displayName = \"KheopskitProvider\";\n\n\treturn {\n\t\tKheopskitProvider: Provider,\n\t\t/** Current state, typed to the bound platform tuple. */\n\t\tuseWallets: () => useWallets<P>(),\n\t\t/** Current accounts, typed to the bound platform tuple. */\n\t\tuseAccounts: () => useWallets<P>().accounts,\n\t};\n};\n","import type { KheopskitPlatform } from \"@kheopskit/core\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Convenience hook returning just the accounts from kheopskit state. Pass the\n * platform tuple as a type argument to recover SDK-precise account types —\n * `useAccounts<typeof platforms>()` — or use the pre-typed hook from\n * {@link createKheopskit}.\n */\nexport const useAccounts = <\n\tP extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[],\n>() => useWallets<P>().accounts;\n"],"mappings":";;;;;;AAGA,MAAa,oBAAA,GAAmBA,MAAAA,cAAAA,CAEtB,IAAI;;;ACHd,MAAa,uBACZ,aACA,cACA,gBACI;CAEJ,IAAI,cAAwB;CAC5B,IAAI,eAAoC;CACxC,IAAI,kBAAkB;CACtB,MAAM,4BAAY,IAAI,IAAwB;CAE9C,MAAM,2BAA2B;EAChC,IAAI,CAAC,gBAAgB,aAAa,QACjC,eAAe,YAAY,WAAW,UAAU;GAC/C,cAAc;GACd,KAAK,MAAM,YAAY,WACtB,SAAS,KAAK;EAEhB,CAAC;CAEH;CASA,MAAM,oBAAoB,eAAe;;;;;;;CAQzC,MAAM,0BAA0B,eAAe;CAE/C,MAAM,aAAa,aAAiC;EACnD;EAKA,mBAAmB;EACnB,UAAU,IAAI,QAAQ;EAGtB,SAAS,YAAY,CAAC;EAEtB,aAAa;GACZ;GACA,UAAU,OAAO,QAAQ;EAG1B;CACD;CAEA,MAAM,gBAAgB;EAGrB,IAAI,oBAAoB,KAAK,cAAc;GAC1C,aAAa,YAAY;GACzB,eAAe;EAChB;CACD;CAEA,OAAO;EACN;EACA;EACA;EACA;CACD;AACD;;;AC1BA,MAAa,qBAAiD,EAC7D,UACA,QACA,iBACK;CACL,MAAM,kBAAA,GAAiBC,MAAAA,QAAAA,QA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,GAAA,MAAA,UAAA,OAAgB;EACf,aAAa,MAAM,QAAQ;CAC5B,GAAG,CAAC,KAAK,CAAC;CAEV,MAAM,SAAA,GAAQC,MAAAA,qBAAAA,CACb,MAAM,WACN,MAAM,aACN,MAAM,iBACP;CAEA,MAAM,SAAA,GAAQN,MAAAA,QAAAA,QAAe,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC;CAEhD,OACC,iBAAA,GAAA,kBAAA,IAAA,CAAC,iBAAiB,UAAlB;EAAkC;EAChC;CACyB,CAAA;AAE7B;;;;;;;;;;;AClJA,MAAa,mBAEa;CACzB,MAAM,OAAA,GAAMO,MAAAA,WAAAA,CAAW,gBAAgB;CAEvC,IAAI,CAAC,KACJ,MAAM,IAAI,MAAM,sDAAsD;CAEvE,OAAO,IAAI;AACZ;;;;;;;;;;;;;;;;;;;ACMA,MAAa,mBAGZ,WACI;CACJ,MAAM,YAA4D,EACjE,UACA,iBAEA,iBAAA,GAAA,kBAAA,IAAA,CAAC,mBAAD;EAA2B;EAAoB;EAC7C;CACiB,CAAA;CAEpB,SAAS,cAAc;CAEvB,OAAO;EACN,mBAAmB;;EAEnB,kBAAkB,WAAc;;EAEhC,mBAAmB,WAAc,CAAC,CAAC;CACpC;AACD;;;;;;;;;ACxCA,MAAa,oBAEN,WAAc,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kheopskit/react",
3
- "version": "5.1.2",
3
+ "version": "5.2.1",
4
4
  "description": "",
5
5
  "private": false,
6
6
  "sideEffects": false,
@@ -35,18 +35,18 @@
35
35
  },
36
36
  "license": "ISC",
37
37
  "peerDependencies": {
38
- "@kheopskit/core": "^5.1.2",
38
+ "@kheopskit/core": "^5.2.1",
39
39
  "react": ">=18.0.0",
40
40
  "react-dom": ">=18.0.0",
41
41
  "rxjs": ">=7.0.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@types/react": "^19.2.17",
45
- "@types/react-dom": "^19.2.3",
46
- "react": "^19.2.8",
47
- "react-dom": "^19.2.8",
44
+ "@types/react": "^19.3.0",
45
+ "@types/react-dom": "^19.3.0",
46
+ "react": "^19.3.0",
47
+ "react-dom": "^19.3.0",
48
48
  "rxjs": "^7.8.2",
49
- "@kheopskit/core": "5.1.2"
49
+ "@kheopskit/core": "5.2.1"
50
50
  },
51
51
  "tsdown": {
52
52
  "entry": [