|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { ReactNode, useEffect } from "react"; |
| 4 | +import { |
| 5 | + createNetworkConfig, |
| 6 | + SuiClientProvider, |
| 7 | + WalletProvider, |
| 8 | + useSuiClientContext, |
| 9 | +} from "@mysten/dapp-kit"; |
| 10 | +import { getFullnodeUrl } from "@mysten/sui/client"; |
| 11 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 12 | +import { isEnokiNetwork, registerEnokiWallets } from "@mysten/enoki"; |
| 13 | +import { enokiConfig } from "@/config/enoki"; |
| 14 | +import "@mysten/dapp-kit/dist/index.css"; |
| 15 | + |
| 16 | +const { networkConfig } = createNetworkConfig({ |
| 17 | + testnet: { url: getFullnodeUrl("testnet") }, |
| 18 | + mainnet: { url: getFullnodeUrl("mainnet") }, |
| 19 | +}); |
| 20 | + |
| 21 | +const queryClient = new QueryClient({ |
| 22 | + defaultOptions: { |
| 23 | + queries: { |
| 24 | + refetchOnWindowFocus: false, |
| 25 | + retry: false, |
| 26 | + }, |
| 27 | + }, |
| 28 | +}); |
| 29 | + |
| 30 | +function RegisterEnokiWallets() { |
| 31 | + const { client, network } = useSuiClientContext(); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (!isEnokiNetwork(network)) return; |
| 35 | + |
| 36 | + const redirectUrl = |
| 37 | + typeof window !== "undefined" |
| 38 | + ? `${window.location.origin}` |
| 39 | + : "http://localhost:3000"; |
| 40 | + |
| 41 | + const { unregister } = registerEnokiWallets({ |
| 42 | + apiKey: enokiConfig.apiKey, |
| 43 | + providers: { |
| 44 | + google: { |
| 45 | + clientId: enokiConfig.googleClientId, |
| 46 | + redirectUrl, |
| 47 | + }, |
| 48 | + }, |
| 49 | + client, |
| 50 | + network, |
| 51 | + }); |
| 52 | + |
| 53 | + return unregister; |
| 54 | + }, [client, network]); |
| 55 | + |
| 56 | + return null; |
| 57 | +} |
| 58 | + |
| 59 | +export function EnokiProviders({ children }: { children: ReactNode }) { |
| 60 | + return ( |
| 61 | + <QueryClientProvider client={queryClient}> |
| 62 | + <SuiClientProvider networks={networkConfig} defaultNetwork="testnet"> |
| 63 | + <RegisterEnokiWallets /> |
| 64 | + <WalletProvider autoConnect={true}>{children}</WalletProvider> |
| 65 | + </SuiClientProvider> |
| 66 | + </QueryClientProvider> |
| 67 | + ); |
| 68 | +} |
0 commit comments