Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the sorting in the confirm wallets modal #784

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions widget/embedded/src/components/ConfirmWalletsModal/WalletList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { useAppStore } from '../../store/AppStore';
import { useUiStore } from '../../store/ui';
import { useWalletsStore } from '../../store/wallets';
import { removeDuplicateFrom } from '../../utils/common';
import { getBlockchainDisplayNameFor } from '../../utils/meta';
import {
getAddress,
Expand Down Expand Up @@ -117,23 +118,65 @@ export function WalletList(props: PropTypes) {
};

useEffect(() => {
setSortedList((sortedList) => {
const selectedWalletIndex = list.findIndex((wallet) =>
isSelected(wallet.type, chain)
);
const selectedWalletIndex = list.findIndex((wallet) =>
isSelected(wallet.type, chain)
);

if (shouldShowMoreWallets && selectedWalletIndex > 1) {
return [list[selectedWalletIndex]].concat(
list.filter((_, index) => index !== selectedWalletIndex)
const updateSortedList = (
list: WalletInfoWithNamespaces[],
selectedWalletIndex: number
) => {
const selectedWallet = list[selectedWalletIndex];
if (selectedWallet) {
setSortedList(
[selectedWallet].concat(
list.filter((_, index) => index !== selectedWalletIndex)
)
);
} else {
setSortedList(list);
}
};

if (!limit) {
return updateSortedList(list, selectedWalletIndex);
}

const prevConnectedWallets = sortedList
.filter((wallet) => wallet.state === WalletState.CONNECTED)
.map((wallet) => wallet.type);

const nextConnectedWallets = list
.filter((wallet) => wallet.state === WalletState.CONNECTED)
.map((wallet) => wallet.type);

const allConnectedWallets = removeDuplicateFrom(
prevConnectedWallets.concat(nextConnectedWallets)
);

const connectedWallets = allConnectedWallets.filter(
(wallet) => !prevConnectedWallets.includes(wallet)
);

const disconnectedWallets = allConnectedWallets.filter(
(wallet) => !nextConnectedWallets.includes(wallet)
);

connectedWallets.forEach(() => updateSortedList(list, selectedWalletIndex));
disconnectedWallets.forEach((wallet) => {
if (!isSelected(wallet, chain)) {
updateSortedList(list, selectedWalletIndex);
}
return sortedList.map(
(sortedItem) =>
list.find((listItem) => listItem.type === sortedItem.type) ??
sortedItem
);
});
}, [JSON.stringify(list)]);

if (
!connectedWallets.length &&
!disconnectedWallets.length &&
list.findIndex((wallet) => isSelected(wallet.type, chain)) >= limit
) {
updateSortedList(list, selectedWalletIndex);
}
}, [JSON.stringify(list), isSelected]);

const modalContainer = document.getElementById(
WIDGET_UI_ID.SWAP_BOX_ID
Expand Down