|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { |
4 | | - StellarWalletsKit, |
5 | | - FREIGHTER_ID, |
6 | | - FreighterModule, |
7 | | - AlbedoModule, |
8 | | - xBullModule, |
9 | | - LobstrModule, |
10 | | -} from "@creit.tech/stellar-wallets-kit"; |
11 | | -import { LedgerModule } from "@creit.tech/stellar-wallets-kit/modules/ledger.module"; |
12 | | -import { |
13 | | - WalletConnectAllowedMethods, |
14 | | - WalletConnectModule, |
15 | | -} from "@creit.tech/stellar-wallets-kit/modules/walletconnect.module"; |
| 3 | +import type { StellarWalletsKit as StellarWalletsKitClass } from "@creit.tech/stellar-wallets-kit"; |
| 4 | +import { Networks } from "@creit.tech/stellar-wallets-kit/types"; |
16 | 5 | import { getCurrentNetworkPassphrase } from "../network"; |
17 | 6 |
|
18 | | -let stellarWalletKitInstance: StellarWalletsKit | null = null; |
| 7 | +type Kit = typeof StellarWalletsKitClass; |
19 | 8 |
|
20 | | -export function getStellarWalletKit(): StellarWalletsKit { |
| 9 | +let kit: Kit | null = null; |
| 10 | +let initPromise: Promise<Kit> | null = null; |
| 11 | + |
| 12 | +/** |
| 13 | + * Lazily loads and initialises the Stellar Wallets Kit. |
| 14 | + * |
| 15 | + * The @creit.tech/stellar-wallets-kit v2 root module initialises |
| 16 | + * @preact/signals at module scope and reads `globalThis.localStorage`, |
| 17 | + * which crashes during Next.js SSR/static prerendering. By dynamically |
| 18 | + * importing the package only inside this function we guarantee the code |
| 19 | + * only ever runs in the browser. |
| 20 | + */ |
| 21 | +export async function getStellarWalletKit(): Promise<Kit> { |
21 | 22 | if (typeof window === "undefined") { |
22 | | - throw new Error("Stellar Wallet Kit solo puede usarse en el navegador."); |
| 23 | + throw new Error("Stellar Wallet Kit can only be used in the browser."); |
23 | 24 | } |
24 | | - if (!stellarWalletKitInstance) { |
25 | | - const network = getCurrentNetworkPassphrase(); |
26 | | - stellarWalletKitInstance = new StellarWalletsKit({ |
27 | | - network, |
28 | | - selectedWalletId: FREIGHTER_ID, |
29 | | - modalTheme: { |
30 | | - bgColor: "#1C1C1C", |
31 | | - textColor: "rgba(255,255,255,0.85)", |
32 | | - solidTextColor: "#FFFFFF", |
33 | | - headerButtonColor: "#229EDF", |
34 | | - dividerColor: "rgba(255,255,255,0.08)", |
35 | | - helpBgColor: "rgba(255,255,255,0.04)", |
36 | | - notAvailableTextColor: "rgba(255,255,255,0.35)", |
37 | | - notAvailableBgColor: "rgba(255,255,255,0.04)", |
38 | | - notAvailableBorderColor: "rgba(255,255,255,0.08)", |
39 | | - }, |
40 | | - modules: [ |
41 | | - new FreighterModule(), |
42 | | - new AlbedoModule(), |
43 | | - new xBullModule(), |
44 | | - new LedgerModule(), |
45 | | - new LobstrModule(), |
46 | | - new WalletConnectModule({ |
47 | | - url: "https://nekoprotocol.xyz", |
48 | | - projectId: process.env.NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID ?? "", |
49 | | - method: WalletConnectAllowedMethods.SIGN, |
50 | | - name: "Neko Protocol", |
51 | | - description: "Neko - All in one platform for RWA tokens", |
52 | | - icons: ["/Neko.svg"], |
53 | | - network, |
54 | | - }), |
55 | | - ], |
56 | | - }); |
| 25 | + |
| 26 | + if (kit) return kit; |
| 27 | + |
| 28 | + if (!initPromise) { |
| 29 | + initPromise = (async () => { |
| 30 | + const [ |
| 31 | + { StellarWalletsKit }, |
| 32 | + { FreighterModule }, |
| 33 | + { AlbedoModule }, |
| 34 | + { xBullModule }, |
| 35 | + { LedgerModule }, |
| 36 | + { LobstrModule }, |
| 37 | + { WalletConnectModule }, |
| 38 | + ] = await Promise.all([ |
| 39 | + import("@creit.tech/stellar-wallets-kit"), |
| 40 | + import("@creit.tech/stellar-wallets-kit/modules/freighter"), |
| 41 | + import("@creit.tech/stellar-wallets-kit/modules/albedo"), |
| 42 | + import("@creit.tech/stellar-wallets-kit/modules/xbull"), |
| 43 | + import("@creit.tech/stellar-wallets-kit/modules/ledger"), |
| 44 | + import("@creit.tech/stellar-wallets-kit/modules/lobstr"), |
| 45 | + import("@creit.tech/stellar-wallets-kit/modules/wallet-connect"), |
| 46 | + ]); |
| 47 | + |
| 48 | + const network = getCurrentNetworkPassphrase() as Networks; |
| 49 | + |
| 50 | + StellarWalletsKit.init({ |
| 51 | + network, |
| 52 | + modules: [ |
| 53 | + new FreighterModule(), |
| 54 | + new AlbedoModule(), |
| 55 | + new xBullModule(), |
| 56 | + new LedgerModule(), |
| 57 | + new LobstrModule(), |
| 58 | + new WalletConnectModule({ |
| 59 | + projectId: process.env.NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID || "", |
| 60 | + metadata: { |
| 61 | + name: "Neko", |
| 62 | + description: "Neko — All-in-one platform for RWAs on Stellar", |
| 63 | + url: "https://nekoprotocol.xyz", |
| 64 | + icons: ["/Neko.svg"], |
| 65 | + }, |
| 66 | + }), |
| 67 | + ], |
| 68 | + theme: { |
| 69 | + background: "#1C1C1C", |
| 70 | + "background-secondary": "#121212", |
| 71 | + "foreground-strong": "#FFFFFF", |
| 72 | + foreground: "rgba(255,255,255,0.85)", |
| 73 | + "foreground-secondary": "rgba(255,255,255,0.6)", |
| 74 | + primary: "#229EDF", |
| 75 | + "primary-foreground": "#FFFFFF", |
| 76 | + transparent: "rgba(0, 0, 0, 0)", |
| 77 | + lighter: "rgba(255,255,255,0.08)", |
| 78 | + light: "rgba(255,255,255,0.06)", |
| 79 | + "light-gray": "rgba(255,255,255,0.35)", |
| 80 | + gray: "rgba(255,255,255,0.25)", |
| 81 | + danger: "oklch(57.7% 0.245 27.325)", |
| 82 | + border: "rgba(255,255,255,0.08)", |
| 83 | + shadow: |
| 84 | + "0 10px 15px -3px rgba(0,0,0,0.3), 0 4px 6px -4px rgba(0,0,0,0.2)", |
| 85 | + "border-radius": "0.75rem", |
| 86 | + "font-family": "inherit", |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + kit = StellarWalletsKit; |
| 91 | + return StellarWalletsKit; |
| 92 | + })(); |
57 | 93 | } |
58 | | - return stellarWalletKitInstance; |
59 | | -} |
60 | 94 |
|
61 | | -export function resetStellarWalletKit(): void { |
62 | | - stellarWalletKitInstance = null; |
| 95 | + return initPromise; |
63 | 96 | } |
64 | 97 |
|
65 | 98 | export const getWallet = () => getStellarWalletKit(); |
0 commit comments