Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/mobile/src/__integration__/discover-folders.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// folder without ever removing a favorite.

import { beforeEach, describe, expect, it } from 'vitest'
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
import { act, fireEvent, screen, waitFor, within } from '@testing-library/react'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
import { mockDiscoverProjects } from '@perawallet/wallet-core-projects/test-handlers'
import { server } from '@test-utils/msw-server'
Expand Down Expand Up @@ -239,6 +239,32 @@ describe('Flow: Discover favorite folders', () => {
await flush()
})

it('Given a folder with four dApps, then its row stacks three logos and +1', async () => {
const urls = ['Tinyman', 'Folks', 'Pact', 'Vestige'].map(name => {
store().addFavorite(dapp(name))
return `https://${name.toLowerCase()}.example`
})
const id = store().createFolder('DeFi')
urls.forEach(url => store().setDappInFolder(id, url, true))
store().createFolder('Empty')
renderWithNavigation(DiscoverAllFoldersScreen, 'DiscoverAllFolders')

const row = await screen.findByTestId(`discover_all_folders_${id}`)
expect(
within(row).getAllByTestId(/^discover_logo_stack_logo_/),
).toHaveLength(3)
expect(
within(row).getByTestId('discover_logo_stack_more').textContent,
).toBe('+1')
const emptyId = store().folderOrder[0]!
expect(
within(
screen.getByTestId(`discover_all_folders_${emptyId}`),
).queryByTestId('discover_logo_stack'),
).toBeNull()
await flush()
})

