Skip to content

Commit

Permalink
feat: Auto logged in components
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Dec 17, 2024
1 parent cad1eb3 commit 44675e1
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-bats-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

-**docs**: Auto logged in components on landing page. @cpcramer #1754
118 changes: 118 additions & 0 deletions site/docs/components/AppDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use client';
import { OnchainKitProvider } from '@coinbase/onchainkit';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { ReactNode } from 'react';
import { http, WagmiProvider, createConfig } from 'wagmi';
import { base, baseSepolia } from 'wagmi/chains';
import { coinbaseWallet } from 'wagmi/connectors';
import '@coinbase/onchainkit/styles.css';
import { createWalletClient } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { useTheme } from '../contexts/Theme.tsx';

const queryClient = new QueryClient();

// WARNING: This is a publicly exposed private key used only for documentation demos.
// Do not use this key for any real transactions or store any assets in this account.
const DUMMY_PK =
'0xa67bdd8a49324aa36cb3f7f7064b9b560e3aa653b774be9793415c0a6fc62cf8';

const demoWalletConnector = (_config: any) => {
const account = privateKeyToAccount(DUMMY_PK as `0x${string}`);

const client = createWalletClient({
account,
chain: baseSepolia,
transport: http(),
});

return {
id: 'testWallet',
name: 'Test Wallet',
type: 'injected' as const,
async connect() {
return {
accounts: [account.address],
chainId: baseSepolia.id,
};
},
async switchChain({ chainId }: { chainId: number }) {
if (chainId === baseSepolia.id) {
return baseSepolia;
}
if (chainId === base.id) {
return base;
}
throw new Error('Unsupported chain');
},
async disconnect() {},
async getAccount() {
return account.address;
},
async getChainId() {
return baseSepolia.id;
},
async isAuthorized() {
return true;
},
onAccountsChanged() {},
onChainChanged() {},
onDisconnect() {},
async getAccounts() {
return [account.address];
},
async getProvider() {
return client;
},
};
};

const wagmiConfig = createConfig({
chains: [base, baseSepolia],
connectors: [
demoWalletConnector,
coinbaseWallet({
appName: 'OnchainKit',
}),
],
ssr: true,
transports: {
[base.id]: http(),
[baseSepolia.id]: http(),
},
});

export default function AppDemo({ children }: { children: ReactNode }) {
const isServer = typeof window === 'undefined';
if (isServer) {
return null;
}
const viteCdpApiKey = import.meta.env.VITE_CDP_API_KEY;
const viteProjectId = import.meta.env.VITE_CDP_PROJECT_ID;
const { theme } = useTheme();

return (
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<OnchainKitProvider
apiKey={viteCdpApiKey}
chain={base}
projectId={viteProjectId}
schemaId="0xf8b05c79f090979bf4a80270aba232dff11a10d9ca55c4f88de95317970f0de9"
config={{
appearance: {
mode: 'auto',
theme: theme,
},
autoConnect: true,
chains: [base, baseSepolia],
}}
>
<div style={{ display: 'flex', flexDirection: 'column' }}>
{children}
</div>
</OnchainKitProvider>
</QueryClientProvider>
</WagmiProvider>
);
}
6 changes: 3 additions & 3 deletions site/docs/components/landing/SwapDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SwapToggleButton,
} from '@coinbase/onchainkit/swap';
import { ConnectWallet, Wallet } from '@coinbase/onchainkit/wallet';
import App from '../App.tsx';
import AppDemo from '../AppDemo.tsx';
import SwapWrapper from '../SwapWrapper.tsx';

export const swapDemoCode = `
Expand Down Expand Up @@ -48,7 +48,7 @@ export const swapDemoCode = `
function SwapDemo() {
return (
<div className="mx-auto flex flex-col items-center justify-center">
<App>
<AppDemo>
<SwapWrapper>
{({ address, swappableTokens }) => {
if (address) {
Expand Down Expand Up @@ -84,7 +84,7 @@ function SwapDemo() {
);
}}
</SwapWrapper>
</App>
</AppDemo>
</div>
);
}
Expand Down
9 changes: 3 additions & 6 deletions site/docs/components/landing/WalletDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import {
Wallet,
WalletDropdown,
WalletDropdownBasename,
WalletDropdownDisconnect,
WalletDropdownLink,
} from '@coinbase/onchainkit/wallet';
import App from '../App.tsx';
import AppDemo from '../AppDemo.tsx';

export const walletDemoCode = `
import {
Expand All @@ -24,7 +23,6 @@ export const walletDemoCode = `
WalletDropdown,
WalletDropdownBasename,
WalletDropdownLink,
WalletDropdownDisconnect,
} from '@coinbase/onchainkit/wallet';
import {
Address,
Expand All @@ -42,7 +40,7 @@ export const walletDemoCode = `

function WalletDemo() {
return (
<App>
<AppDemo>
<Wallet>
<ConnectWallet>
<Avatar className="h-6 w-6" />
Expand All @@ -61,10 +59,9 @@ function WalletDemo() {
Wallet
</WalletDropdownLink>
<WalletDropdownBasename />
<WalletDropdownDisconnect />
</WalletDropdown>
</Wallet>
</App>
</AppDemo>
);
}

Expand Down

0 comments on commit 44675e1

Please sign in to comment.