11import { 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
44import { CONNECT_WALLET_PAGES } from "../../constants" ;
55import { RootContext } from "../../context/RootContext" ;
@@ -11,8 +11,6 @@ import ConnectWalletList from "./ConnectWalletList";
1111import ConnectWalletQrCode from "./ConnectWalletQrCode" ;
1212import ConnectWalletSearch from "./ConnectWalletSearch" ;
1313
14- const WALLET_LIMIT_COUNT = 10 ;
15-
1614function 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 (
0 commit comments