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

Feat: Reorganize Wallet PopUp #945

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions components/UI/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { PendingBoostClaim } from "types/backTypes";
import Typography from "./typography/typography";
import { TEXT_TYPE } from "@constants/typography";
import Hamburger from "./hamburger";
import { sortConnectors } from "@utils/connectors";

const Navbar: FunctionComponent = () => {
const currentNetwork = getCurrentNetwork();
Expand Down Expand Up @@ -53,10 +54,11 @@ const Navbar: FunctionComponent = () => {
linkText: "",
},
]);
const sortedConnectors = sortConnectors(availableConnectors);

const { starknetkitConnectModal } = useStarknetkitConnectModal({
connectors: availableConnectors as any,
connectors: sortedConnectors as any,
});

const fetchAndUpdateNotifications = async () => {
if (!address) return;
const res = await getPendingBoostClaims(hexToDecimal(address));
Expand Down Expand Up @@ -344,4 +346,4 @@ const Navbar: FunctionComponent = () => {
);
};

export default Navbar;
export default Navbar;
58 changes: 58 additions & 0 deletions tests/utils/connectors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { sortConnectors } from "../../utils/connectors";
class MockConnector {
constructor(id, name, isAvailable) {
this.id = id;
this.name = name;
this.isAvailable = isAvailable;
}
available() {
return this.isAvailable;
}
}

describe("sortConnectors function", () => {
it("should place available connectors first, followed by unavailable ones", () => {
const connectors = [
new MockConnector("okxwallet", "Okx Wallet", false),
new MockConnector("braavos", "Braavos", true),
new MockConnector("bitkeep", "Bitget Wallet", false),
new MockConnector("argentX", "Argent X", true),
];

const sortedConnectors = sortConnectors(connectors);

expect(sortedConnectors[0].name).toBe("Braavos");
expect(sortedConnectors[1].name).toBe("Argent X");
expect(sortedConnectors[2].name).toBe("Okx Wallet");
expect(sortedConnectors[3].name).toBe("Bitget Wallet");
});

it("should return an empty array if no connectors are provided", () => {
const sortedConnectors = sortConnectors([]);
expect(sortedConnectors).toEqual([]);
});

it("should handle the case when all connectors are available", () => {
const connectors = [
new MockConnector("braavos", "Braavos", true),
new MockConnector("argentX", "Argent X", true),
];

const sortedConnectors = sortConnectors(connectors);

expect(sortedConnectors[0].name).toBe("Braavos");
expect(sortedConnectors[1].name).toBe("Argent X");
});

it("should handle the case when all connectors are unavailable", () => {
const connectors = [
new MockConnector("okxwallet", "Okx Wallet", false),
new MockConnector("bitkeep", "Bitget Wallet", false),
];

const sortedConnectors = sortConnectors(connectors);

expect(sortedConnectors[0].name).toBe("Okx Wallet");
expect(sortedConnectors[1].name).toBe("Bitget Wallet");
});
});
24 changes: 24 additions & 0 deletions utils/connectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Connector } from "starknetkit";

export const sortConnectors = (connectors: Connector[]): Connector[] => {
const available: Connector[] = [];
const notAvailable: Connector[] = [];

// Sort connectors
connectors.forEach((connector) => {
if (connector.available()) {
available.push(connector);
} else {
notAvailable.push(connector);
}
});

return [
...available,
// Reorganized not available connectors
...notAvailable.sort((a, b) => {
const order = ["okxwallet", "bitkeep"]; // desired order
return order.indexOf(a.id) - order.indexOf(b.id);
}),
];
};
Comment on lines +16 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve robustness of connector ordering logic

The current implementation has several potential issues:

  1. The order array is hardcoded and should be configurable
  2. The sorting logic doesn't handle connectors that aren't in the order array
  3. No type safety for connector IDs

Consider this improved implementation:

+const WALLET_ORDER = ["okxwallet", "bitkeep"] as const;
+type KnownWalletId = typeof WALLET_ORDER[number];
+
 export const sortConnectors = (connectors: Connector[]): Connector[] => {
   // ... previous code ...

   return [
     ...available,
-    // Reorganized not available connectors
     ...notAvailable.sort((a, b) => {
-      const order = ["okxwallet", "bitkeep"]; // desired order
-      return order.indexOf(a.id) - order.indexOf(b.id);
+      const aIndex = WALLET_ORDER.indexOf(a.id as KnownWalletId);
+      const bIndex = WALLET_ORDER.indexOf(b.id as KnownWalletId);
+      
+      // If neither wallet is in the order array, maintain their relative position
+      if (aIndex === -1 && bIndex === -1) return 0;
+      // If only one wallet is in the order array, it should come first
+      if (aIndex === -1) return 1;
+      if (bIndex === -1) return -1;
+      // Both wallets are in the order array, sort by their index
+      return aIndex - bIndex;
     }),
   ];
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return [
...available,
// Reorganized not available connectors
...notAvailable.sort((a, b) => {
const order = ["okxwallet", "bitkeep"]; // desired order
return order.indexOf(a.id) - order.indexOf(b.id);
}),
];
};
const WALLET_ORDER = ["okxwallet", "bitkeep"] as const;
type KnownWalletId = typeof WALLET_ORDER[number];
return [
...available,
...notAvailable.sort((a, b) => {
const aIndex = WALLET_ORDER.indexOf(a.id as KnownWalletId);
const bIndex = WALLET_ORDER.indexOf(b.id as KnownWalletId);
// If neither wallet is in the order array, maintain their relative position
if (aIndex === -1 && bIndex === -1) return 0;
// If only one wallet is in the order array, it should come first
if (aIndex === -1) return 1;
if (bIndex === -1) return -1;
// Both wallets are in the order array, sort by their index
return aIndex - bIndex;
}),
];
};