From ec97b00c32448b22eb2cc530372af4fb8855c18a Mon Sep 17 00:00:00 2001 From: Iris Date: Fri, 9 Aug 2024 10:24:50 +0200 Subject: [PATCH] fix: sort starknet connectors (#21) --- utils/starknetConnectorsWrapper.ts | 49 +++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/utils/starknetConnectorsWrapper.ts b/utils/starknetConnectorsWrapper.ts index 6b36473..d9b481f 100644 --- a/utils/starknetConnectorsWrapper.ts +++ b/utils/starknetConnectorsWrapper.ts @@ -29,15 +29,54 @@ export const getConnectors = (): any[] => { return connectors; }; +const isPartner = (connector: Connector) => { + return connector.id === "argentX" || connector.id === "braavos"; +}; + +// Helper function to randomize the order of an array +const shuffleArray = (array: T[]): T[] => { + const shuffled = [...array]; + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + return shuffled; +}; + export const sortConnectors = (connectors: Connector[]) => { - const available: Connector[] = []; - const notAvailable: Connector[] = []; + const available: { partners: Connector[]; others: Connector[] } = { + partners: [], + others: [], + }; + const notAvailable: { partners: Connector[]; others: Connector[] } = { + partners: [], + others: [], + }; + const mobileConnectors: Connector[] = []; connectors.forEach((connector) => { - if (connector.available()) available.push(connector); - else notAvailable.push(connector); + if (connector.id === "argentWebWallet" || connector.id === "argentMobile") + mobileConnectors.push(connector); + else if (connector.available()) { + if (isPartner(connector)) available.partners.push(connector); + else available.others.push(connector); + } else { + if (isPartner(connector)) notAvailable.partners.push(connector); + else notAvailable.others.push(connector); + } }); - return available.concat(notAvailable); + const availablePartnersRand = shuffleArray(available.partners); + const notAvailablePartnersRand = shuffleArray(notAvailable.partners); + + const sortedConnectors: Connector[] = [ + ...availablePartnersRand, // Randomized order of installed partners wallets + ...available.others, // non partner wallets installed (i.e Bitget) + ...notAvailablePartnersRand, // Randomized order of non installed partners wallets + ...mobileConnectors, // Argent mobile and web wallet + ...notAvailable.others, // other non installed wallets (i.e Bitget) + ]; + + return sortedConnectors; }; export const getConnectorIcon = (id: string) => {