Skip to content

Commit

Permalink
fix: de-dupe wallets in connectorsForWallets (#1910)
Browse files Browse the repository at this point in the history
* chore: de-dupe wallets

* chore: small tweak

* chore: format

* Update sour-bobcats-dance.md

---------

Co-authored-by: Magomed Khamidov <53529533+KosmosKey@users.noreply.github.com>
Co-authored-by: Daniel Sinclair <d@niel.nyc>
  • Loading branch information
3 people committed Apr 3, 2024
1 parent fc4d7e1 commit 4dd1e45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-bobcats-dance.md
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Fixed an issue that allowed duplicate wallets to be added to the Connect Modal when using `connectorsForWallets`
14 changes: 14 additions & 0 deletions packages/rainbowkit/src/utils/uniqueBy.ts
@@ -0,0 +1,14 @@
export function uniqueBy<key extends string, item extends Record<key, any>>(
items: item[],
key: key,
): item[] {
const filtered: item[] = [];

for (const item of items) {
if (!filtered.some((x) => x[key] === item[key])) {
filtered.push(item);
}
}

return filtered;
}
10 changes: 6 additions & 4 deletions packages/rainbowkit/src/wallets/connectorsForWallets.ts
@@ -1,6 +1,7 @@
import type { CreateConnectorFn } from 'wagmi';
import { isHexString } from '../utils/colors';
import { omitUndefinedValues } from '../utils/omitUndefinedValues';
import { uniqueBy } from '../utils/uniqueBy';
import type {
RainbowKitWalletConnectParameters,
Wallet,
Expand Down Expand Up @@ -93,12 +94,13 @@ export const connectorsForWallets = (
});
});

// Filtering out duplicated wallets in case there is any.
// We process the known visible wallets first so that the potentially
// hidden wallets have access to the complete list of resolved wallets
const walletListItems: WalletListItem[] = [
...visibleWallets,
...potentiallyHiddenWallets,
];
const walletListItems: WalletListItem[] = uniqueBy(
[...visibleWallets, ...potentiallyHiddenWallets],
'id',
);

for (const {
createConnector,
Expand Down

0 comments on commit 4dd1e45

Please sign in to comment.