it('Given folders, when one is tapped in All folders, then that folder opens', async () => {
store().createFolder('Older')
const id = store().createFolder('Newest')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2022-2026 Pera Wallet, LDA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/

import type { Nullable } from '@perawallet/wallet-core-shared'
import { PWIcon, PWImage, PWText, PWView } from '@components/core'
import { dappLogoStack } from '../../utils/dappLogoStack'
import { useStyles } from './styles'

export type DappLogoStackProps = {
/** The first dApps' logos, in display order. */
logos: Nullable<string>[]
/** Every dApp in the row, for the "+N" chip. */
total: number
}

/** Up to three overlapped dApp logos and a "+N" chip, like `ChainIconStack`. */
export const DappLogoStack = ({ logos, total }: DappLogoStackProps) => {
const styles = useStyles()
const { visible, hiddenCount } = dappLogoStack(logos, total)
if (visible.length === 0) return null

return (
<PWView
style={styles.stack}
testID='discover_logo_stack'
>
{visible.map((logo, index) => (
<PWView
// Logos repeat and can be null; the slot is the identity.
// oxlint-disable-next-line react/no-array-index-key -- fixed slots, never reordered in place
key={index}
style={[styles.slot, index > 0 && styles.stacked]}
testID={`discover_logo_stack_logo_${index}`}
>
{logo ? (
<PWImage
source={{ uri: logo }}
style={styles.logo}
/>
) : (
<PWView style={styles.logo}>
<PWIcon
name='globe'
size='xs'
variant='secondary'
/>
</PWView>
)}
</PWView>
))}
{hiddenCount > 0 ? (
<PWView
style={[styles.slot, styles.stacked]}
testID='discover_logo_stack_more'
>
<PWView style={styles.more}>
<PWText
variant='captionMedium'
style={styles.moreLabel}
>
{`+${hiddenCount}`}
</PWText>
</PWView>
</PWView>
) : null}
</PWView>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright 2022-2026 Pera Wallet, LDA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/

export { DappLogoStack } from './DappLogoStack'
export type { DappLogoStackProps } from './DappLogoStack'
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022-2026 Pera Wallet, LDA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/

import { makeStyles } from '@rneui/themed'

// Matches ChainIconStack's 20px icons and "+N" chip.
// lanekeep-ignore-next-line pera/no-numeric-sizes reason: stacked logo size, as ChainIconStack
export const DAPP_LOGO_STACK_SIZE = 20

export const useStyles = makeStyles(theme => ({
stack: {
flexDirection: 'row',
alignItems: 'center',
},
// The ring in the row's background keeps overlapped logos distinct.
slot: {
borderWidth: theme.borders.md,
borderColor: theme.colors.background,
borderRadius: theme.borderRadius.full,
},
stacked: {
marginLeft: -theme.spacing.xs,
},
logo: {
width: DAPP_LOGO_STACK_SIZE,
height: DAPP_LOGO_STACK_SIZE,
borderRadius: theme.borderRadius.full,
overflow: 'hidden',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: theme.colors.layerGray,
},
more: {
width: DAPP_LOGO_STACK_SIZE,
height: DAPP_LOGO_STACK_SIZE,
borderRadius: theme.borderRadius.full,
alignItems: 'center',
justifyContent: 'center',
borderWidth: theme.borders.sm,
borderColor: theme.colors.layerGray,
backgroundColor: theme.colors.background,
},
moreLabel: {
color: theme.colors.textGray,
},
}))
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const FavoritesSection = () => {
key: 'all',
name: t('discover.favorites.all_favorites'),
count: section.favoriteCount,
logos: section.favoriteLogos,
icon: 'star' as const,
onPress: section.openAllFavorites,
testID: 'discover_home_all_favorites',
Expand All @@ -45,6 +46,7 @@ export const FavoritesSection = () => {
key: folder.id,
name: folder.name,
count: folder.count,
logos: folder.logos,
icon: 'folder' as const,
onPress: () => section.openFolder(folder.id),
testID: `discover_home_folder_${folder.id}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,46 @@ describe('useFavoritesSection', () => {
// Assert
expect(result.current.favoriteCount).toBe(2)
expect(result.current.folders).toEqual([
{ id: 'folder-1', name: 'One', count: 1 },
{ id: 'folder-1', name: 'One', count: 1, logos: [null] },
])
expect(result.current.hasMore).toBe(false)
})

it("gives each folder its first dApps' logos in folder order", () => {
store().addFavorite({
...dapp('https://a.io'),
logo: 'https://a.io/l.png',
})
store().addFavorite(dapp('https://b.io'))
const id = store().createFolder('Swap')
store().setDappInFolder(id, 'https://b.io', true)
store().setDappInFolder(id, 'https://a.io', true)

const { result } = renderHook(() => useFavoritesSection())

expect(result.current.folders[0]?.logos).toEqual([
null,
'https://a.io/l.png',
])
})

it('gives All favorites the newest logos first, at most three', () => {
;['a', 'b', 'c', 'd'].forEach(name =>
store().addFavorite({
...dapp(`https://${name}.io`),
logo: `https://${name}.io/l.png`,
}),
)

const { result } = renderHook(() => useFavoritesSection())

expect(result.current.favoriteLogos).toEqual([
'https://d.io/l.png',
'https://c.io/l.png',
'https://b.io/l.png',
])
})

it('is empty only with no favorites and no folders', () => {
const { result, rerender } = renderHook(() => useFavoritesSection())
expect(result.current.isEmpty).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
limitations under the License
*/

import { useCallback } from 'react'
import { useCallback, useMemo } from 'react'
import type { Nullable } from '@perawallet/wallet-core-shared'
import { useAppNavigation } from '@hooks/useAppNavigation'
import { useCreateFolder } from '../../hooks/useCreateFolder'
import { useFolderRows, type FolderRowItem } from '../../hooks/useFolderRows'
import {
logosOf,
useFolderRows,
type FolderRowItem,
} from '../../hooks/useFolderRows'
import { useDiscoverFavoritesStore } from '../../store/useDiscoverFavoritesStore'

/** Folder rows the home shows before "See all". */
Expand All @@ -23,6 +28,8 @@ export type FavoritesFolderRow = FolderRowItem

export type UseFavoritesSectionResult = {
favoriteCount: number
/** All favorites' row logos: the newest favorites first. */
favoriteLogos: Nullable<string>[]
/** Newest first, at most `HOME_FOLDER_LIMIT`. */
folders: FavoritesFolderRow[]
hasMore: boolean
Expand All @@ -36,7 +43,13 @@ export type UseFavoritesSectionResult = {

export const useFavoritesSection = (): UseFavoritesSectionResult => {
const navigation = useAppNavigation()
const favoriteCount = useDiscoverFavoritesStore(s => s.favoriteOrder.length)
const favoriteOrder = useDiscoverFavoritesStore(s => s.favoriteOrder)
const favorites = useDiscoverFavoritesStore(s => s.favorites)
const favoriteCount = favoriteOrder.length
const favoriteLogos = useMemo(
() => logosOf([...favoriteOrder].reverse(), favorites),
[favoriteOrder, favorites],
)

const rows = useFolderRows()

Expand All @@ -60,6 +73,7 @@ export const useFavoritesSection = (): UseFavoritesSectionResult => {

return {
favoriteCount,
favoriteLogos,
folders: rows.slice(0, HOME_FOLDER_LIMIT),
hasMore: rows.length > HOME_FOLDER_LIMIT,
isEmpty: favoriteCount === 0 && rows.length === 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@
*/

import FolderIcon from '@assets/icons/discover/folder.svg'
import type { Nullable } from '@perawallet/wallet-core-shared'
import { PWIcon, PWText, PWTouchableOpacity, PWView } from '@components/core'
import { useLanguage } from '@hooks/useLanguage'
import { DappLogoStack } from '../DappLogoStack'
import { useStyles } from './styles'

export type FolderRowProps = {
name: string
count: number
/** The first dApps' logos, stacked before the count. */
logos?: Nullable<string>[]
/** \`folder\`: the design's folder glyph; \`star\`: the All favorites entry. */
icon: 'folder' | 'star'
onPress: () => void
testID?: string
}

/** A favorites folder (or the All favorites entry): icon, name, "N dApps", chevron. */
/** A favorites folder (or the All favorites entry): icon, name, logos and "N dApps", chevron. */
export const FolderRow = ({
name,
count,
logos,
icon,
onPress,
testID,
Expand Down Expand Up @@ -59,13 +64,21 @@ export const FolderRow = ({
>
{name}
</PWText>
<PWText
variant='footnoteMedium'
weight={400}
style={styles.count}
>
{t('discover.favorites.dapp_count', { count })}
</PWText>
<PWView style={styles.countRow}>
{logos ? (
<DappLogoStack
logos={logos}
total={count}
/>
) : null}
<PWText
variant='footnoteMedium'
weight={400}
style={styles.count}
>
{t('discover.favorites.dapp_count', { count })}
</PWText>
</PWView>
</PWView>
<PWView style={styles.chevron}>
<PWIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const useStyles = makeStyles(theme => ({
info: {
flex: 1,
},
countRow: {
flexDirection: 'row',
alignItems: 'center',
gap: theme.spacing.xs,
},
count: {
color: theme.colors.textGray,
},
Expand Down
Loading
Loading