mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-16 08:08:56 -05:00
92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
import React from 'react';
|
|
import { useEffect, useState } from 'react';
|
|
import { farcasterFrame } from '@farcaster/miniapp-wagmi-connector';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { createConfig, http, WagmiProvider } from 'wagmi';
|
|
import { useConnect, useAccount } from 'wagmi';
|
|
import { base, degen, mainnet, optimism, unichain, celo } from 'wagmi/chains';
|
|
import { coinbaseWallet, metaMask } from 'wagmi/connectors';
|
|
import { APP_NAME, APP_ICON_URL, APP_URL } from '~/lib/constants';
|
|
|
|
// Custom hook for Coinbase Wallet detection and auto-connection
|
|
function useCoinbaseWalletAutoConnect() {
|
|
const [isCoinbaseWallet, setIsCoinbaseWallet] = useState(false);
|
|
const { connect, connectors } = useConnect();
|
|
const { isConnected } = useAccount();
|
|
|
|
useEffect(() => {
|
|
// Check if we're running in Coinbase Wallet
|
|
const checkCoinbaseWallet = () => {
|
|
const isInCoinbaseWallet =
|
|
window.ethereum?.isCoinbaseWallet ||
|
|
window.ethereum?.isCoinbaseWalletExtension ||
|
|
window.ethereum?.isCoinbaseWalletBrowser;
|
|
setIsCoinbaseWallet(!!isInCoinbaseWallet);
|
|
};
|
|
|
|
checkCoinbaseWallet();
|
|
window.addEventListener('ethereum#initialized', checkCoinbaseWallet);
|
|
|
|
return () => {
|
|
window.removeEventListener('ethereum#initialized', checkCoinbaseWallet);
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
// Auto-connect if in Coinbase Wallet and not already connected
|
|
if (isCoinbaseWallet && !isConnected) {
|
|
connect({ connector: connectors[1] }); // Coinbase Wallet connector
|
|
}
|
|
}, [isCoinbaseWallet, isConnected, connect, connectors]);
|
|
|
|
return isCoinbaseWallet;
|
|
}
|
|
|
|
export const config = createConfig({
|
|
chains: [base, optimism, mainnet, degen, unichain, celo],
|
|
transports: {
|
|
[base.id]: http(),
|
|
[optimism.id]: http(),
|
|
[mainnet.id]: http(),
|
|
[degen.id]: http(),
|
|
[unichain.id]: http(),
|
|
[celo.id]: http(),
|
|
},
|
|
connectors: [
|
|
farcasterFrame(),
|
|
coinbaseWallet({
|
|
appName: APP_NAME,
|
|
appLogoUrl: APP_ICON_URL,
|
|
preference: 'all',
|
|
}),
|
|
metaMask({
|
|
dappMetadata: {
|
|
name: APP_NAME,
|
|
url: APP_URL,
|
|
},
|
|
}),
|
|
],
|
|
});
|
|
|
|
const queryClient = new QueryClient();
|
|
|
|
// Wrapper component that provides Coinbase Wallet auto-connection
|
|
function CoinbaseWalletAutoConnect({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
useCoinbaseWalletAutoConnect();
|
|
return <>{children}</>;
|
|
}
|
|
|
|
export default function Provider({ children }: { children: React.ReactNode }) {
|
|
return (
|
|
<WagmiProvider config={config}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<CoinbaseWalletAutoConnect>{children}</CoinbaseWalletAutoConnect>
|
|
</QueryClientProvider>
|
|
</WagmiProvider>
|
|
);
|
|
}
|