Skip to content

Commit d70c495

Browse files
authored
Merge pull request #13066 from ethereum/performance/find-wallets
Performance: fix hydration issues on find wallets page
2 parents b8bbaa1 + 38458dc commit d70c495

File tree

7 files changed

+78
-101
lines changed

7 files changed

+78
-101
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: 18 additions & 41 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 {
@@ -13,15 +12,11 @@ import {
1312
SimpleGrid,
1413
SimpleGridProps,
1514
Stack,
16-
Table,
1715
TableProps,
18-
Td,
1916
Text,
20-
Th,
21-
Tr,
2217
} from "@chakra-ui/react"
2318

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

2621
import { ButtonLink } from "@/components/Buttons"
2722
import { WalletMoreInfo } from "@/components/FindWallet/WalletTable/WalletMoreInfo"
@@ -31,8 +26,7 @@ import Tag from "@/components/Tag"
3126

3227
import { trackCustomEvent } from "@/lib/utils/matomo"
3328
import {
34-
formatSupportedLanguages,
35-
getSupportedLanguages,
29+
formatStringList,
3630
getWalletPersonas,
3731
walletsListingCount,
3832
} from "@/lib/utils/wallets"
@@ -50,7 +44,7 @@ import { WalletSupportedLanguageContext } from "@/contexts/WalletSupportedLangua
5044
import { useWalletTable } from "@/hooks/useWalletTable"
5145

5246
const Container = (props: TableProps) => (
53-
<Table
47+
<Box
5448
w="full"
5549
sx={{
5650
th: {
@@ -75,7 +69,6 @@ const WalletContainer = (props: ChildOnlyProp & ContainerProps) => (
7569

7670
const Grid = forwardRef<SimpleGridProps, "tr">((props, ref) => (
7771
<SimpleGrid
78-
as={Tr}
7972
ref={ref}
8073
templateColumns={{
8174
base: "60% auto 0% 0% 5%",
@@ -123,7 +116,7 @@ const WalletContentHeader = (props: ChildOnlyProp) => (
123116
/>
124117
)
125118

126-
const Wallet = forwardRef<ChildOnlyProp, "tr">((props, ref) => (
119+
const WalletRow = forwardRef<ChildOnlyProp, "tr">((props, ref) => (
127120
<Grid
128121
tabIndex={0}
129122
ref={ref}
@@ -207,7 +200,7 @@ export type WalletTableProps = {
207200
filters: WalletFilter
208201
resetFilters: () => void
209202
resetWalletFilter: React.MutableRefObject<() => void>
210-
walletData: WalletData[]
203+
walletData: Wallet[]
211204
onOpen: () => void
212205
}
213206

@@ -219,7 +212,6 @@ const WalletTable = ({
219212
onOpen,
220213
}: WalletTableProps) => {
221214
const { t } = useTranslation("page-wallets-find-wallet")
222-
const { locale } = useRouter()
223215
const {
224216
featureDropdownItems,
225217
filteredWallets,
@@ -243,7 +235,7 @@ const WalletTable = ({
243235
return (
244236
<Container>
245237
<WalletContentHeader>
246-
<Th sx={{ textAlign: "start !important" }}>
238+
<Box sx={{ textAlign: "start !important" }}>
247239
<Flex justifyContent="space-between" px={{ base: 2.5, md: 0 }}>
248240
{/* Displayed on mobile only */}
249241
<Text
@@ -277,7 +269,7 @@ const WalletTable = ({
277269
</Text>
278270
)}
279271
</Flex>
280-
</Th>
272+
</Box>
281273
</WalletContentHeader>
282274
{filteredWallets.length === 0 && (
283275
<WalletEmptyState
@@ -304,21 +296,6 @@ const WalletTable = ({
304296
const hasDeviceLabels = deviceLabels.length > 0
305297
const hasAllLabels = hasPersonasLabels && hasDeviceLabels
306298

307-
// Supported languages
308-
const supportedLanguages = getSupportedLanguages(
309-
wallet.languages_supported,
310-
locale!
311-
)
312-
const numberOfSupportedLanguages = supportedLanguages.length
313-
const sliceSize = NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN
314-
const rest = numberOfSupportedLanguages - sliceSize
315-
const restText = `${rest > 0 ? "+" : ""} ${rest > 0 ? rest : ""}`
316-
317-
const formattedSupportedLanguages = formatSupportedLanguages(
318-
supportedLanguages,
319-
sliceSize
320-
)
321-
322299
const showMoreInfo = (wallet) => {
323300
updateMoreInfo(wallet.key)
324301
// Log "more info" event only on expanding
@@ -335,7 +312,7 @@ const WalletTable = ({
335312
key={wallet.key}
336313
bg={wallet.moreInfo ? "background.highlight" : "transparent"}
337314
>
338-
<Wallet
315+
<WalletRow
339316
// Make wallets more info section open on 'Enter'
340317
onKeyUp={(e) => {
341318
if (e.key === "Enter") showMoreInfo(wallet)
@@ -346,7 +323,7 @@ const WalletTable = ({
346323
showMoreInfo(wallet)
347324
}}
348325
>
349-
<Td lineHeight="revert">
326+
<Box lineHeight="revert">
350327
<Flex
351328
justifyContent="space-between"
352329
alignItems="center"
@@ -419,13 +396,13 @@ const WalletTable = ({
419396
fontWeight="normal !important"
420397
>
421398
{/* Show up to 5 supported languages and use a tooltip for the rest */}
422-
{`${formattedSupportedLanguages}`}{" "}
423-
{rest > 0 && (
424-
<SupportedLanguagesTooltip
425-
supportedLanguages={supportedLanguages}
426-
restText={restText}
427-
/>
428-
)}
399+
{`${formatStringList(
400+
wallet.supportedLanguages,
401+
NUMBER_OF_SUPPORTED_LANGUAGES_SHOWN
402+
)}`}{" "}
403+
<SupportedLanguagesTooltip
404+
supportedLanguages={wallet.supportedLanguages}
405+
/>
429406
</Text>
430407
</Flex>
431408
</Stack>
@@ -478,8 +455,8 @@ const WalletTable = ({
478455
{t("page-find-wallet-visit-website")}
479456
</ButtonLink>
480457
</Box>
481-
</Td>
482-
</Wallet>
458+
</Box>
459+
</WalletRow>
483460

484461
{wallet.moreInfo && (
485462
<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
@@ -622,6 +622,10 @@ export interface WalletData {
622622
new_to_crypto?: boolean
623623
}
624624

625+
export type Wallet = WalletData & {
626+
supportedLanguages: string[]
627+
}
628+
625629
export type WalletFilter = typeof WALLETS_FILTERS_DEFAULT
626630

627631
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

0 commit comments

Comments
 (0)