Skip to content

Commit 8d3504f

Browse files
committed
format the language support for each wallet at build time
1 parent a590eab commit 8d3504f

File tree

7 files changed

+59
-69
lines changed

7 files changed

+59
-69
lines changed

src/components/FindWallet/MobileFiltersMenu.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
Flex,
1111
} from "@chakra-ui/react"
1212

13-
import { Button } from "@/components/Buttons"
13+
import type { Wallet } from "@/lib/types"
1414

15-
import walletData from "@/data/wallets/wallet-data"
15+
import { Button } from "@/components/Buttons"
1616

1717
import OldHeading from "../OldHeading"
1818

@@ -26,12 +26,14 @@ import WalletFilterSidebar, {
2626
import { useWalletTable } from "@/hooks/useWalletTable"
2727

2828
type MobileFiltersMenuProps = WalletFilterSidebarProps & {
29+
walletData: Wallet[]
2930
showMobileSidebar: boolean
3031
onOpen: () => void
3132
onClose: () => void
3233
}
3334

3435
export const MobileFiltersMenu = ({
36+
walletData,
3537
filters,
3638
resetWalletFilter,
3739
resetFilters,

src/components/FindWallet/WalletTable/SupportedLanguagesTooltip.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import { Box, Text } from "@chakra-ui/react"
1+
import { Text } from "@chakra-ui/react"
22

33
import Tooltip from "@/components/Tooltip"
44

5-
import { formatSupportedLanguages } from "@/lib/utils/wallets"
5+
import { formatStringList } from "@/lib/utils/wallets"
66

77
import { NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN } from "@/lib/constants"
88

99
type SupportedLanguagesTooltipProps = {
1010
supportedLanguages: string[]
11-
restText: string
1211
}
1312

1413
// Tooltip to show other supported languages on a wallet
1514
export const SupportedLanguagesTooltip = ({
1615
supportedLanguages,
17-
restText,
1816
}: SupportedLanguagesTooltipProps) => {
19-
const tooltipContent = formatSupportedLanguages(
17+
const numberOfSupportedLanguages = supportedLanguages.length
18+
const rest = numberOfSupportedLanguages - NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN
19+
20+
if (rest <= 0) {
21+
return null
22+
}
23+
24+
const tooltipContent = formatStringList(
2025
supportedLanguages.slice(NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN)
2126
)
2227

@@ -28,7 +33,7 @@ export const SupportedLanguagesTooltip = ({
2833
fontSize="md !important"
2934
fontWeight="normal !important"
3035
>
31-
{restText}
36+
+ {rest}
3237
</Text>
3338
</Tooltip>
3439
)

src/components/FindWallet/WalletTable/index.tsx

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ReactNode, useContext } from "react"
2-
import { useRouter } from "next/router"
32
import { useTranslation } from "next-i18next"
43
import { MdExpandLess, MdExpandMore } from "react-icons/md"
54
import {
@@ -17,7 +16,7 @@ import {
1716
Text,
1817
} from "@chakra-ui/react"
1918

20-
import { ChildOnlyProp, WalletData, WalletFilter } from "@/lib/types"
19+
import type { ChildOnlyProp, Wallet, WalletFilter } from "@/lib/types"
2120

2221
import { ButtonLink } from "@/components/Buttons"
2322
import { WalletMoreInfo } from "@/components/FindWallet/WalletTable/WalletMoreInfo"
@@ -27,8 +26,7 @@ import Tag from "@/components/Tag"
2726

2827
import { trackCustomEvent } from "@/lib/utils/matomo"
2928
import {
30-
formatSupportedLanguages,
31-
getSupportedLanguages,
29+
formatStringList,
3230
getWalletPersonas,
3331
walletsListingCount,
3432
} from "@/lib/utils/wallets"
@@ -118,7 +116,7 @@ const WalletContentHeader = (props: ChildOnlyProp) => (
118116
/>
119117
)
120118

121-
const Wallet = forwardRef<ChildOnlyProp, "tr">((props, ref) => (
119+
const WalletRow = forwardRef<ChildOnlyProp, "tr">((props, ref) => (
122120
<Grid
123121
tabIndex={0}
124122
ref={ref}
@@ -202,7 +200,7 @@ export type WalletTableProps = {
202200
filters: WalletFilter
203201
resetFilters: () => void
204202
resetWalletFilter: React.MutableRefObject<() => void>
205-
walletData: WalletData[]
203+
walletData: Wallet[]
206204
onOpen: () => void
207205
}
208206

@@ -214,7 +212,6 @@ const WalletTable = ({
214212
onOpen,
215213
}: WalletTableProps) => {
216214
const { t } = useTranslation("page-wallets-find-wallet")
217-
const { locale } = useRouter()
218215
const {
219216
featureDropdownItems,
220217
filteredWallets,
@@ -299,21 +296,6 @@ const WalletTable = ({
299296
const hasDeviceLabels = deviceLabels.length > 0
300297
const hasAllLabels = hasPersonasLabels && hasDeviceLabels
301298

302-
// Supported languages
303-
const supportedLanguages = getSupportedLanguages(
304-
wallet.languages_supported,
305-
locale!
306-
)
307-
const numberOfSupportedLanguages = supportedLanguages.length
308-
const sliceSize = NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN
309-
const rest = numberOfSupportedLanguages - sliceSize
310-
const restText = `${rest > 0 ? "+" : ""} ${rest > 0 ? rest : ""}`
311-
312-
const formattedSupportedLanguages = formatSupportedLanguages(
313-
supportedLanguages,
314-
sliceSize
315-
)
316-
317299
const showMoreInfo = (wallet) => {
318300
updateMoreInfo(wallet.key)
319301
// Log "more info" event only on expanding
@@ -330,7 +312,7 @@ const WalletTable = ({
330312
key={wallet.key}
331313
bg={wallet.moreInfo ? "background.highlight" : "transparent"}
332314
>
333-
<Wallet
315+
<WalletRow
334316
// Make wallets more info section open on 'Enter'
335317
onKeyUp={(e) => {
336318
if (e.key === "Enter") showMoreInfo(wallet)
@@ -414,13 +396,13 @@ const WalletTable = ({
414396
fontWeight="normal !important"
415397
>
416398
{/* Show up to 5 supported languages and use a tooltip for the rest */}
417-
{`${formattedSupportedLanguages}`}{" "}
418-
{rest > 0 && (
419-
<SupportedLanguagesTooltip
420-
supportedLanguages={supportedLanguages}
421-
restText={restText}
422-
/>
423-
)}
399+
{`${formatStringList(
400+
wallet.supportedLanguages,
401+
NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN
402+
)}`}{" "}
403+
<SupportedLanguagesTooltip
404+
supportedLanguages={wallet.supportedLanguages}
405+
/>
424406
</Text>
425407
</Flex>
426408
</Stack>
@@ -474,7 +456,7 @@ const WalletTable = ({
474456
</ButtonLink>
475457
</Box>
476458
</Box>
477-
</Wallet>
459+
</WalletRow>
478460

479461
{wallet.moreInfo && (
480462
<WalletMoreInfo

src/hooks/useWalletTable.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useContext, useEffect, useState } from "react"
22

3-
import { DropdownOption } from "@/lib/types"
4-
5-
import { WalletTableProps } from "@/components/FindWallet/WalletTable"
3+
import type { DropdownOption, Wallet, WalletFilter } from "@/lib/types"
64

75
import { WalletSupportedLanguageContext } from "@/contexts/WalletSupportedLanguageContext"
86

9-
type UseWalletTableProps = Pick<WalletTableProps, "filters" | "walletData"> & {
7+
type UseWalletTableProps = {
8+
walletData: Wallet[]
9+
filters: WalletFilter
1010
t: (x: string) => string
1111
}
1212

src/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,10 @@ export interface WalletData {
581581
new_to_crypto?: boolean
582582
}
583583

584+
export interface Wallet extends WalletData {
585+
supportedLanguages: string[]
586+
}
587+
584588
export type WalletFilter = typeof WALLETS_FILTERS_DEFAULT
585589

586590
export interface WalletFilterData {

src/lib/utils/wallets.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
NEW_TO_CRYPTO_FEATURES,
1313
NFTS_FEATURES,
1414
} from "../constants"
15-
import { WalletData, WalletFilter } from "../types"
15+
import type { WalletData, WalletFilter } from "../types"
1616

1717
export const getSupportedLocaleWallets = (locale: string) =>
1818
shuffle(
@@ -65,21 +65,21 @@ export const getWalletPersonas = (wallet: WalletData) => {
6565

6666
// Get a list of wallet supported languages with native title
6767
export const getSupportedLanguages = (
68-
walletSupportedLanguages: string[],
68+
supportedLanguageCodes: string[],
6969
locale: string
7070
) => {
7171
const supportedLanguages = [] as string[]
7272

7373
// current locale should appear first on the list, this manipulates the array to move it to the top if needed
7474
const supportsCurrentLocale = (current) => current === locale
75-
const localeIndex = walletSupportedLanguages.findIndex(supportsCurrentLocale)
75+
const localeIndex = supportedLanguageCodes.findIndex(supportsCurrentLocale)
7676

7777
if (localeIndex >= 0) {
78-
walletSupportedLanguages.splice(localeIndex, 1)
79-
walletSupportedLanguages.unshift(locale)
78+
supportedLanguageCodes.splice(localeIndex, 1)
79+
supportedLanguageCodes.unshift(locale)
8080
}
8181

82-
walletSupportedLanguages.forEach((supportedLanguage) => {
82+
supportedLanguageCodes.forEach((supportedLanguage) => {
8383
// Get supported language name
8484
const supportedLanguageName = getLanguageCodeName(supportedLanguage, locale)
8585
// Capitalize supported language name
@@ -90,13 +90,8 @@ export const getSupportedLanguages = (
9090
}
9191

9292
// Format languages list to be displayed on UI label
93-
export const formatSupportedLanguages = (
94-
supportedLanguages: string[],
95-
sliceSize?: number
96-
) => {
97-
return sliceSize
98-
? supportedLanguages.slice(0, sliceSize).join(", ")
99-
: supportedLanguages.join(", ")
93+
export const formatStringList = (strings: string[], sliceSize?: number) => {
94+
return sliceSize ? strings.slice(0, sliceSize).join(", ") : strings.join(", ")
10095
}
10196

10297
// Get border custom color for Persona filter

src/pages/wallets/find-wallet.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { useEffect, useRef, useState } from "react"
1+
import { useRef, useState } from "react"
22
import { GetStaticProps, InferGetStaticPropsType } from "next"
33
import { useRouter } from "next/router"
44
import { useTranslation } from "next-i18next"
55
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
6-
import {
7-
Box,
8-
calc,
9-
Center,
10-
Flex,
11-
Text,
12-
useDisclosure,
13-
useTheme,
14-
} from "@chakra-ui/react"
6+
import { Box, calc, Center, Flex, Text, useDisclosure } from "@chakra-ui/react"
157

16-
import { BasePageProps, ChildOnlyProp, WalletData } from "@/lib/types"
8+
import type { BasePageProps, ChildOnlyProp, Wallet } from "@/lib/types"
179

1810
import BannerNotification from "@/components/BannerNotification"
1911
import Breadcrumbs from "@/components/Breadcrumbs"
@@ -32,6 +24,7 @@ import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
3224
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
3325
import {
3426
getNonSupportedLocaleWallets,
27+
getSupportedLanguages,
3528
getSupportedLocaleWallets,
3629
} from "@/lib/utils/wallets"
3730

@@ -57,7 +50,7 @@ const Subtitle = ({ children }: ChildOnlyProp) => (
5750
)
5851

5952
type Props = BasePageProps & {
60-
wallets: WalletData[]
53+
wallets: Wallet[]
6154
}
6255

6356
export const getStaticProps = (async ({ locale }) => {
@@ -71,7 +64,15 @@ export const getStaticProps = (async ({ locale }) => {
7164

7265
const supportedLocaleWallets = getSupportedLocaleWallets(locale!)
7366
const noSupportedLocaleWallets = getNonSupportedLocaleWallets(locale!)
74-
const wallets = supportedLocaleWallets.concat(noSupportedLocaleWallets)
67+
const walletsData = supportedLocaleWallets.concat(noSupportedLocaleWallets)
68+
69+
const wallets = walletsData.map((wallet) => ({
70+
...wallet,
71+
supportedLanguages: getSupportedLanguages(
72+
wallet.languages_supported,
73+
locale!
74+
),
75+
}))
7576

7677
return {
7778
props: {
@@ -196,6 +197,7 @@ const FindWalletPage = ({
196197
{/* Mobile filters menu */}
197198
<Box hideFrom="lg">
198199
<MobileFiltersMenu
200+
walletData={wallets}
199201
filters={filters}
200202
resetWalletFilter={resetWalletFilter}
201203
updateFilterOption={updateFilterOption}

0 commit comments

Comments
 (0)