|
| 1 | +import type { |
| 2 | + UserProfileWeb3Provider, |
| 3 | + UserProfileWeb3Wallet, |
| 4 | +} from '@clerk/ui/mosaic/features/user-profile/user-profile-web3-wallets-section.view'; |
| 5 | +import { useState } from 'react'; |
| 6 | + |
| 7 | +interface DemoWallet extends UserProfileWeb3Wallet { |
| 8 | + providerId?: string; |
| 9 | +} |
| 10 | + |
| 11 | +const providers: UserProfileWeb3Provider[] = [ |
| 12 | + { id: 'metamask', provider: 'MetaMask', iconUrl: 'https://img.clerk.com/static/metamask.svg' }, |
| 13 | + { id: 'coinbase-wallet', provider: 'Coinbase Wallet', iconUrl: 'https://img.clerk.com/static/coinbase_wallet.svg' }, |
| 14 | +]; |
| 15 | + |
| 16 | +export const primaryWallet: DemoWallet = { |
| 17 | + id: 'wallet_1', |
| 18 | + providerId: 'metamask', |
| 19 | + provider: 'MetaMask', |
| 20 | + iconUrl: 'https://img.clerk.com/static/metamask.svg', |
| 21 | + address: '0x71C7656EC7ab88b098defB751B7401B5f6d8976F', |
| 22 | + isPrimary: true, |
| 23 | + isVerified: true, |
| 24 | +}; |
| 25 | + |
| 26 | +export const secondaryWallet: DemoWallet = { |
| 27 | + id: 'wallet_2', |
| 28 | + providerId: 'coinbase-wallet', |
| 29 | + provider: 'Coinbase Wallet', |
| 30 | + iconUrl: 'https://img.clerk.com/static/coinbase_wallet.svg', |
| 31 | + address: '0x1234567890abcdef1234567890abcdef12345678', |
| 32 | + isVerified: true, |
| 33 | +}; |
| 34 | + |
| 35 | +export function useWeb3WalletsFixture({ |
| 36 | + initialWallets = [primaryWallet], |
| 37 | + availableProviders = providers, |
| 38 | + primaryError = false, |
| 39 | + removalState, |
| 40 | +}: { |
| 41 | + initialWallets?: DemoWallet[]; |
| 42 | + availableProviders?: UserProfileWeb3Provider[]; |
| 43 | + primaryError?: boolean; |
| 44 | + removalState?: 'pending' | 'error'; |
| 45 | +} = {}) { |
| 46 | + const [wallets, setWallets] = useState(initialWallets); |
| 47 | + const [connectionProviders, setConnectionProviders] = useState(availableProviders); |
| 48 | + const [primaryFailed, setPrimaryFailed] = useState(false); |
| 49 | + |
| 50 | + return { |
| 51 | + wallets, |
| 52 | + availableProviders: connectionProviders.filter( |
| 53 | + provider => !wallets.some(wallet => wallet.providerId === provider.id && wallet.isVerified), |
| 54 | + ), |
| 55 | + onConnect: (id: string) => { |
| 56 | + const provider = connectionProviders.find(item => item.id === id); |
| 57 | + if (!provider) { |
| 58 | + return; |
| 59 | + } |
| 60 | + setConnectionProviders(current => |
| 61 | + current.map(item => (item.id === id ? { ...item, connectError: undefined } : item)), |
| 62 | + ); |
| 63 | + setWallets(current => { |
| 64 | + if (current.some(wallet => wallet.providerId === id && !wallet.isVerified)) { |
| 65 | + return current.map(wallet => (wallet.providerId === id ? { ...wallet, isVerified: true } : wallet)); |
| 66 | + } |
| 67 | + return [ |
| 68 | + ...current, |
| 69 | + { |
| 70 | + provider: provider.provider, |
| 71 | + iconUrl: provider.iconUrl, |
| 72 | + id: `wallet_${id}`, |
| 73 | + providerId: id, |
| 74 | + address: '0x1234567890abcdef1234567890abcdef12345678', |
| 75 | + isVerified: true, |
| 76 | + isPrimary: !current.some(wallet => wallet.isPrimary), |
| 77 | + }, |
| 78 | + ]; |
| 79 | + }); |
| 80 | + }, |
| 81 | + onSetPrimary: (id: string) => { |
| 82 | + if (primaryError && !primaryFailed) { |
| 83 | + setPrimaryFailed(true); |
| 84 | + setWallets(current => |
| 85 | + current.map(wallet => |
| 86 | + wallet.id === id |
| 87 | + ? { ...wallet, primaryError: 'Unable to set this wallet as primary. Please try again.' } |
| 88 | + : wallet, |
| 89 | + ), |
| 90 | + ); |
| 91 | + return; |
| 92 | + } |
| 93 | + setWallets(current => |
| 94 | + current.map(wallet => ({ ...wallet, isPrimary: wallet.id === id, primaryError: undefined })), |
| 95 | + ); |
| 96 | + }, |
| 97 | + onRemove: (id: string) => { |
| 98 | + if (removalState === 'pending') { |
| 99 | + setWallets(current => current.map(wallet => (wallet.id === id ? { ...wallet, isRemoving: true } : wallet))); |
| 100 | + setTimeout(() => { |
| 101 | + setWallets(current => current.filter(wallet => wallet.id !== id)); |
| 102 | + }, 1500); |
| 103 | + return; |
| 104 | + } |
| 105 | + setWallets(current => { |
| 106 | + if (removalState === 'error' && !current.find(wallet => wallet.id === id)?.removalError) { |
| 107 | + return current.map(wallet => |
| 108 | + wallet.id === id ? { ...wallet, removalError: 'Unable to remove wallet. Please try again.' } : wallet, |
| 109 | + ); |
| 110 | + } |
| 111 | + return current.filter(wallet => wallet.id !== id); |
| 112 | + }); |
| 113 | + }, |
| 114 | + }; |
| 115 | +} |
0 commit comments