diff --git a/apps/mobile/src/__integration__/discover-folders.spec.tsx b/apps/mobile/src/__integration__/discover-folders.spec.tsx index 6216e08a8f..1d8ccd704e 100644 --- a/apps/mobile/src/__integration__/discover-folders.spec.tsx +++ b/apps/mobile/src/__integration__/discover-folders.spec.tsx @@ -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' @@ -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') diff --git a/apps/mobile/src/modules/discover/components/DappLogoStack/DappLogoStack.tsx b/apps/mobile/src/modules/discover/components/DappLogoStack/DappLogoStack.tsx new file mode 100644 index 0000000000..b5f046a24e --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogoStack/DappLogoStack.tsx @@ -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[] + /** 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 ( + + {visible.map((logo, index) => ( + 0 && styles.stacked]} + testID={`discover_logo_stack_logo_${index}`} + > + {logo ? ( + + ) : ( + + + + )} + + ))} + {hiddenCount > 0 ? ( + + + + {`+${hiddenCount}`} + + + + ) : null} + + ) +} diff --git a/apps/mobile/src/modules/discover/components/DappLogoStack/index.ts b/apps/mobile/src/modules/discover/components/DappLogoStack/index.ts new file mode 100644 index 0000000000..671e3617f0 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogoStack/index.ts @@ -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' diff --git a/apps/mobile/src/modules/discover/components/DappLogoStack/styles.ts b/apps/mobile/src/modules/discover/components/DappLogoStack/styles.ts new file mode 100644 index 0000000000..d67ca0769e --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogoStack/styles.ts @@ -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, + }, +})) diff --git a/apps/mobile/src/modules/discover/components/FavoritesSection/FavoritesSection.tsx b/apps/mobile/src/modules/discover/components/FavoritesSection/FavoritesSection.tsx index 315691ca98..02a76cbd58 100644 --- a/apps/mobile/src/modules/discover/components/FavoritesSection/FavoritesSection.tsx +++ b/apps/mobile/src/modules/discover/components/FavoritesSection/FavoritesSection.tsx @@ -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', @@ -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}`, diff --git a/apps/mobile/src/modules/discover/components/FavoritesSection/__tests__/useFavoritesSection.spec.ts b/apps/mobile/src/modules/discover/components/FavoritesSection/__tests__/useFavoritesSection.spec.ts index b5c8fb0e35..f6a6e942c2 100644 --- a/apps/mobile/src/modules/discover/components/FavoritesSection/__tests__/useFavoritesSection.spec.ts +++ b/apps/mobile/src/modules/discover/components/FavoritesSection/__tests__/useFavoritesSection.spec.ts @@ -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) diff --git a/apps/mobile/src/modules/discover/components/FavoritesSection/useFavoritesSection.ts b/apps/mobile/src/modules/discover/components/FavoritesSection/useFavoritesSection.ts index 3cab5cade9..c1bd7e4700 100644 --- a/apps/mobile/src/modules/discover/components/FavoritesSection/useFavoritesSection.ts +++ b/apps/mobile/src/modules/discover/components/FavoritesSection/useFavoritesSection.ts @@ -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". */ @@ -23,6 +28,8 @@ export type FavoritesFolderRow = FolderRowItem export type UseFavoritesSectionResult = { favoriteCount: number + /** All favorites' row logos: the newest favorites first. */ + favoriteLogos: Nullable[] /** Newest first, at most `HOME_FOLDER_LIMIT`. */ folders: FavoritesFolderRow[] hasMore: boolean @@ -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() @@ -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, diff --git a/apps/mobile/src/modules/discover/components/FolderRow/FolderRow.tsx b/apps/mobile/src/modules/discover/components/FolderRow/FolderRow.tsx index 8ae55c4155..b49d0fbc03 100644 --- a/apps/mobile/src/modules/discover/components/FolderRow/FolderRow.tsx +++ b/apps/mobile/src/modules/discover/components/FolderRow/FolderRow.tsx @@ -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[] /** \`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, @@ -59,13 +64,21 @@ export const FolderRow = ({ > {name} - - {t('discover.favorites.dapp_count', { count })} - + + {logos ? ( + + ) : null} + + {t('discover.favorites.dapp_count', { count })} + + ({ info: { flex: 1, }, + countRow: { + flexDirection: 'row', + alignItems: 'center', + gap: theme.spacing.xs, + }, count: { color: theme.colors.textGray, }, diff --git a/apps/mobile/src/modules/discover/hooks/useFolderRows.ts b/apps/mobile/src/modules/discover/hooks/useFolderRows.ts index 9aac4500ce..4386285fb3 100644 --- a/apps/mobile/src/modules/discover/hooks/useFolderRows.ts +++ b/apps/mobile/src/modules/discover/hooks/useFolderRows.ts @@ -11,22 +11,51 @@ */ import { useMemo } from 'react' +import type { Nullable } from '@perawallet/wallet-core-shared' import { useDiscoverFavoritesStore } from '../store/useDiscoverFavoritesStore' +import type { DiscoverFavorite } from '../types' +import { DAPP_LOGO_STACK_MAX } from '../utils/dappLogoStack' -export type FolderRowItem = { id: string; name: string; count: number } +export type FolderRowItem = { + id: string + name: string + count: number + /** The first dApps' logos, in folder order, for the row's logo stack. */ + logos: Nullable[] +} + +/** The logos of the first favorites under `keys`, for a row's logo stack. */ +export const logosOf = ( + keys: readonly string[], + favorites: Readonly>, +): Nullable[] => + keys + .flatMap(key => { + const favorite = favorites[key] + return favorite ? [favorite.logo] : [] + }) + .slice(0, DAPP_LOGO_STACK_MAX) -/** Every folder as a row (name and dApp count), newest first. */ +/** Every folder as a row (name, dApp count, first logos), newest first. */ export const useFolderRows = (): FolderRowItem[] => { const folders = useDiscoverFavoritesStore(s => s.folders) const folderOrder = useDiscoverFavoritesStore(s => s.folderOrder) + const favorites = useDiscoverFavoritesStore(s => s.favorites) return useMemo( () => folderOrder.flatMap(id => { const folder = folders[id] return folder - ? [{ id, name: folder.name, count: folder.items.length }] + ? [ + { + id, + name: folder.name, + count: folder.items.length, + logos: logosOf(folder.items, favorites), + }, + ] : [] }), - [folders, folderOrder], + [folders, folderOrder, favorites], ) } diff --git a/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/DiscoverAllFoldersScreen.tsx b/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/DiscoverAllFoldersScreen.tsx index b518ec2eec..23abbd6955 100644 --- a/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/DiscoverAllFoldersScreen.tsx +++ b/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/DiscoverAllFoldersScreen.tsx @@ -32,6 +32,7 @@ export const DiscoverAllFoldersScreen = () => { screen.openFolder(item.id)} testID={`discover_all_folders_${item.id}`} diff --git a/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/__tests__/useDiscoverAllFoldersScreen.spec.ts b/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/__tests__/useDiscoverAllFoldersScreen.spec.ts index 5e75c7da2b..7138ab305d 100644 --- a/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/__tests__/useDiscoverAllFoldersScreen.spec.ts +++ b/apps/mobile/src/modules/discover/screens/DiscoverAllFoldersScreen/__tests__/useDiscoverAllFoldersScreen.spec.ts @@ -55,6 +55,7 @@ describe('useDiscoverAllFoldersScreen', () => { id: 'folder-5', name: 'E', count: 0, + logos: [], }) expect(navigate).toHaveBeenCalledWith('DiscoverFolder', { folderId: 'folder-2', diff --git a/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/useDiscoverFolderScreen.ts b/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/useDiscoverFolderScreen.ts index 06ddbda6ca..a67909edda 100644 --- a/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/useDiscoverFolderScreen.ts +++ b/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/useDiscoverFolderScreen.ts @@ -29,7 +29,7 @@ import type { DiscoverFavorite } from '../../types' export type UseDiscoverFolderScreenResult = UseFavoriteDappActionsResult & { folderId: string /** Null once the folder is gone (deleted elsewhere); the screen then leaves. */ - folder: Nullable + folder: Nullable> dapps: DiscoverFavorite[] } diff --git a/apps/mobile/src/modules/discover/utils/__tests__/dappLogoStack.spec.ts b/apps/mobile/src/modules/discover/utils/__tests__/dappLogoStack.spec.ts new file mode 100644 index 0000000000..422e42afca --- /dev/null +++ b/apps/mobile/src/modules/discover/utils/__tests__/dappLogoStack.spec.ts @@ -0,0 +1,41 @@ +/* + 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 { describe, expect, it } from 'vitest' +import { DAPP_LOGO_STACK_MAX, dappLogoStack } from '../dappLogoStack' + +describe('dappLogoStack', () => { + it(`shows up to ${DAPP_LOGO_STACK_MAX} logos and counts the rest`, () => { + expect(dappLogoStack(['a', null, 'c'], 5)).toEqual({ + visible: ['a', null, 'c'], + hiddenCount: 2, + }) + }) + + it('has no "+N" when every dApp is visible', () => { + expect(dappLogoStack(['a', 'b'], 2)).toEqual({ + visible: ['a', 'b'], + hiddenCount: 0, + }) + }) + + it('takes only the first logos when given more', () => { + expect(dappLogoStack(['a', 'b', 'c', 'd'], 4)).toEqual({ + visible: ['a', 'b', 'c'], + hiddenCount: 1, + }) + }) + + it('is empty for an empty folder', () => { + expect(dappLogoStack([], 0)).toEqual({ visible: [], hiddenCount: 0 }) + }) +}) diff --git a/apps/mobile/src/modules/discover/utils/dappLogoStack.ts b/apps/mobile/src/modules/discover/utils/dappLogoStack.ts new file mode 100644 index 0000000000..f7a3f65811 --- /dev/null +++ b/apps/mobile/src/modules/discover/utils/dappLogoStack.ts @@ -0,0 +1,31 @@ +/* + 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' + +/** Logos a folder row shows before the "+N" chip, like `ChainIconStack`. */ +export const DAPP_LOGO_STACK_MAX = 3 + +export type DappLogoStack = { + visible: Nullable[] + /** dApps past the visible logos, shown as "+N". */ + hiddenCount: number +} + +/** The logos to stack and how many dApps are left over, out of `total`. */ +export const dappLogoStack = ( + logos: readonly Nullable[], + total: number, +): DappLogoStack => { + const visible = logos.slice(0, DAPP_LOGO_STACK_MAX) + return { visible, hiddenCount: Math.max(0, total - visible.length) } +}