Skip to content

Commit 0e86fd7

Browse files
authored
Merge pull request #10520 from ethereum/randomize-cex-list
Randomize exchange and wallet listing order on /get-eth
2 parents 4847fd1 + 22c5d89 commit 0e86fd7

File tree

1 file changed

+68
-59
lines changed

1 file changed

+68
-59
lines changed

src/components/EthExchanges/use-eth-exchanges.ts

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useState } from "react"
2+
import { shuffle } from "lodash"
23
import { useStaticQuery, graphql } from "gatsby"
34
import { IGatsbyImageData } from "gatsby-plugin-image"
45
import { useI18next, useTranslation } from "gatsby-plugin-react-i18next"
@@ -516,31 +517,34 @@ export const useEthExchanges = () => {
516517
const hasSelectedCountry = !!state?.selectedCountry?.value
517518
if (hasSelectedCountry) {
518519
// Filter to exchanges that serve selected Country
519-
filteredExchanges = exchangesArray
520-
.filter(
521-
(exchange) => state?.selectedCountry?.exchanges[exchange] === "TRUE"
522-
)
523-
// Format array for <CardList/>
524-
.map((exchange) => {
525-
// Add state exceptions if Country is USA
526-
let description: string | null = null
527-
if (state?.selectedCountry?.value === t("page-get-eth-exchanges-usa")) {
528-
const exceptions = exchanges[exchange].usaExceptions
529-
if (exceptions.length > 0) {
530-
description = `${t(
531-
"page-get-eth-exchanges-except"
532-
)} ${exceptions.join(", ")}`
520+
filteredExchanges = shuffle(
521+
exchangesArray
522+
.filter(
523+
(exchange) => state?.selectedCountry?.exchanges[exchange] === "TRUE"
524+
)
525+
// Format array for <CardList/>
526+
.map((exchange) => {
527+
// Add state exceptions if Country is USA
528+
let description: string | null = null
529+
if (
530+
state?.selectedCountry?.value === t("page-get-eth-exchanges-usa")
531+
) {
532+
const exceptions = exchanges[exchange].usaExceptions
533+
if (exceptions.length > 0) {
534+
description = `${t(
535+
"page-get-eth-exchanges-except"
536+
)} ${exceptions.join(", ")}`
537+
}
533538
}
534-
}
535-
return {
536-
title: exchanges[exchange].name,
537-
description,
538-
link: exchanges[exchange].url,
539-
image: getImage(exchanges[exchange].image)!,
540-
alt: "",
541-
}
542-
})
543-
.sort((a, b) => a.title.localeCompare(b.title))
539+
return {
540+
title: exchanges[exchange].name,
541+
description,
542+
link: exchanges[exchange].url,
543+
image: getImage(exchanges[exchange].image)!,
544+
alt: "",
545+
}
546+
})
547+
)
544548

545549
// Filter to wallet providers that serve selected Country
546550
filteredWalletProviders = walletProvidersArray.filter(
@@ -549,43 +553,48 @@ export const useEthExchanges = () => {
549553
}
550554
if (filteredWalletProviders.length) {
551555
// Construct wallets based on the provider
552-
filteredWallets = filteredWalletProviders
553-
.reduce<Array<FilteredData>>((res, currentProvider) => {
554-
const wallets = Object.keys(walletProviders[currentProvider].wallets)
556+
filteredWallets = shuffle(
557+
filteredWalletProviders.reduce<Array<FilteredData>>(
558+
(res, currentProvider) => {
559+
const wallets = Object.keys(walletProviders[currentProvider].wallets)
555560

556-
const flattenWallets = wallets.reduce<Array<FilteredData>>(
557-
(result, currentWallet) => {
558-
const walletObject =
559-
walletProviders[currentProvider].wallets[currentWallet]
560-
// Add state exceptions if Country is USA
561-
let description: string | null = null
562-
if (
563-
state?.selectedCountry?.value === t("page-get-eth-exchanges-usa")
564-
) {
565-
const exceptions = walletProviders[currentProvider].usaExceptions
566-
if (exceptions.length > 0) {
567-
description = `${t(
568-
"page-get-eth-exchanges-except"
569-
)} ${exceptions.join(", ")}`
561+
const flattenWallets = wallets.reduce<Array<FilteredData>>(
562+
(result, currentWallet) => {
563+
const walletObject =
564+
walletProviders[currentProvider].wallets[currentWallet]
565+
// Add state exceptions if Country is USA
566+
let description: string | null = null
567+
if (
568+
state?.selectedCountry?.value ===
569+
t("page-get-eth-exchanges-usa")
570+
) {
571+
const exceptions =
572+
walletProviders[currentProvider].usaExceptions
573+
if (exceptions.length > 0) {
574+
description = `${t(
575+
"page-get-eth-exchanges-except"
576+
)} ${exceptions.join(", ")}`
577+
}
578+
// Filter out wallets that only service USA
579+
} else if (walletObject.isUsaOnly) {
580+
return result
570581
}
571-
// Filter out wallets that only service USA
572-
} else if (walletObject.isUsaOnly) {
573-
return result
574-
}
575-
return result.concat({
576-
title: currentWallet,
577-
description,
578-
link: walletObject.url,
579-
image: getImage(walletObject.image)!,
580-
alt: "",
581-
})
582-
},
583-
[]
584-
)
585-
// Flatten data into single array for <CardList/>
586-
return res.concat(flattenWallets)
587-
}, [])
588-
.sort((a, b) => a.title.localeCompare(b.title))
582+
return result.concat({
583+
title: currentWallet,
584+
description,
585+
link: walletObject.url,
586+
image: getImage(walletObject.image)!,
587+
alt: "",
588+
})
589+
},
590+
[]
591+
)
592+
// Flatten data into single array for <CardList/>
593+
return res.concat(flattenWallets)
594+
},
595+
[]
596+
)
597+
)
589598
}
590599

591600
const hasExchangeResults = filteredExchanges.length > 0

0 commit comments

Comments
 (0)