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

Fix incorrect network order when connected with wc #1576

Merged
merged 10 commits into from
Dec 19, 2023
25 changes: 21 additions & 4 deletions packages/scaffold/src/views/w3m-networks-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ export class W3mNetworksView extends LitElement {
const { approvedCaipNetworkIds, requestedCaipNetworks, supportsAllNetworks } =
NetworkController.state
const approvedIds = approvedCaipNetworkIds
const requested = requestedCaipNetworks
const requestedNetworks = requestedCaipNetworks
const approvedIndexMap: Record<string, number> = {}
if (requestedNetworks && approvedIds) {
approvedIds.forEach((id, index) => {
approvedIndexMap[id] = index
})

if (approvedIds?.length) {
requested?.sort((a, b) => approvedIds.indexOf(b.id) - approvedIds.indexOf(a.id))
requestedNetworks.sort((a, b) => {
const indexA = approvedIndexMap[a.id]
const indexB = approvedIndexMap[b.id]

if (indexA !== undefined && indexB !== undefined) {
return indexA - indexB
} else if (indexA !== undefined) {
return -1
} else if (indexB !== undefined) {
return 1
}

return 0
})
}

return requested?.map(
return requestedNetworks?.map(
network => html`
<wui-card-select
.selected=${this.caipNetwork?.id === network.id}
Expand Down