Skip to content

Commit e71a58f

Browse files
committed
fix external wallets of when filtering chain namespaces
1 parent 94428d8 commit e71a58f

File tree

3 files changed

+34
-51
lines changed

3 files changed

+34
-51
lines changed

packages/modal/src/ui/components/ConnectWallet/ConnectWallet.tsx

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ChainNamespaceType, WALLET_CONNECTORS } from "@web3auth/no-modal";
2-
import { FormEvent, useContext, useEffect, useMemo, useState } from "react";
2+
import { FormEvent, useContext, useMemo, useState } from "react";
33

44
import { CONNECT_WALLET_PAGES } from "../../constants";
55
import { RootContext } from "../../context/RootContext";
@@ -11,8 +11,6 @@ import ConnectWalletList from "./ConnectWalletList";
1111
import ConnectWalletQrCode from "./ConnectWalletQrCode";
1212
import ConnectWalletSearch from "./ConnectWalletSearch";
1313

14-
const WALLET_LIMIT_COUNT = 10;
15-
1614
function ConnectWallet(props: ConnectWalletProps) {
1715
const {
1816
isDark,
@@ -22,7 +20,6 @@ function ConnectWallet(props: ConnectWalletProps) {
2220
walletConnectUri,
2321
walletRegistry,
2422
allExternalButtons,
25-
totalExternalWallets,
2623
customAdapterButtons,
2724
adapterVisibilityMap,
2825
deviceDetails,
@@ -35,13 +32,11 @@ function ConnectWallet(props: ConnectWalletProps) {
3532

3633
const [currentPage, setCurrentPage] = useState(CONNECT_WALLET_PAGES.CONNECT_WALLET);
3734
const [selectedWallet, setSelectedWallet] = useState(false);
38-
const [externalButtons, setExternalButtons] = useState<ExternalButton[]>([]);
39-
const [totalExternalWalletsCount, setTotalExternalWalletsCount] = useState<number>(0);
35+
const [isLoading] = useState<boolean>(false);
4036
const [selectedButton, setSelectedButton] = useState<ExternalButton>(null);
4137
const [walletSearch, setWalletSearch] = useState<string>("");
42-
const [isLoading, setIsLoading] = useState<boolean>(true);
4338
const [selectedChain, setSelectedChain] = useState<string>("all");
44-
const [initialWalletCount, setInitialWalletCount] = useState<number>(0);
39+
const [isShowAllWallets, setIsShowAllWallets] = useState<boolean>(false);
4540

4641
const handleBack = () => {
4742
if (!selectedWallet && currentPage === CONNECT_WALLET_PAGES.CONNECT_WALLET && onBackClick) {
@@ -69,13 +64,9 @@ function ConnectWallet(props: ConnectWalletProps) {
6964
});
7065
}, [allExternalButtons, customAdapterButtons]);
7166

72-
const filteredButtons = (searchValue: string) => {
73-
return allUniqueButtons.filter((button) => button.name.toLowerCase().includes(searchValue.toLowerCase()));
74-
};
75-
7667
const defaultButtonKeys = useMemo(() => new Set(Object.keys(walletRegistry.default)), [walletRegistry]);
7768

78-
const sortedButtons = useMemo(() => {
69+
const defaultButtons = useMemo(() => {
7970
// display order: default injected buttons > custom adapter buttons > default non-injected buttons
8071
const buttons = [
8172
...allExternalButtons.filter((button) => button.hasInjectedWallet && defaultButtonKeys.has(button.name)),
@@ -91,7 +82,7 @@ function ConnectWallet(props: ConnectWalletProps) {
9182
});
9283
}, [allExternalButtons, customAdapterButtons, defaultButtonKeys]);
9384

94-
const visibleButtons = useMemo(() => {
85+
const installedWalletButtons = useMemo(() => {
9586
const visibilityMap = adapterVisibilityMap;
9687
return Object.keys(config).reduce((acc, adapter) => {
9788
if (![WALLET_CONNECTORS.WALLET_CONNECT_V2].includes(adapter) && visibilityMap[adapter]) {
@@ -110,27 +101,38 @@ function ConnectWallet(props: ConnectWalletProps) {
110101
const handleWalletSearch = (e: FormEvent<HTMLInputElement>) => {
111102
const searchValue = (e.target as HTMLInputElement).value;
112103
setWalletSearch(searchValue);
113-
if (searchValue) {
114-
setExternalButtons(filteredButtons(searchValue));
115-
} else {
116-
setExternalButtons(sortedButtons);
117-
}
118-
setInitialWalletCount(sortedButtons.length);
119104
};
120105

121-
useEffect(() => {
106+
const handleChainFilterChange = (chain: string) => {
107+
setSelectedChain(chain);
108+
setIsShowAllWallets(false);
109+
};
110+
111+
const filteredButtons = useMemo(() => {
122112
if (walletDiscoverySupported) {
123-
setExternalButtons(sortedButtons);
124-
setInitialWalletCount(sortedButtons.length);
125-
setTotalExternalWalletsCount(totalExternalWallets);
126-
} else {
127-
setExternalButtons(visibleButtons);
128-
setTotalExternalWalletsCount(visibleButtons.length);
113+
const buttons = allUniqueButtons;
114+
return buttons
115+
.filter((button) => selectedChain === "all" || button.chainNamespaces.includes(selectedChain as ChainNamespaceType))
116+
.filter((button) => button.name.toLowerCase().includes(walletSearch.toLowerCase()));
129117
}
130-
setTimeout(() => {
131-
setIsLoading(false);
132-
}, 0);
133-
}, [walletDiscoverySupported, sortedButtons, visibleButtons, totalExternalWallets]);
118+
return installedWalletButtons;
119+
}, [walletDiscoverySupported, installedWalletButtons, walletSearch, allUniqueButtons, selectedChain]);
120+
121+
const externalButtons = useMemo(() => {
122+
if (walletDiscoverySupported && !walletSearch && !isShowAllWallets) {
123+
return defaultButtons;
124+
}
125+
return filteredButtons;
126+
}, [walletDiscoverySupported, walletSearch, filteredButtons, defaultButtons, isShowAllWallets]);
127+
128+
const totalExternalWalletsCount = useMemo(() => {
129+
return filteredButtons.length;
130+
}, [filteredButtons]);
131+
132+
const initialWalletCount = useMemo(() => {
133+
if (isShowAllWallets) return totalExternalWalletsCount;
134+
return walletDiscoverySupported ? defaultButtons.length : installedWalletButtons.length;
135+
}, [walletDiscoverySupported, defaultButtons, installedWalletButtons, isShowAllWallets, totalExternalWalletsCount]);
134136

135137
const handleWalletClick = (button: ExternalButton) => {
136138
const isInjectedConnectorAndSingleChainNamespace = button.hasInjectedWallet && button.chainNamespaces?.length === 1;
@@ -155,24 +157,7 @@ function ConnectWallet(props: ConnectWalletProps) {
155157
};
156158

157159
const handleMoreWallets = () => {
158-
// setIsLoading(true);
159-
setInitialWalletCount((prev) => prev + 10);
160-
const buttons = allUniqueButtons.slice(initialWalletCount, initialWalletCount + WALLET_LIMIT_COUNT);
161-
setExternalButtons((prev) => [...prev, ...buttons]);
162-
setInitialWalletCount((prev) => prev + WALLET_LIMIT_COUNT);
163-
};
164-
165-
const handleChainFilterChange = (chain: string) => {
166-
setInitialWalletCount(0);
167-
setSelectedChain(chain);
168-
if (chain === "all") {
169-
setExternalButtons(sortedButtons.slice(0, WALLET_LIMIT_COUNT));
170-
setTotalExternalWalletsCount(sortedButtons.length);
171-
} else {
172-
const filteredButtons = sortedButtons.filter((button) => button.chainNamespaces.includes(chain as ChainNamespaceType));
173-
setExternalButtons(filteredButtons.slice(0, WALLET_LIMIT_COUNT));
174-
setTotalExternalWalletsCount(filteredButtons.length);
175-
}
160+
setIsShowAllWallets(true);
176161
};
177162

178163
return (

packages/modal/src/ui/components/ConnectWallet/ConnectWallet.type.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export interface ConnectWalletProps {
88
walletConnectUri: string | undefined;
99
walletRegistry?: WalletRegistry;
1010
allExternalButtons: ExternalButton[];
11-
totalExternalWallets: number;
1211
customAdapterButtons: ExternalButton[];
1312
adapterVisibilityMap: Record<string, boolean>;
1413
deviceDetails: { platform: platform; browser: browser; os: os };

packages/modal/src/ui/components/Root/Root.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ function Root(props: RootProps) {
426426
walletConnectUri={modalState.walletConnectUri}
427427
config={modalState.externalWalletsConfig}
428428
walletRegistry={walletRegistry}
429-
totalExternalWallets={totalExternalWalletsLength}
430429
allExternalButtons={allButtons}
431430
adapterVisibilityMap={adapterVisibilityMap}
432431
customAdapterButtons={customAdapterButtons}

0 commit comments

Comments
 (0)