Use common sort

This commit is contained in:
ekzyis 2024-07-08 11:34:00 +02:00
parent 1a60f13d72
commit 7402885998
2 changed files with 27 additions and 19 deletions

View File

@ -251,12 +251,35 @@ export function getEnabledWallet (me) {
return WALLET_DEFS return WALLET_DEFS
.filter(def => !!def.sendPayment) .filter(def => !!def.sendPayment)
.map(def => { .map(def => {
// populate definition with properties from useWallet that are required for sorting
const key = getStorageKey(def.name, me) const key = getStorageKey(def.name, me)
const config = SSR ? null : JSON.parse(window?.localStorage.getItem(key)) const config = SSR ? null : JSON.parse(window?.localStorage.getItem(key))
return { def, config } const priority = config?.priority
return { ...def, config, priority }
}) })
.filter(({ config }) => config?.enabled) .filter(({ config }) => config?.enabled)
.sort((w1, w2) => w1.config.priority - w2.config.priority)[0]?.def .sort(walletPrioritySort)[0]
}
export function walletPrioritySort (w1, w2) {
const delta = w1.priority - w2.priority
// delta is NaN if either priority is undefined
if (!Number.isNaN(delta) && delta !== 0) return delta
// if one wallet has a priority but the other one doesn't, the one with the priority comes first
if (w1.priority !== undefined && w2.priority === undefined) return -1
if (w1.priority === undefined && w2.priority !== undefined) return 1
// both wallets have no priority set, falling back to other methods
// if both wallets have an id, use that as tie breaker
// since that's the order in which autowithdrawals are attempted
if (w1.id && w2.id) return Number(w1.id) - Number(w2.id)
console.log('w1', w1, 'w2', w2)
// else we will use the card title as tie breaker
return w1.card.title < w2.card.title ? -1 : 1
} }
export function useWallets () { export function useWallets () {

View File

@ -2,7 +2,7 @@ import { getGetServerSideProps } from '@/api/ssrApollo'
import Layout from '@/components/layout' import Layout from '@/components/layout'
import styles from '@/styles/wallet.module.css' import styles from '@/styles/wallet.module.css'
import Link from 'next/link' import Link from 'next/link'
import { useWallets } from '@/components/wallet' import { useWallets, walletPrioritySort } from '@/components/wallet'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
@ -91,22 +91,7 @@ export default function Wallet ({ ssrData }) {
return 1 return 1
} }
const delta = w1.priority - w2.priority return walletPrioritySort(w1, w2)
// delta is NaN if either priority is undefined
if (!Number.isNaN(delta) && delta !== 0) return delta
// if one wallet has a priority but the other one doesn't, the one with the priority comes first
if (w1.priority !== undefined && w2.priority === undefined) return -1
if (w1.priority === undefined && w2.priority !== undefined) return 1
// both wallets have no priority set, falling back to other methods
// if both wallets have an id, use that as tie breaker
// since that's the order in which autowithdrawals are attempted
if (w1.id && w2.id) return Number(w1.id) - Number(w2.id)
// else we will use the card title as tie breaker
return w1.card.title < w2.card.title ? -1 : 1
}) })
.map((w, i) => { .map((w, i) => {
const draggable = mounted && w.enabled const draggable = mounted && w.enabled