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 1 commit
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/sorts";

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;
24 changes: 24 additions & 0 deletions utils/sorts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Connector } from "starknetkit";
Copy link
Contributor

Choose a reason for hiding this comment

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

rename the file connectors.ts, we'll reuse it later for connectors utils


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);
}
});
fricoben marked this conversation as resolved.
Show resolved Hide resolved

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);
}),
];
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve sorting configuration and handle unknown connectors

The current implementation has two potential issues:

  1. The order array is hardcoded and not configurable
  2. Connectors not in the order array will be sorted incorrectly (both will return -1 from indexOf)

Consider this improved implementation:

+const DEFAULT_ORDER = ["okxwallet", "bitkeep"];
+
 export const sortConnectors = (connectors: Connector[]): Connector[] => {
   const available: Connector[] = [];
   const notAvailable: Connector[] = [];
+  const order = DEFAULT_ORDER;

   // Rest of the code...

   return [
     ...available,
     ...notAvailable.sort((a, b) => {
-      const order = ["okxwallet", "bitkeep"]; // desired order
-      return order.indexOf(a.id) - order.indexOf(b.id);
+      const indexA = order.indexOf(a.id);
+      const indexB = order.indexOf(b.id);
+      // If neither connector is in the order array, maintain their relative position
+      if (indexA === -1 && indexB === -1) return 0;
+      // If only one connector is in the order array, prioritize it
+      if (indexA === -1) return 1;
+      if (indexB === -1) return -1;
+      // Both connectors are in the order array, sort by their index
+      return indexA - indexB;
     }),
   ];
 };
📝 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 DEFAULT_ORDER = ["okxwallet", "bitkeep"];
return [
...available,
...notAvailable.sort((a, b) => {
const indexA = order.indexOf(a.id);
const indexB = order.indexOf(b.id);
// If neither connector is in the order array, maintain their relative position
if (indexA === -1 && indexB === -1) return 0;
// If only one connector is in the order array, prioritize it
if (indexA === -1) return 1;
if (indexB === -1) return -1;
// Both connectors are in the order array, sort by their index
return indexA - indexB;
}),
];
};

Copy link
Contributor

Choose a reason for hiding this comment

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

you need to create a connector.test.js function to test your util and make sur it covers all hedge cases here :)