Skip to content

Commit

Permalink
Merge branch 'develop' into dbrans/petnames-useDisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrans committed Jan 31, 2024
2 parents 480bd4c + 3fe1f55 commit a6c7e8c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
49 changes: 38 additions & 11 deletions app/scripts/controllers/network-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ const controllerName = 'NetworkOrderController';
/**
* The network ID of a network.
*/
export type NetworkId = string;

export type NetworksInfo = {
networkId: string;
networkRpcUrl: string;
};

// State shape for NetworkOrderController
export type NetworkOrderControllerState = {
orderedNetworkList: NetworkId[];
orderedNetworkList: NetworksInfo[];
};

// Describes the structure of a state change event
Expand Down Expand Up @@ -48,7 +52,7 @@ export type NetworkOrderControllerMessenger = RestrictedControllerMessenger<
>;

// Default state for the controller
const defaultState = {
const defaultState: NetworkOrderControllerState = {
orderedNetworkList: [],
};

Expand Down Expand Up @@ -116,17 +120,40 @@ export class NetworkOrderController extends BaseController<
const combinedNetworks = [...MAINNET_CHAINS, ...networkConfigurations];

// Extract unique chainIds from the combined networks
const uniqueChainIds = combinedNetworks.map((item) => item.chainId);
const uniqueChainIds = combinedNetworks.map((item) => ({
networkId: item.chainId,
networkRpcUrl: item.rpcUrl,
}));

// Arrays to store reordered and new unique chainIds
let reorderedNetworks: NetworksInfo[] = [];
const newUniqueNetworks: NetworksInfo[] = [];

// Iterate through uniqueChainIds to reorder existing elements
uniqueChainIds.forEach((newItem) => {
const existingIndex = this.state.orderedNetworkList.findIndex(
(item) =>
item.networkId === newItem.networkId &&
item.networkRpcUrl === newItem.networkRpcUrl,
);
// eslint-disable-next-line no-negated-condition
if (existingIndex !== -1) {
// Reorder existing element
reorderedNetworks[existingIndex] = newItem;
} else {
// Add new unique element
newUniqueNetworks.push(newItem);
}
});

// Filter out null values and concatenate reordered and new unique networks
reorderedNetworks = reorderedNetworks
.filter((item) => Boolean(item))
.concat(newUniqueNetworks);

// Update the state with the new networks list
this.update((state) => {
// Combine existing networks with unique chainIds, excluding duplicates
state.orderedNetworkList = [
...state.orderedNetworkList,
...uniqueChainIds.filter(
(id) => !state.orderedNetworkList.includes(id),
),
];
state.orderedNetworkList = reorderedNetworks;
});
}

Expand Down
4 changes: 2 additions & 2 deletions shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ export const TEST_CHAINS = [
];

export const MAINNET_CHAINS = [
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.MAINNET, rpcUrl: MAINNET_RPC_URL },
{ chainId: CHAIN_IDS.LINEA_MAINNET, rpcUrl: LINEA_MAINNET_RPC_URL },
];

const typedCapitalize = <K extends string>(k: K): Capitalize<K> =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"networkConfigurations": "object"
},
"NetworkOrderController": {
"orderedNetworkList": { "0": "string", "1": "string", "2": "string" }
"orderedNetworkList": { "0": "object", "1": "object", "2": "object" }
},
"NftController": {
"allNftContracts": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"permissionActivityLog": "object",
"subjectMetadata": "object",
"announcements": "object",
"orderedNetworkList": { "0": "string", "1": "string", "2": "string" },
"orderedNetworkList": { "0": "object", "1": "object", "2": "object" },
"pinnedAccountList": {},
"hiddenAccountList": {},
"gasFeeEstimatesByChainId": {},
Expand Down
38 changes: 27 additions & 11 deletions ui/components/multichain/network-list-menu/network-list-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,25 @@ export const NetworkListMenu = ({ onClose }) => {
return nonTestNetworks;
}

// Reorder nonTestNetworks based on the order of chainIds in orderedNetworksList
const sortedNetworkList = orderedNetworksList
.map((chainId) =>
nonTestNetworks.find((network) => network.chainId === chainId),
)
.filter(Boolean);

return sortedNetworkList;
// Create a mapping of chainId to index in orderedNetworksList
const orderedIndexMap = {};
orderedNetworksList.forEach((network, index) => {
orderedIndexMap[`${network.networkId}_${network.networkRpcUrl}`] = index;
});

// Sort nonTestNetworks based on the order in orderedNetworksList
const sortedNonTestNetworks = nonTestNetworks.sort((a, b) => {
const keyA = `${a.chainId}_${a.rpcUrl}`;
const keyB = `${b.chainId}_${b.rpcUrl}`;
return orderedIndexMap[keyA] - orderedIndexMap[keyB];
});

return sortedNonTestNetworks;
};

const networksList = newOrderNetworks();
const [items, setItems] = useState([...networksList]);

useEffect(() => {
if (currentlyOnTestNetwork) {
dispatch(setShowTestNetworks(currentlyOnTestNetwork));
Expand All @@ -125,13 +132,20 @@ export const NetworkListMenu = ({ onClose }) => {
if (!result.destination) {
return;
}

const newItems = [...items];
const [removed] = newItems.splice(result.source.index, 1);
newItems.splice(result.destination.index, 0, removed);
setItems(newItems);
const orderedArray = newItems.map((obj) => obj.chainId);

// Convert the updated array back to NetworksInfo format
const orderedArray = newItems.map((obj) => ({
networkId: obj.chainId, // Assuming chainId is the networkId
networkRpcUrl: obj.rpcUrl,
}));

dispatch(updateNetworksList(orderedArray));

setItems(newItems);
};

let searchResults =
Expand Down Expand Up @@ -162,7 +176,9 @@ export const NetworkListMenu = ({ onClose }) => {
return null;
}

const isCurrentNetwork = currentNetwork.id === network.id;
const isCurrentNetwork =
currentNetwork.id === network.id &&
currentNetwork.rpcUrl === network.rpcUrl;

const canDeleteNetwork =
isUnlocked && !isCurrentNetwork && network.removable;
Expand Down

0 comments on commit a6c7e8c

Please sign in to comment.