11import { useState } from "react"
2+ import { shuffle } from "lodash"
23import { useStaticQuery , graphql } from "gatsby"
34import { IGatsbyImageData } from "gatsby-plugin-image"
45import { useI18next , useTranslation } from "gatsby-plugin-react-i18next"
@@ -492,16 +493,15 @@ export const useEthExchanges = () => {
492493 }
493494
494495 // Add `value` & `label` for Select component
495- const exchangesByCountry : Array < ExchangeByCountry > =
496- data . exchangesByCountry . nodes
497- . map ( ( node ) => {
498- return {
499- value : node . country ,
500- label : node . country ,
501- exchanges : node ,
502- }
503- } )
504- . sort ( ( ) => Math . random ( ) - 0.5 )
496+ const exchangesByCountry : Array < ExchangeByCountry > = shuffle (
497+ data . exchangesByCountry . nodes . map ( ( node ) => {
498+ return {
499+ value : node . country ,
500+ label : node . country ,
501+ exchanges : node ,
502+ }
503+ } )
504+ )
505505
506506 const exchangesArray = Object . keys ( exchanges ) as Array < ExchangeName >
507507 const walletProvidersArray = Object . keys (
@@ -516,31 +516,34 @@ export const useEthExchanges = () => {
516516 const hasSelectedCountry = ! ! state ?. selectedCountry ?. value
517517 if ( hasSelectedCountry ) {
518518 // 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 ( ", " ) } `
519+ filteredExchanges = shuffle (
520+ exchangesArray
521+ . filter (
522+ ( exchange ) => state ?. selectedCountry ?. exchanges [ exchange ] === "TRUE"
523+ )
524+ // Format array for <CardList/>
525+ . map ( ( exchange ) => {
526+ // Add state exceptions if Country is USA
527+ let description : string | null = null
528+ if (
529+ state ?. selectedCountry ?. value === t ( "page-get-eth-exchanges-usa" )
530+ ) {
531+ const exceptions = exchanges [ exchange ] . usaExceptions
532+ if ( exceptions . length > 0 ) {
533+ description = `${ t (
534+ "page-get-eth-exchanges-except"
535+ ) } ${ exceptions . join ( ", " ) } `
536+ }
533537 }
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 ( ( ) => Math . random ( ) - 0.5 )
538+ return {
539+ title : exchanges [ exchange ] . name ,
540+ description,
541+ link : exchanges [ exchange ] . url ,
542+ image : getImage ( exchanges [ exchange ] . image ) ! ,
543+ alt : "" ,
544+ }
545+ } )
546+ )
544547
545548 // Filter to wallet providers that serve selected Country
546549 filteredWalletProviders = walletProvidersArray . filter (
@@ -549,43 +552,48 @@ export const useEthExchanges = () => {
549552 }
550553 if ( filteredWalletProviders . length ) {
551554 // Construct wallets based on the provider
552- filteredWallets = filteredWalletProviders
553- . reduce < Array < FilteredData > > ( ( res , currentProvider ) => {
554- const wallets = Object . keys ( walletProviders [ currentProvider ] . wallets )
555+ filteredWallets = shuffle (
556+ filteredWalletProviders . reduce < Array < FilteredData > > (
557+ ( res , currentProvider ) => {
558+ const wallets = Object . keys ( walletProviders [ currentProvider ] . wallets )
555559
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 ( ", " ) } `
560+ const flattenWallets = wallets . reduce < Array < FilteredData > > (
561+ ( result , currentWallet ) => {
562+ const walletObject =
563+ walletProviders [ currentProvider ] . wallets [ currentWallet ]
564+ // Add state exceptions if Country is USA
565+ let description : string | null = null
566+ if (
567+ state ?. selectedCountry ?. value ===
568+ t ( "page-get-eth-exchanges-usa" )
569+ ) {
570+ const exceptions =
571+ walletProviders [ currentProvider ] . usaExceptions
572+ if ( exceptions . length > 0 ) {
573+ description = `${ t (
574+ "page-get-eth-exchanges-except"
575+ ) } ${ exceptions . join ( ", " ) } `
576+ }
577+ // Filter out wallets that only service USA
578+ } else if ( walletObject . isUsaOnly ) {
579+ return result
570580 }
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 ( ( ) => Math . random ( ) - 0.5 )
581+ return result . concat ( {
582+ title : currentWallet ,
583+ description,
584+ link : walletObject . url ,
585+ image : getImage ( walletObject . image ) ! ,
586+ alt : "" ,
587+ } )
588+ } ,
589+ [ ]
590+ )
591+ // Flatten data into single array for <CardList/>
592+ return res . concat ( flattenWallets )
593+ } ,
594+ [ ]
595+ )
596+ )
589597 }
590598
591599 const hasExchangeResults = filteredExchanges . length > 0
0 commit comments