|
| 1 | +import { useContext, routes } from '../../ConnectKit'; |
| 2 | + |
| 3 | +import { |
| 4 | + ConnectorsContainer, |
| 5 | + ConnectorButton, |
| 6 | + ConnectorLabel, |
| 7 | + ConnectorIcon, |
| 8 | + RecentlyUsedTag, |
| 9 | +} from './styles'; |
| 10 | + |
| 11 | +import { useWallets } from '../../../hooks/useWallets'; |
| 12 | +import { useInjectedWallet } from '../../../hooks/connectors/useInjectedWallet'; |
| 13 | +import { isInjectedConnector, isWalletConnectConnector } from '../../../utils'; |
| 14 | + |
| 15 | +import { useWalletConnectUri } from '../../../hooks/connectors/useWalletConnectUri'; |
| 16 | +import { useLastConnector } from '../../../hooks/useLastConnector'; |
| 17 | +import useIsMobile from '../../../hooks/useIsMobile'; |
| 18 | + |
| 19 | +import { ScrollArea } from '../../Common/ScrollArea'; |
| 20 | +import Alert from '../Alert'; |
| 21 | + |
| 22 | +const ConnectorList = () => { |
| 23 | + const context = useContext(); |
| 24 | + const isMobile = useIsMobile(); |
| 25 | + |
| 26 | + const { uri } = useWalletConnectUri(); |
| 27 | + const { lastConnectorId } = useLastConnector(); |
| 28 | + const injectedWallet = useInjectedWallet(); |
| 29 | + |
| 30 | + const wallets = useWallets(); |
| 31 | + |
| 32 | + const walletsToDisplay = |
| 33 | + context.options?.hideRecentBadge || |
| 34 | + lastConnectorId === 'walletConnect-WalletConnect' // do not hoist walletconnect to top of list |
| 35 | + ? wallets |
| 36 | + : [ |
| 37 | + // move last used wallet to top of list |
| 38 | + // using .filter and spread to avoid mutating original array order with .sort |
| 39 | + ...wallets.filter( |
| 40 | + (wallet) => |
| 41 | + lastConnectorId === |
| 42 | + `${wallet.connector.id}-${wallet.connector.name}` |
| 43 | + ), |
| 44 | + ...wallets.filter( |
| 45 | + (wallet) => |
| 46 | + lastConnectorId !== |
| 47 | + `${wallet.connector.id}-${wallet.connector.name}` |
| 48 | + ), |
| 49 | + ]; |
| 50 | + |
| 51 | + return ( |
| 52 | + <ScrollArea mobileDirection={'horizontal'}> |
| 53 | + {walletsToDisplay.length === 0 && ( |
| 54 | + <Alert error>No connectors found in ConnectKit config.</Alert> |
| 55 | + )} |
| 56 | + {walletsToDisplay.length > 0 && ( |
| 57 | + <ConnectorsContainer |
| 58 | + $mobile={isMobile} |
| 59 | + $totalResults={walletsToDisplay.length} |
| 60 | + > |
| 61 | + {walletsToDisplay.map((wallet) => { |
| 62 | + const { |
| 63 | + id, |
| 64 | + name, |
| 65 | + shortName, |
| 66 | + icon, |
| 67 | + iconConnector, |
| 68 | + connector, |
| 69 | + createUri, |
| 70 | + } = wallet; |
| 71 | + |
| 72 | + let deeplink = isMobile ? createUri?.(uri ?? '') : undefined; |
| 73 | + |
| 74 | + const redirectToMoreWallets = |
| 75 | + isMobile && isWalletConnectConnector(id); |
| 76 | + |
| 77 | + if (isInjectedConnector(id) && !injectedWallet.enabled) return null; |
| 78 | + if (redirectToMoreWallets) deeplink = undefined; // mobile redirects to more wallets page |
| 79 | + |
| 80 | + const walletInfo = |
| 81 | + isInjectedConnector(wallet.id) && injectedWallet.enabled |
| 82 | + ? // && injectedWallet.wallet.name === wallet.name |
| 83 | + { |
| 84 | + name: injectedWallet.wallet.name, |
| 85 | + shortName: |
| 86 | + injectedWallet.wallet.shortName ?? |
| 87 | + injectedWallet.wallet.name, |
| 88 | + icon: injectedWallet.wallet.icon, |
| 89 | + //iconRadius: 0, |
| 90 | + } |
| 91 | + : { |
| 92 | + name: name, |
| 93 | + shortName: shortName ?? name, |
| 94 | + icon: iconConnector ?? icon, |
| 95 | + iconRadius: wallet.id === 'walletConnect' ? 0 : undefined, |
| 96 | + }; |
| 97 | + |
| 98 | + const ButtonInner = ({ |
| 99 | + disabled = false, |
| 100 | + }: { |
| 101 | + disabled?: boolean; |
| 102 | + }) => ( |
| 103 | + <ConnectorButton |
| 104 | + as={deeplink ? 'a' : undefined} |
| 105 | + href={deeplink ? deeplink : undefined} |
| 106 | + disabled={disabled || context.route !== routes.CONNECTORS} |
| 107 | + onClick={ |
| 108 | + deeplink |
| 109 | + ? undefined |
| 110 | + : () => { |
| 111 | + if (redirectToMoreWallets) { |
| 112 | + context.setRoute(routes.MOBILECONNECTORS); |
| 113 | + } else { |
| 114 | + context.setRoute(routes.CONNECT); |
| 115 | + context.setConnector({ id: id, name: name }); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + > |
| 120 | + <ConnectorIcon |
| 121 | + data-small={wallet.iconShouldShrink} |
| 122 | + style={{ |
| 123 | + borderRadius: walletInfo.iconRadius, |
| 124 | + }} |
| 125 | + > |
| 126 | + {walletInfo.icon} |
| 127 | + </ConnectorIcon> |
| 128 | + <ConnectorLabel> |
| 129 | + {isMobile ? walletInfo.shortName : walletInfo.name} |
| 130 | + {!context.options?.hideRecentBadge && |
| 131 | + lastConnectorId === `${connector.id}-${connector.name}` && ( |
| 132 | + <RecentlyUsedTag> |
| 133 | + <span>Recent</span> |
| 134 | + </RecentlyUsedTag> |
| 135 | + )} |
| 136 | + </ConnectorLabel> |
| 137 | + </ConnectorButton> |
| 138 | + ); |
| 139 | + /* |
| 140 | + if (!connector.ready && injectedWallet.enabled) { |
| 141 | + return ( |
| 142 | + <Tooltip |
| 143 | + key={id} |
| 144 | + xOffset={18} |
| 145 | + message={ |
| 146 | + <div style={{ width: 230, padding: '6px 4px' }}> |
| 147 | + {name} Unavailable as {injectedWallet?.wallet?.name} is |
| 148 | + installed. Disable {injectedWallet?.wallet?.name} to |
| 149 | + connect with {name}. |
| 150 | + </div> |
| 151 | + } |
| 152 | + delay={0} |
| 153 | + > |
| 154 | + <ButtonInner disabled /> |
| 155 | + </Tooltip> |
| 156 | + ); |
| 157 | + } |
| 158 | + */ |
| 159 | + return <ButtonInner key={id} />; |
| 160 | + })} |
| 161 | + </ConnectorsContainer> |
| 162 | + )} |
| 163 | + </ScrollArea> |
| 164 | + ); |
| 165 | +}; |
| 166 | + |
| 167 | +export default ConnectorList; |
0 commit comments