Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Enable auto logged in components on landing page #1754

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
119 changes: 119 additions & 0 deletions site/docs/components/AppDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'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 type { Config } from 'wagmi';
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' as const;

const demoWalletConnector = (_config: Config) => {
const account = privateKeyToAccount(DUMMY_PK);

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 />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that we are removing disconnect option, consider adding a warning like, "wallet address for demonstration purposes only. do not send funds to this address"

</WalletDropdown>
</Wallet>
</App>
</AppDemo>
);
}

Expand Down
Loading