From c4599d344c51cefa213e509fda9a394bb5d1f556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:03:57 +0300 Subject: [PATCH 01/10] feat(discover): save a folder's dApps in a given order [PER-2760] --- .../useDiscoverFavoritesStore.spec.ts | 44 +++++++++++++++++++ .../store/useDiscoverFavoritesStore.ts | 14 ++++++ 2 files changed, 58 insertions(+) diff --git a/apps/mobile/src/modules/discover/store/__tests__/useDiscoverFavoritesStore.spec.ts b/apps/mobile/src/modules/discover/store/__tests__/useDiscoverFavoritesStore.spec.ts index 171bd8389a..c54ab6fa83 100644 --- a/apps/mobile/src/modules/discover/store/__tests__/useDiscoverFavoritesStore.spec.ts +++ b/apps/mobile/src/modules/discover/store/__tests__/useDiscoverFavoritesStore.spec.ts @@ -236,6 +236,50 @@ describe('useDiscoverFavoritesStore folders', () => { expect(Object.keys(state().folders)).toEqual([id]) }) + describe('setFolderItems', () => { + const a = 'https://a.io' + const b = 'https://b.io' + const c = 'https://c.io' + const folderWith = (...urls: string[]) => { + urls.forEach(url => state().addFavorite(dapp(url))) + const id = state().createFolder('One') + urls.forEach(url => state().setDappInFolder(id, url, true)) + return id + } + + test('saves the given order', () => { + const id = folderWith(a, b, c) + state().setFolderItems(id, [c, a, b]) + expect(state().folders[id]?.items).toEqual([c, a, b]) + }) + + test('drops keys that are no longer favorites', () => { + const id = folderWith(a, b) + state().removeFavorite(b) + state().setFolderItems(id, [b, a]) + expect(state().folders[id]?.items).toEqual([a]) + }) + + test('keeps each key once', () => { + const id = folderWith(a, b) + state().setFolderItems(id, [a, a, b]) + expect(state().folders[id]?.items).toEqual([a, b]) + }) + + test('leaves an empty folder when every dApp is taken out', () => { + const id = folderWith(a, b) + state().setFolderItems(id, []) + expect(state().folders[id]?.items).toEqual([]) + }) + + test('is a no-op for an unknown folder', () => { + folderWith(a) + const before = state().folders + state().setFolderItems('missing', [a]) + expect(state().folders).toBe(before) + }) + }) + test('deleteFolder keeps the favorites', () => { state().addFavorite(dapp('https://a.io')) const id = state().createFolder('One', 'https://a.io') diff --git a/apps/mobile/src/modules/discover/store/useDiscoverFavoritesStore.ts b/apps/mobile/src/modules/discover/store/useDiscoverFavoritesStore.ts index 464561b68c..f9e2d33eff 100644 --- a/apps/mobile/src/modules/discover/store/useDiscoverFavoritesStore.ts +++ b/apps/mobile/src/modules/discover/store/useDiscoverFavoritesStore.ts @@ -62,6 +62,11 @@ type DiscoverFavoritesActions = { deleteFolder(id: string): void /** No-op unless `url` is a favorite and the folder exists. */ setDappInFolder(id: string, url: string, inFolder: boolean): void + /** + * Rearrange's Done: the folder's dApps in this order. Keys that are not + * favorites any more are dropped, each key is kept once. + */ + setFolderItems(id: string, keys: readonly string[]): void /** * Adds the legacy favorites that are not favorites yet, in no folder, so * newest-first lists show them after the native ones in the given order; @@ -257,6 +262,15 @@ export const useDiscoverFavoritesStore = create()( folders: { ...state.folders, [id]: { ...folder, items } }, })) }, + setFolderItems: (id, keys) => { + const folder = get().folders[id] + if (!folder) return + const { favorites } = get() + const items = [...new Set(keys)].filter(key => key in favorites) + set(state => ({ + folders: { ...state.folders, [id]: { ...folder, items } }, + })) + }, importLegacyFavorites: (entries, directory) => { const { favorites, legacyImport } = get() const added: Record = {} From ab0220c4ac3121f8a0720c284dc7a8c64c27539c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:04:55 +0300 Subject: [PATCH 02/10] feat(discover): copy and analytics for the folder menu [PER-2760] --- .../src/analytics/events/contexts/discover.ts | 6 ++++++ .../src/analytics/events/metadata-keys.ts | 1 + apps/mobile/src/i18n/locales/de.json | 18 +++++++++++++++++- apps/mobile/src/i18n/locales/en.json | 18 +++++++++++++++++- apps/mobile/src/i18n/locales/es.json | 18 +++++++++++++++++- apps/mobile/src/i18n/locales/fr.json | 18 +++++++++++++++++- apps/mobile/src/i18n/locales/pt-BR.json | 18 +++++++++++++++++- apps/mobile/src/i18n/locales/tr.json | 18 +++++++++++++++++- 8 files changed, 109 insertions(+), 6 deletions(-) diff --git a/apps/mobile/src/analytics/events/contexts/discover.ts b/apps/mobile/src/analytics/events/contexts/discover.ts index 218b4a1876..1eea1407dc 100644 --- a/apps/mobile/src/analytics/events/contexts/discover.ts +++ b/apps/mobile/src/analytics/events/contexts/discover.ts @@ -31,6 +31,9 @@ export enum DiscoverEvent { FolderCreate = 'discover_folder_create', FolderDappAdd = 'discover_folder_dapp_add', FolderDappRemove = 'discover_folder_dapp_remove', + FolderRename = 'discover_folder_rename', + FolderDelete = 'discover_folder_delete', + FolderRearrange = 'discover_folder_rearrange', FavoriteToastTap = 'discover_favorite_toast_tap', LegacyFavoritesImported = 'discover_legacy_favorites_imported', LegacyFavoritesImportFailed = 'discover_favorites_migration_failed', @@ -71,6 +74,9 @@ export interface DiscoverRequiredPayloads { [Key.DappName]: string [Key.ChainId]?: ChainId } + [DiscoverEvent.FolderRearrange]: { + [Key.RemovedCount]: number + } [DiscoverEvent.LegacyFavoritesImported]: { [Key.ImportedCount]: number [Key.SkippedCount]: number diff --git a/apps/mobile/src/analytics/events/metadata-keys.ts b/apps/mobile/src/analytics/events/metadata-keys.ts index 1b54c30908..120ad9e0aa 100644 --- a/apps/mobile/src/analytics/events/metadata-keys.ts +++ b/apps/mobile/src/analytics/events/metadata-keys.ts @@ -74,6 +74,7 @@ export enum AnalyticsMetadataKey { ImportedCount = 'imported_count', SkippedCount = 'skipped_count', AlreadyFavoriteCount = 'already_favorite_count', + RemovedCount = 'removed_count', UnreadableKeyCount = 'unreadable_key_count', AttemptCount = 'attempt_count', FailureReason = 'failure_reason', diff --git a/apps/mobile/src/i18n/locales/de.json b/apps/mobile/src/i18n/locales/de.json index 22195fc1ac..431ca8aa1f 100644 --- a/apps/mobile/src/i18n/locales/de.json +++ b/apps/mobile/src/i18n/locales/de.json @@ -85,7 +85,23 @@ "create": "Erstellen", "save": "Speichern" }, - "empty": "Noch keine dApps in diesem Ordner" + "empty": "Noch keine dApps in diesem Ordner", + "menu": { + "title": "Ordneroptionen", + "open_label": "Ordneroptionen", + "rename": "Ordner umbenennen", + "rearrange": "dApps neu anordnen", + "delete": "Ordner löschen" + }, + "rearrange": { + "title": "dApps neu anordnen", + "remove_label": "{{name}} aus diesem Ordner entfernen" + }, + "delete_confirm": { + "title": "Diesen Ordner löschen?", + "body": "Seine dApps bleiben in deinen Favoriten.", + "confirm": "Ordner löschen" + } }, "favorites": { "title": "Favoriten", diff --git a/apps/mobile/src/i18n/locales/en.json b/apps/mobile/src/i18n/locales/en.json index 09ab5e7704..cde3e99c5f 100644 --- a/apps/mobile/src/i18n/locales/en.json +++ b/apps/mobile/src/i18n/locales/en.json @@ -85,7 +85,23 @@ "create": "Create", "save": "Save" }, - "empty": "No dApps in this folder yet" + "empty": "No dApps in this folder yet", + "menu": { + "title": "Folder options", + "open_label": "Folder options", + "rename": "Rename Folder", + "rearrange": "Rearrange dApps", + "delete": "Delete Folder" + }, + "rearrange": { + "title": "Rearrange dApps", + "remove_label": "Remove {{name}} from this folder" + }, + "delete_confirm": { + "title": "Delete this folder?", + "body": "Its dApps stay in your favorites.", + "confirm": "Delete Folder" + } }, "favorites": { "title": "Favorites", diff --git a/apps/mobile/src/i18n/locales/es.json b/apps/mobile/src/i18n/locales/es.json index 23a6df7533..629204f39d 100644 --- a/apps/mobile/src/i18n/locales/es.json +++ b/apps/mobile/src/i18n/locales/es.json @@ -85,7 +85,23 @@ "create": "Crear", "save": "Guardar" }, - "empty": "Aún no hay dApps en esta carpeta" + "empty": "Aún no hay dApps en esta carpeta", + "menu": { + "title": "Opciones de la carpeta", + "open_label": "Opciones de la carpeta", + "rename": "Cambiar nombre de la carpeta", + "rearrange": "Reordenar dApps", + "delete": "Eliminar carpeta" + }, + "rearrange": { + "title": "Reordenar dApps", + "remove_label": "Quitar {{name}} de esta carpeta" + }, + "delete_confirm": { + "title": "¿Eliminar esta carpeta?", + "body": "Sus dApps seguirán en tus favoritos.", + "confirm": "Eliminar carpeta" + } }, "favorites": { "title": "Favoritos", diff --git a/apps/mobile/src/i18n/locales/fr.json b/apps/mobile/src/i18n/locales/fr.json index 850b3f88e7..d7906b3fa5 100644 --- a/apps/mobile/src/i18n/locales/fr.json +++ b/apps/mobile/src/i18n/locales/fr.json @@ -85,7 +85,23 @@ "create": "Créer", "save": "Enregistrer" }, - "empty": "Aucune dApp dans ce dossier pour le moment" + "empty": "Aucune dApp dans ce dossier pour le moment", + "menu": { + "title": "Options du dossier", + "open_label": "Options du dossier", + "rename": "Renommer le dossier", + "rearrange": "Réorganiser les dApps", + "delete": "Supprimer le dossier" + }, + "rearrange": { + "title": "Réorganiser les dApps", + "remove_label": "Retirer {{name}} de ce dossier" + }, + "delete_confirm": { + "title": "Supprimer ce dossier ?", + "body": "Ses dApps restent dans vos favoris.", + "confirm": "Supprimer le dossier" + } }, "favorites": { "title": "Favoris", diff --git a/apps/mobile/src/i18n/locales/pt-BR.json b/apps/mobile/src/i18n/locales/pt-BR.json index 7f0369b3fa..08e15c4eb7 100644 --- a/apps/mobile/src/i18n/locales/pt-BR.json +++ b/apps/mobile/src/i18n/locales/pt-BR.json @@ -85,7 +85,23 @@ "create": "Criar", "save": "Salvar" }, - "empty": "Ainda não há dApps nesta pasta" + "empty": "Ainda não há dApps nesta pasta", + "menu": { + "title": "Opções da pasta", + "open_label": "Opções da pasta", + "rename": "Renomear pasta", + "rearrange": "Reorganizar dApps", + "delete": "Excluir pasta" + }, + "rearrange": { + "title": "Reorganizar dApps", + "remove_label": "Remover {{name}} desta pasta" + }, + "delete_confirm": { + "title": "Excluir esta pasta?", + "body": "Os dApps dela continuam nos seus favoritos.", + "confirm": "Excluir pasta" + } }, "favorites": { "title": "Favoritos", diff --git a/apps/mobile/src/i18n/locales/tr.json b/apps/mobile/src/i18n/locales/tr.json index 3db2281af7..ee63b03a36 100644 --- a/apps/mobile/src/i18n/locales/tr.json +++ b/apps/mobile/src/i18n/locales/tr.json @@ -85,7 +85,23 @@ "create": "Oluştur", "save": "Kaydet" }, - "empty": "Bu klasörde henüz dApp yok" + "empty": "Bu klasörde henüz dApp yok", + "menu": { + "title": "Klasör seçenekleri", + "open_label": "Klasör seçenekleri", + "rename": "Klasörü yeniden adlandır", + "rearrange": "dApp'leri yeniden sırala", + "delete": "Klasörü sil" + }, + "rearrange": { + "title": "dApp'leri yeniden sırala", + "remove_label": "Bu klasörden çıkar: {{name}}" + }, + "delete_confirm": { + "title": "Bu klasör silinsin mi?", + "body": "İçindeki dApp'ler favorilerinde kalır.", + "confirm": "Klasörü sil" + } }, "favorites": { "title": "Favoriler", From 8e87e41dbd2ba1b321e73174b24cfee0100b796a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:06:14 +0300 Subject: [PATCH 03/10] feat(discover): rearrange sheet for a folder's dApps [PER-2760] --- .../RearrangeFolderSheet.tsx | 147 ++++++++++++++++++ .../__tests__/useRearrangeFolderSheet.spec.ts | 118 ++++++++++++++ .../components/RearrangeFolderSheet/index.ts | 15 ++ .../components/RearrangeFolderSheet/styles.ts | 56 +++++++ .../useRearrangeFolderSheet.ts | 71 +++++++++ 5 files changed, 407 insertions(+) create mode 100644 apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx create mode 100644 apps/mobile/src/modules/discover/components/RearrangeFolderSheet/__tests__/useRearrangeFolderSheet.spec.ts create mode 100644 apps/mobile/src/modules/discover/components/RearrangeFolderSheet/index.ts create mode 100644 apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts create mode 100644 apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx new file mode 100644 index 0000000000..b3c9ee8d65 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx @@ -0,0 +1,147 @@ +/* + 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 { useCallback } from 'react' +import DraggableFlatList, { + ScaleDecorator, + type RenderItemParams, +} from 'react-native-draggable-flatlist' +import { useSafeAreaInsets } from 'react-native-safe-area-context' +import { + PWButton, + PWIcon, + PWImage, + PWText, + PWTouchableOpacity, + PWView, +} from '@components/core' +import { SheetHeader } from '@modules/bottom-sheet' +import { useLanguage } from '@hooks/useLanguage' +import type { DiscoverFavorite } from '../../types' +import { + favoriteKeyOf, + useRearrangeFolderSheet, +} from './useRearrangeFolderSheet' +import { useStyles } from './styles' + +export type RearrangeFolderSheetProps = { folderId: string } + +/** + * Reorders a folder's dApps and takes some out; open it with + * `useBottomSheet().request`. Done resolves the + * keys, the close icon is Cancel. + */ +export const RearrangeFolderSheet = ({ + folderId, +}: RearrangeFolderSheetProps) => { + const { t } = useLanguage() + const insets = useSafeAreaInsets() + const styles = useStyles({ bottomInset: insets.bottom }) + const sheet = useRearrangeFolderSheet({ folderId }) + const { remove, reorder } = sheet + + const renderItem = useCallback( + ({ item, drag, isActive }: RenderItemParams) => { + const key = favoriteKeyOf(item) + return ( + + + remove(key)} + style={styles.remove} + accessibilityLabel={t( + 'discover.folders.rearrange.remove_label', + { name: item.name }, + )} + testID={`discover_rearrange_remove_${key}`} + > + + + {item.logo ? ( + + ) : ( + + + + )} + + {item.name} + + + + + + + ) + }, + [styles, remove, t], + ) + + const handleDragEnd = useCallback( + ({ data }: { data: DiscoverFavorite[] }) => + reorder(data.map(favoriteKeyOf)), + [reorder], + ) + + return ( + + + } + /> + + + ) +} diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/__tests__/useRearrangeFolderSheet.spec.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/__tests__/useRearrangeFolderSheet.spec.ts new file mode 100644 index 0000000000..d11c5ad1f9 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/__tests__/useRearrangeFolderSheet.spec.ts @@ -0,0 +1,118 @@ +/* + 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 { act, renderHook } from '@testing-library/react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { generateUniqueId } from '@perawallet/wallet-core-shared' +import { useBottomSheetResult } from '@modules/bottom-sheet' +import { useDiscoverFavoritesStore } from '../../../store/useDiscoverFavoritesStore' +import { useRearrangeFolderSheet } from '../useRearrangeFolderSheet' + +vi.mock('@modules/bottom-sheet', () => ({ + useBottomSheetResult: vi.fn(), +})) + +const resolve = vi.fn() +const dismiss = vi.fn() +const K1 = 'https://one.example' +const K2 = 'https://two.example' + +const store = () => useDiscoverFavoritesStore.getState() +const keysOf = (dapps: { url: string }[]) => dapps.map(d => d.url) + +describe('useRearrangeFolderSheet', () => { + let folderId: string + + beforeEach(() => { + vi.clearAllMocks() + let next = 0 + vi.mocked(generateUniqueId).mockImplementation(() => `folder-${++next}`) + vi.mocked(useBottomSheetResult).mockReturnValue({ + resolve, + dismiss, + } as never) + useDiscoverFavoritesStore.setState({ + favorites: {}, + favoriteOrder: [], + folders: {}, + folderOrder: [], + }) + for (const url of [K1, K2]) { + store().addFavorite({ + url, + name: url, + logo: null, + categories: [], + chains: [], + }) + } + folderId = store().createFolder('Swap') + store().setDappInFolder(folderId, K1, true) + store().setDappInFolder(folderId, K2, true) + }) + + it('starts with the folder dApps in folder order', () => { + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + expect(keysOf(result.current.dapps)).toEqual([K1, K2]) + }) + + it('reorders the working list', () => { + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + act(() => result.current.reorder([K2, K1])) + + expect(keysOf(result.current.dapps)).toEqual([K2, K1]) + expect(store().folders[folderId]?.items).toEqual([K1, K2]) + }) + + it('removes a dApp from the working list only', () => { + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + act(() => result.current.remove(K1)) + + expect(keysOf(result.current.dapps)).toEqual([K2]) + expect(store().folders[folderId]?.items).toEqual([K1, K2]) + expect(store().isFavorite(K1)).toBe(true) + }) + + it('resolves the working order on done', () => { + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + act(() => result.current.reorder([K2, K1])) + act(() => result.current.remove(K1)) + act(() => result.current.done()) + + expect(resolve).toHaveBeenCalledWith([K2]) + }) + + it('dismisses on cancel without touching the store', () => { + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + act(() => result.current.remove(K1)) + act(() => result.current.cancel()) + + expect(dismiss).toHaveBeenCalledTimes(1) + expect(resolve).not.toHaveBeenCalled() + expect(store().folders[folderId]?.items).toEqual([K1, K2]) + }) +}) diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/index.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/index.ts new file mode 100644 index 0000000000..155f60fc69 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/index.ts @@ -0,0 +1,15 @@ +/* + 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 { RearrangeFolderSheet } from './RearrangeFolderSheet' +export type { RearrangeFolderSheetProps } from './RearrangeFolderSheet' +export type { RearrangeFolderSheetResult } from './useRearrangeFolderSheet' diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts new file mode 100644 index 0000000000..243e2ed554 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts @@ -0,0 +1,56 @@ +/* + 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' +import { DISCOVER_ROW_LEAD_SIZE } from '../../utils/discoverLayout' + +type StyleProps = { bottomInset?: number } + +export const useStyles = makeStyles( + (theme, { bottomInset = 0 }: StyleProps = {}) => ({ + container: { + flex: 1, + }, + listFill: { + flex: 1, + }, + listContent: { + paddingHorizontal: theme.spacing.lg, + paddingBottom: theme.spacing.xl + bottomInset, + }, + row: { + flexDirection: 'row', + alignItems: 'center', + gap: theme.spacing.md, + paddingVertical: theme.spacing.sm, + backgroundColor: theme.colors.background, + }, + remove: { + padding: theme.spacing.xs, + }, + logo: { + width: DISCOVER_ROW_LEAD_SIZE, + height: DISCOVER_ROW_LEAD_SIZE, + borderRadius: theme.borderRadius.full, + overflow: 'hidden', + alignItems: 'center', + justifyContent: 'center', + backgroundColor: theme.colors.layerGray, + }, + name: { + flex: 1, + }, + dragHandle: { + paddingLeft: theme.spacing.md, + }, + }), +) diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts new file mode 100644 index 0000000000..dd07d4f969 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts @@ -0,0 +1,71 @@ +/* + 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 { useCallback, useState } from 'react' +import { useBottomSheetResult } from '@modules/bottom-sheet' +import { + selectFolderFavorites, + useDiscoverFavoritesStore, +} from '../../store/useDiscoverFavoritesStore' +import type { DiscoverFavorite } from '../../types' +import { normalizeDappUrl } from '../../utils/normalizeDappUrl' + +/** What the sheet resolves with on Done: the folder's favorite keys, in order. */ +export type RearrangeFolderSheetResult = string[] + +export type UseRearrangeFolderSheetParams = { folderId: string } + +export type UseRearrangeFolderSheetResult = { + dapps: DiscoverFavorite[] + reorder: (keys: string[]) => void + remove: (key: string) => void + done: () => void + cancel: () => void +} + +/** A favorite's store key; favorites only hold urls that normalise. */ +export const favoriteKeyOf = (dapp: Pick): string => + normalizeDappUrl(dapp.url) ?? dapp.url + +/** + * The working list of the Rearrange sheet. Read once at mount and kept local + * until Done, so Cancel or a dismiss changes nothing. + */ +export const useRearrangeFolderSheet = ({ + folderId, +}: UseRearrangeFolderSheetParams): UseRearrangeFolderSheetResult => { + const { resolve, dismiss } = + useBottomSheetResult() + const [dapps, setDapps] = useState(() => + selectFolderFavorites(useDiscoverFavoritesStore.getState(), folderId), + ) + + const reorder = useCallback((keys: string[]) => { + setDapps(current => { + const byKey = new Map(current.map(d => [favoriteKeyOf(d), d])) + return keys.flatMap(key => { + const dapp = byKey.get(key) + return dapp ? [dapp] : [] + }) + }) + }, []) + const remove = useCallback((key: string) => { + setDapps(current => current.filter(d => favoriteKeyOf(d) !== key)) + }, []) + const done = useCallback( + () => resolve(dapps.map(favoriteKeyOf)), + [dapps, resolve], + ) + const cancel = useCallback(() => dismiss(), [dismiss]) + + return { dapps, reorder, remove, done, cancel } +} From 7e6acb335d7bfd5b570df855fb191ca833b5c6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:08:17 +0300 Subject: [PATCH 04/10] feat(discover): folder menu with rename, rearrange and delete [PER-2760] --- .../DeleteFolderSheet/DeleteFolderSheet.tsx | 35 +++++ .../components/DeleteFolderSheet/index.ts | 13 ++ .../FolderMenuSheet/FolderMenuSheet.tsx | 59 ++++++++ .../__tests__/useFolderMenuSheet.spec.ts | 70 +++++++++ .../components/FolderMenuSheet/index.ts | 15 ++ .../components/FolderMenuSheet/styles.ts | 25 +++ .../FolderMenuSheet/useFolderMenuSheet.ts | 69 +++++++++ .../hooks/__tests__/useFolderMenu.spec.ts | 143 ++++++++++++++++++ .../modules/discover/hooks/useFolderMenu.tsx | 112 ++++++++++++++ 9 files changed, 541 insertions(+) create mode 100644 apps/mobile/src/modules/discover/components/DeleteFolderSheet/DeleteFolderSheet.tsx create mode 100644 apps/mobile/src/modules/discover/components/DeleteFolderSheet/index.ts create mode 100644 apps/mobile/src/modules/discover/components/FolderMenuSheet/FolderMenuSheet.tsx create mode 100644 apps/mobile/src/modules/discover/components/FolderMenuSheet/__tests__/useFolderMenuSheet.spec.ts create mode 100644 apps/mobile/src/modules/discover/components/FolderMenuSheet/index.ts create mode 100644 apps/mobile/src/modules/discover/components/FolderMenuSheet/styles.ts create mode 100644 apps/mobile/src/modules/discover/components/FolderMenuSheet/useFolderMenuSheet.ts create mode 100644 apps/mobile/src/modules/discover/hooks/__tests__/useFolderMenu.spec.ts create mode 100644 apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx diff --git a/apps/mobile/src/modules/discover/components/DeleteFolderSheet/DeleteFolderSheet.tsx b/apps/mobile/src/modules/discover/components/DeleteFolderSheet/DeleteFolderSheet.tsx new file mode 100644 index 0000000000..a166971047 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DeleteFolderSheet/DeleteFolderSheet.tsx @@ -0,0 +1,35 @@ +/* + 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 { ConfirmActionContent } from '@components/ConfirmActionContent' +import { useLanguage } from '@hooks/useLanguage' + +/** Confirms deleting a folder; resolves `true` on confirm. */ +export const DeleteFolderSheet = () => { + const { t } = useLanguage() + return ( + + icon='trash' + iconVariant='error' + title={t('discover.folders.delete_confirm.title')} + message={t('discover.folders.delete_confirm.body')} + isMessageCentered + confirmLabel={t('discover.folders.delete_confirm.confirm')} + confirmVariant='destructive' + confirmValue + cancelLabel={t('common.cancel.label')} + testID='discover_delete_folder_sheet' + confirmTestID='discover_delete_folder_confirm' + cancelTestID='discover_delete_folder_cancel' + /> + ) +} diff --git a/apps/mobile/src/modules/discover/components/DeleteFolderSheet/index.ts b/apps/mobile/src/modules/discover/components/DeleteFolderSheet/index.ts new file mode 100644 index 0000000000..a8706f430a --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DeleteFolderSheet/index.ts @@ -0,0 +1,13 @@ +/* + 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 { DeleteFolderSheet } from './DeleteFolderSheet' diff --git a/apps/mobile/src/modules/discover/components/FolderMenuSheet/FolderMenuSheet.tsx b/apps/mobile/src/modules/discover/components/FolderMenuSheet/FolderMenuSheet.tsx new file mode 100644 index 0000000000..64c686afb9 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/FolderMenuSheet/FolderMenuSheet.tsx @@ -0,0 +1,59 @@ +/* + 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 { + PWIcon, + PWSheetLayout, + PWText, + PWTouchableOpacity, +} from '@components/core' +import { SheetHeader } from '@modules/bottom-sheet' +import { useLanguage } from '@hooks/useLanguage' +import { useFolderMenuSheet } from './useFolderMenuSheet' +import { useStyles } from './styles' + +export type FolderMenuSheetProps = { hasDapps: boolean } + +/** The folder screen's "…" menu; resolves a `FolderMenuChoice`. */ +export const FolderMenuSheet = ({ hasDapps }: FolderMenuSheetProps) => { + const { t } = useLanguage() + const styles = useStyles() + const sheet = useFolderMenuSheet({ hasDapps }) + + return ( + } + > + {sheet.items.map(item => ( + sheet.choose(item.choice)} + testID={`discover_folder_menu_${item.choice}`} + > + + + {t(item.labelKey)} + + + ))} + + ) +} diff --git a/apps/mobile/src/modules/discover/components/FolderMenuSheet/__tests__/useFolderMenuSheet.spec.ts b/apps/mobile/src/modules/discover/components/FolderMenuSheet/__tests__/useFolderMenuSheet.spec.ts new file mode 100644 index 0000000000..7c0ad09ec0 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/FolderMenuSheet/__tests__/useFolderMenuSheet.spec.ts @@ -0,0 +1,70 @@ +/* + 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 { act, renderHook } from '@testing-library/react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { useBottomSheetResult } from '@modules/bottom-sheet' +import { useFolderMenuSheet } from '../useFolderMenuSheet' + +vi.mock('@modules/bottom-sheet', () => ({ + useBottomSheetResult: vi.fn(), +})) + +const resolve = vi.fn() + +describe('useFolderMenuSheet', () => { + beforeEach(() => { + vi.clearAllMocks() + vi.mocked(useBottomSheetResult).mockReturnValue({ + resolve, + dismiss: vi.fn(), + } as never) + }) + + it('lists rename, rearrange, delete in that order', () => { + const { result } = renderHook(() => + useFolderMenuSheet({ hasDapps: true }), + ) + + expect(result.current.items.map(i => i.choice)).toEqual([ + 'rename', + 'rearrange', + 'delete', + ]) + expect(result.current.items.map(i => i.isDestructive)).toEqual([ + false, + false, + true, + ]) + }) + + it('leaves out rearrange for an empty folder', () => { + const { result } = renderHook(() => + useFolderMenuSheet({ hasDapps: false }), + ) + + expect(result.current.items.map(i => i.choice)).toEqual([ + 'rename', + 'delete', + ]) + }) + + it('resolves the chosen item', () => { + const { result } = renderHook(() => + useFolderMenuSheet({ hasDapps: true }), + ) + + act(() => result.current.choose('delete')) + + expect(resolve).toHaveBeenCalledWith('delete') + }) +}) diff --git a/apps/mobile/src/modules/discover/components/FolderMenuSheet/index.ts b/apps/mobile/src/modules/discover/components/FolderMenuSheet/index.ts new file mode 100644 index 0000000000..e6c07ac6c8 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/FolderMenuSheet/index.ts @@ -0,0 +1,15 @@ +/* + 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 { FolderMenuSheet } from './FolderMenuSheet' +export type { FolderMenuSheetProps } from './FolderMenuSheet' +export type { FolderMenuChoice } from './useFolderMenuSheet' diff --git a/apps/mobile/src/modules/discover/components/FolderMenuSheet/styles.ts b/apps/mobile/src/modules/discover/components/FolderMenuSheet/styles.ts new file mode 100644 index 0000000000..d28b0e6275 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/FolderMenuSheet/styles.ts @@ -0,0 +1,25 @@ +/* + 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' + +export const useStyles = makeStyles(theme => ({ + row: { + flexDirection: 'row', + alignItems: 'center', + gap: theme.spacing.md, + paddingVertical: theme.spacing.md, + }, + destructive: { + color: theme.colors.error, + }, +})) diff --git a/apps/mobile/src/modules/discover/components/FolderMenuSheet/useFolderMenuSheet.ts b/apps/mobile/src/modules/discover/components/FolderMenuSheet/useFolderMenuSheet.ts new file mode 100644 index 0000000000..00870432b3 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/FolderMenuSheet/useFolderMenuSheet.ts @@ -0,0 +1,69 @@ +/* + 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 { useCallback, useMemo } from 'react' +import { i18nKey, type I18nKey } from '@perawallet/wallet-core-chain-contract' +import type { IconName } from '@components/core' +import { useBottomSheetResult } from '@modules/bottom-sheet' + +export type FolderMenuChoice = 'rename' | 'rearrange' | 'delete' + +export type FolderMenuItem = { + choice: FolderMenuChoice + labelKey: I18nKey + icon: IconName + isDestructive: boolean +} + +export type UseFolderMenuSheetParams = { hasDapps: boolean } + +export type UseFolderMenuSheetResult = { + items: FolderMenuItem[] + choose: (choice: FolderMenuChoice) => void +} + +const ITEMS: FolderMenuItem[] = [ + { + choice: 'rename', + labelKey: i18nKey('discover.folders.menu.rename'), + icon: 'edit-pen', + isDestructive: false, + }, + { + choice: 'rearrange', + labelKey: i18nKey('discover.folders.menu.rearrange'), + icon: 'horizontal-line-stack', + isDestructive: false, + }, + { + choice: 'delete', + labelKey: i18nKey('discover.folders.menu.delete'), + icon: 'trash', + isDestructive: true, + }, +] + +/** The folder menu's rows; an empty folder has nothing to rearrange. */ +export const useFolderMenuSheet = ({ + hasDapps, +}: UseFolderMenuSheetParams): UseFolderMenuSheetResult => { + const { resolve } = useBottomSheetResult() + const items = useMemo( + () => ITEMS.filter(item => hasDapps || item.choice !== 'rearrange'), + [hasDapps], + ) + const choose = useCallback( + (choice: FolderMenuChoice) => resolve(choice), + [resolve], + ) + return { items, choose } +} diff --git a/apps/mobile/src/modules/discover/hooks/__tests__/useFolderMenu.spec.ts b/apps/mobile/src/modules/discover/hooks/__tests__/useFolderMenu.spec.ts new file mode 100644 index 0000000000..9db0e1a75e --- /dev/null +++ b/apps/mobile/src/modules/discover/hooks/__tests__/useFolderMenu.spec.ts @@ -0,0 +1,143 @@ +/* + 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 { isValidElement, type ReactElement } from 'react' +import { act, renderHook } from '@testing-library/react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { generateUniqueId } from '@perawallet/wallet-core-shared' +import { DiscoverEvent, trackEvent } from '@analytics' +import { useBottomSheet } from '@modules/bottom-sheet' +import { FolderNameSheet } from '../../components/FolderNameSheet' +import { useDiscoverFavoritesStore } from '../../store/useDiscoverFavoritesStore' +import { useFolderMenu } from '../useFolderMenu' + +vi.mock('@analytics', async () => ({ + ...(await vi.importActual('@analytics')), + trackEvent: vi.fn(), +})) +vi.mock('@modules/bottom-sheet', async () => ({ + ...(await vi.importActual('@modules/bottom-sheet')), + useBottomSheet: vi.fn(), +})) + +const request = vi.fn() +const K1 = 'https://one.example' +const K2 = 'https://two.example' +const store = () => useDiscoverFavoritesStore.getState() + +/** Queues what each opened sheet resolves with, in order. */ +const sheetsResolve = (...values: unknown[]) => { + for (const value of values) request.mockResolvedValueOnce(value) +} + +describe('useFolderMenu', () => { + let folderId: string + + beforeEach(() => { + vi.clearAllMocks() + request.mockReset() + let next = 0 + vi.mocked(generateUniqueId).mockImplementation(() => `folder-${++next}`) + vi.mocked(useBottomSheet).mockReturnValue({ request } as never) + useDiscoverFavoritesStore.setState({ + favorites: {}, + favoriteOrder: [], + folders: {}, + folderOrder: [], + }) + for (const url of [K1, K2]) { + store().addFavorite({ + url, + name: url, + logo: null, + categories: [], + chains: [], + }) + } + folderId = store().createFolder('Swap') + store().setDappInFolder(folderId, K1, true) + store().setDappInFolder(folderId, K2, true) + }) + + const openMenu = async () => { + const { result } = renderHook(() => useFolderMenu(folderId)) + await act(() => result.current.openMenu()) + } + + it('renames with the name sheet prefilled', async () => { + sheetsResolve('rename', 'New name') + + await openMenu() + + const contents = request.mock.calls[1]?.[0].contents as ReactElement<{ + mode: string + initialName: string + }> + expect(isValidElement(contents) && contents.type).toBe(FolderNameSheet) + expect(contents.props).toMatchObject({ + mode: 'rename', + initialName: 'Swap', + }) + expect(store().folders[folderId]?.name).toBe('New name') + expect(trackEvent).toHaveBeenCalledWith(DiscoverEvent.FolderRename) + }) + + it('saves the rearranged order', async () => { + sheetsResolve('rearrange', [K2]) + + await openMenu() + + expect(store().folders[folderId]?.items).toEqual([K2]) + expect(store().isFavorite(K1)).toBe(true) + expect(trackEvent).toHaveBeenCalledWith(DiscoverEvent.FolderRearrange, { + removed_count: 1, + }) + }) + + it('deletes after confirm, keeping the favorites', async () => { + sheetsResolve('delete', true) + + await openMenu() + + expect(store().folders[folderId]).toBeUndefined() + expect(store().isFavorite(K1)).toBe(true) + expect(store().isFavorite(K2)).toBe(true) + expect(trackEvent).toHaveBeenCalledWith(DiscoverEvent.FolderDelete) + }) + + it.each([ + ['the menu', [undefined]], + ['the name sheet', ['rename', undefined]], + ['the rearrange sheet', ['rearrange', undefined]], + ['the delete confirm', ['delete', undefined]], + ])('does nothing when %s is dismissed', async (_, values) => { + sheetsResolve(...values) + const before = store().folders[folderId] + + await openMenu() + + expect(store().folders[folderId]).toBe(before) + expect(trackEvent).not.toHaveBeenCalled() + }) + + it('is a no-op when the folder is gone before the action', async () => { + request.mockResolvedValueOnce('rename').mockImplementationOnce(() => { + store().deleteFolder(folderId) + return Promise.resolve('New name') + }) + + await openMenu() + + expect(store().folders[folderId]).toBeUndefined() + expect(trackEvent).not.toHaveBeenCalled() + }) +}) diff --git a/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx b/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx new file mode 100644 index 0000000000..8d76717f8a --- /dev/null +++ b/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx @@ -0,0 +1,112 @@ +/* + 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 { useCallback } from 'react' +import { + DiscoverEvent, + AnalyticsMetadataKey as Key, + trackEvent, +} from '@analytics' +import { useBottomSheet } from '@modules/bottom-sheet' +import { DeleteFolderSheet } from '../components/DeleteFolderSheet' +import { + FolderMenuSheet, + type FolderMenuChoice, +} from '../components/FolderMenuSheet' +import { + FolderNameSheet, + type FolderNameSheetResult, +} from '../components/FolderNameSheet' +import { + RearrangeFolderSheet, + type RearrangeFolderSheetResult, +} from '../components/RearrangeFolderSheet' +import { useDiscoverFavoritesStore } from '../store/useDiscoverFavoritesStore' + +export type UseFolderMenuResult = { openMenu: () => Promise } + +const AUTO_SHEET = { + size: 'auto', + enablePanDownToClose: true, + // PWSheetLayout scrolls only without the auto container. + autoCreateContainer: false, +} as const + +const folderOf = (id: string) => + useDiscoverFavoritesStore.getState().folders[id] + +/** + * The folder screen's "…" menu: rename, rearrange or delete. A dismissed sheet + * changes nothing; a folder deleted meanwhile makes the action a no-op. + */ +export const useFolderMenu = (folderId: string): UseFolderMenuResult => { + const { request } = useBottomSheet() + + const rename = useCallback(async () => { + const name = await request({ + contents: ( + + ), + options: AUTO_SHEET, + }) + if (name === undefined || !folderOf(folderId)) return + useDiscoverFavoritesStore.getState().renameFolder(folderId, name) + trackEvent(DiscoverEvent.FolderRename) + }, [request, folderId]) + + const rearrange = useCallback(async () => { + const keys = await request({ + contents: , + options: { + size: 'modal', + enablePanDownToClose: false, + enableContentPanningGesture: false, + autoCreateContainer: false, + }, + }) + const before = folderOf(folderId) + if (keys === undefined || !before) return + const kept = new Set(keys) + const removed = before.items.filter(key => !kept.has(key)).length + useDiscoverFavoritesStore.getState().setFolderItems(folderId, keys) + trackEvent(DiscoverEvent.FolderRearrange, { + [Key.RemovedCount]: removed, + }) + }, [request, folderId]) + + const remove = useCallback(async () => { + const isConfirmed = await request({ + contents: , + options: { size: 'auto', enablePanDownToClose: true }, + }) + if (isConfirmed !== true || !folderOf(folderId)) return + useDiscoverFavoritesStore.getState().deleteFolder(folderId) + trackEvent(DiscoverEvent.FolderDelete) + }, [request, folderId]) + + const openMenu = useCallback(async () => { + const folder = folderOf(folderId) + if (!folder) return + const choice = await request({ + contents: 0} />, + options: AUTO_SHEET, + }) + if (choice === 'rename') await rename() + else if (choice === 'rearrange') await rearrange() + else if (choice === 'delete') await remove() + }, [request, folderId, rename, rearrange, remove]) + + return { openMenu } +} From f74f8a748756f1191ccc444f6281593c778469af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:10:04 +0300 Subject: [PATCH 05/10] feat(discover): open the folder menu from the folder header [PER-2760] --- .../__integration__/discover-folders.spec.tsx | 126 ++++++++++++++++-- .../RearrangeFolderSheet.tsx | 1 + .../DiscoverFolderScreen.tsx | 15 ++- .../useDiscoverFolderScreen.ts | 2 + 4 files changed, 132 insertions(+), 12 deletions(-) diff --git a/apps/mobile/src/__integration__/discover-folders.spec.tsx b/apps/mobile/src/__integration__/discover-folders.spec.tsx index 067a9ae54b..2ce2dab36c 100644 --- a/apps/mobile/src/__integration__/discover-folders.spec.tsx +++ b/apps/mobile/src/__integration__/discover-folders.spec.tsx @@ -13,7 +13,8 @@ // Native Discover favorites in folders: a folder lists its dApps under a // header that shows the folder's own name, unstarring removes a dApp from the // folder and from favorites, All favorites lists newest first, and All folders -// opens a folder. +// opens a folder. The header's "…" menu renames, rearranges and deletes a +// folder without ever removing a favorite. import { beforeEach, describe, expect, it } from 'vitest' import { act, fireEvent, screen, waitFor } from '@testing-library/react' @@ -23,6 +24,7 @@ import { server } from '@test-utils/msw-server' import { render } from '@test-utils/render' import { renderWithNavigation } from '@test-utils/renderWithNavigation' import { headeredScreen } from '@routes/screen-options' +import { BottomSheetManager } from '@modules/bottom-sheet' import { useDiscoverFavoritesStore } from '@modules/discover/store/useDiscoverFavoritesStore' import type { DiscoverDapp } from '@modules/discover/types' @@ -51,25 +53,129 @@ const flush = () => const HeaderedStack = createNativeStackNavigator() -/** The folder screen under the app's real navigation header. */ +/** The folder screen under the app's real navigation header, with the sheet host. */ const renderFolderWithHeader = (folderId: string) => render( - - - , + <> + + + + + , ) +const TINYMAN = 'https://tinyman.example' +const FOLKS = 'https://folks.example' + +/** A folder holding Tinyman then Folks, opened under its header. */ +const openSwapFolder = async () => { + store().addFavorite(dapp('Tinyman')) + store().addFavorite(dapp('Folks')) + const id = store().createFolder('Swap', TINYMAN) + store().setDappInFolder(id, FOLKS, true) + renderFolderWithHeader(id) + await screen.findByTestId('discover_folder_title') + return id +} + +/** `PWIcon` takes `onPress`, which a DOM click does not reach. */ +const pressIcon = (testID: string): void => { + const icon = screen.getByTestId(testID) as unknown as Record< + string, + { onPress: () => void } + > + const propsKey = Object.keys(icon).find(key => + key.startsWith('__reactProps'), + )! + act(() => icon[propsKey]!.onPress()) +} + +const chooseFromMenu = async (choice: string) => { + fireEvent.click(screen.getByTestId('discover_folder_menu_button')) + fireEvent.click(await screen.findByTestId(`discover_folder_menu_${choice}`)) +} + describe('Flow: Discover favorite folders', () => { beforeEach(() => { server.use(mockDiscoverProjects({ response: [] })) store().resetState() }) + it('Given a folder, when it is renamed from its menu, then the header shows the new name', async () => { + const id = await openSwapFolder() + + await chooseFromMenu('rename') + const input = await screen.findByTestId('discover_folder_name_input') + expect((input as HTMLInputElement).value).toBe('Swap') + fireEvent.change(input, { target: { value: ' DEX ' } }) + fireEvent.click(screen.getByTestId('discover_folder_name_submit')) + + await waitFor(() => { + expect( + screen.getByTestId('discover_folder_title').textContent, + ).toBe('DEX') + }) + expect(store().folders[id]?.name).toBe('DEX') + await flush() + }) + + it('Given a folder, when it is deleted from its menu, then it leaves the screen and its dApps stay favorites', async () => { + const id = await openSwapFolder() + + await chooseFromMenu('delete') + fireEvent.click( + await screen.findByTestId('discover_delete_folder_confirm'), + ) + + await waitFor(() => { + expect(store().folders[id]).toBeUndefined() + }) + expect(store().isFavorite(TINYMAN)).toBe(true) + expect(store().isFavorite(FOLKS)).toBe(true) + await flush() + }) + + it('Given a folder with two dApps, when one is taken out in Rearrange and Done is tapped, then only the other is listed and both are still favorites', async () => { + const id = await openSwapFolder() + + await chooseFromMenu('rearrange') + fireEvent.click( + await screen.findByTestId(`discover_rearrange_remove_${TINYMAN}`), + ) + fireEvent.click(screen.getByTestId('discover_rearrange_done')) + + await waitFor(() => { + expect( + screen.queryByTestId(`discover_folder_dapp_${TINYMAN}`), + ).toBeNull() + }) + expect(screen.getByTestId(`discover_folder_dapp_${FOLKS}`)).toBeTruthy() + expect(store().folders[id]?.items).toEqual([FOLKS]) + expect(store().isFavorite(TINYMAN)).toBe(true) + await flush() + }) + + it('Given a folder, when Rearrange is cancelled, then nothing changes', async () => { + const id = await openSwapFolder() + + await chooseFromMenu('rearrange') + fireEvent.click( + await screen.findByTestId(`discover_rearrange_remove_${TINYMAN}`), + ) + pressIcon('discover_rearrange_header-close') + + await waitFor(() => { + expect(screen.queryByTestId('discover_rearrange_done')).toBeNull() + }) + expect(store().folders[id]?.items).toEqual([TINYMAN, FOLKS]) + await flush() + }) + it('Given a folder named web3.apps, then the header shows that name and its dApp count, and its dApps are listed', async () => { store().addFavorite(dapp('Tinyman')) store().addFavorite(dapp('Folks')) diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx index b3c9ee8d65..96947bf06b 100644 --- a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx @@ -122,6 +122,7 @@ export const RearrangeFolderSheet = ({ title={t('discover.folders.rearrange.title')} onClose={sheet.cancel} showClose + testID='discover_rearrange_header' rightAction={ { // The screen's own navigation object: it owns this screen's header options. const navigation = useNavigation() const screen = useDiscoverFolderScreen() + const { openMenu } = useFolderMenu(screen.folderId) const name = screen.folder?.name ?? '' const count = screen.folder?.count ?? 0 @@ -34,8 +36,17 @@ export const DiscoverFolderScreen = () => { count={count} /> ), + headerRight: () => ( + + + + ), }) - }, [navigation, name, count]) + }, [navigation, name, count, openMenu, t]) return ( dapps: DiscoverFavorite[] @@ -61,6 +62,7 @@ export const useDiscoverFolderScreen = (): UseDiscoverFolderScreenResult => { return { ...actions, + folderId, folder: folderRecord ? { id: folderId, From 8670632424881c66facf4a50057d3454a1a60f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:27:54 +0300 Subject: [PATCH 06/10] test(discover): a double tap on the folder menu opens one menu [PER-2760] --- .../__integration__/discover-folders.spec.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/mobile/src/__integration__/discover-folders.spec.tsx b/apps/mobile/src/__integration__/discover-folders.spec.tsx index 2ce2dab36c..cc70b8261f 100644 --- a/apps/mobile/src/__integration__/discover-folders.spec.tsx +++ b/apps/mobile/src/__integration__/discover-folders.spec.tsx @@ -25,6 +25,7 @@ import { render } from '@test-utils/render' import { renderWithNavigation } from '@test-utils/renderWithNavigation' import { headeredScreen } from '@routes/screen-options' import { BottomSheetManager } from '@modules/bottom-sheet' +import { useBottomSheetStore } from '@modules/bottom-sheet/store/bottomSheetStore' import { useDiscoverFavoritesStore } from '@modules/discover/store/useDiscoverFavoritesStore' import type { DiscoverDapp } from '@modules/discover/types' @@ -106,6 +107,21 @@ describe('Flow: Discover favorite folders', () => { store().resetState() }) + it('Given a folder, when its menu button is tapped twice, then one menu opens', async () => { + await openSwapFolder() + + fireEvent.click(screen.getByTestId('discover_folder_menu_button')) + fireEvent.click(screen.getByTestId('discover_folder_menu_button')) + + await screen.findByTestId('discover_folder_menu_rename') + await flush() + // The button's double-press guard keeps the second tap from stacking + // a second menu; the sheet stack is the truth, not the painted DOM. + expect( + useBottomSheetStore.getState().requests.filter(r => r.isVisible), + ).toHaveLength(1) + }) + it('Given a folder, when it is renamed from its menu, then the header shows the new name', async () => { const id = await openSwapFolder() From 365b6f82e81d1342a40f75863d3e37ee4862909a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:29:05 +0300 Subject: [PATCH 07/10] fix(discover): do not hand the menu promise to onPress [PER-2760] --- .../screens/DiscoverFolderScreen/DiscoverFolderScreen.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/DiscoverFolderScreen.tsx b/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/DiscoverFolderScreen.tsx index 29494778bd..4aa29fe0b3 100644 --- a/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/DiscoverFolderScreen.tsx +++ b/apps/mobile/src/modules/discover/screens/DiscoverFolderScreen/DiscoverFolderScreen.tsx @@ -38,7 +38,7 @@ export const DiscoverFolderScreen = () => { ), headerRight: () => ( void openMenu()} testID='discover_folder_menu_button' accessibilityLabel={t('discover.folders.menu.open_label')} > From ebc65b1a8cd554beb8a3b88275c44c0dd8f4c087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:55:16 +0300 Subject: [PATCH 08/10] fix(discover): rearrange by the folder's own keys, share the dApp logo [PER-2760] --- .../discover/components/DappLogo/DappLogo.tsx | 35 ++++++ .../discover/components/DappLogo/index.ts | 14 +++ .../discover/components/DappLogo/styles.ts | 26 ++++ .../discover/components/DappRow/DappRow.tsx | 16 +-- .../discover/components/DappRow/styles.ts | 10 -- .../RearrangeFolderSheet.tsx | 111 ++++++++---------- .../__tests__/useRearrangeFolderSheet.spec.ts | 39 +++++- .../components/RearrangeFolderSheet/styles.ts | 10 -- .../useRearrangeFolderSheet.ts | 46 ++++---- 9 files changed, 184 insertions(+), 123 deletions(-) create mode 100644 apps/mobile/src/modules/discover/components/DappLogo/DappLogo.tsx create mode 100644 apps/mobile/src/modules/discover/components/DappLogo/index.ts create mode 100644 apps/mobile/src/modules/discover/components/DappLogo/styles.ts diff --git a/apps/mobile/src/modules/discover/components/DappLogo/DappLogo.tsx b/apps/mobile/src/modules/discover/components/DappLogo/DappLogo.tsx new file mode 100644 index 0000000000..8ddcf4be85 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogo/DappLogo.tsx @@ -0,0 +1,35 @@ +/* + 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 { PWIcon, PWImage, PWView } from '@components/core' +import type { Nullable } from '@perawallet/wallet-core-shared' +import { useStyles } from './styles' + +export type DappLogoProps = { logo: Nullable } + +/** A dApp's round logo at row size, or a globe when it has none. */ +export const DappLogo = ({ logo }: DappLogoProps) => { + const styles = useStyles() + return logo ? ( + + ) : ( + + + + ) +} diff --git a/apps/mobile/src/modules/discover/components/DappLogo/index.ts b/apps/mobile/src/modules/discover/components/DappLogo/index.ts new file mode 100644 index 0000000000..0af39d4869 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogo/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 { DappLogo } from './DappLogo' +export type { DappLogoProps } from './DappLogo' diff --git a/apps/mobile/src/modules/discover/components/DappLogo/styles.ts b/apps/mobile/src/modules/discover/components/DappLogo/styles.ts new file mode 100644 index 0000000000..f4b868c918 --- /dev/null +++ b/apps/mobile/src/modules/discover/components/DappLogo/styles.ts @@ -0,0 +1,26 @@ +/* + 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' +import { DISCOVER_ROW_LEAD_SIZE } from '../../utils/discoverLayout' + +export const useStyles = makeStyles(theme => ({ + logo: { + width: DISCOVER_ROW_LEAD_SIZE, + height: DISCOVER_ROW_LEAD_SIZE, + borderRadius: theme.borderRadius.full, + overflow: 'hidden', + alignItems: 'center', + justifyContent: 'center', + backgroundColor: theme.colors.layerGray, + }, +})) diff --git a/apps/mobile/src/modules/discover/components/DappRow/DappRow.tsx b/apps/mobile/src/modules/discover/components/DappRow/DappRow.tsx index 850abcc027..673881f479 100644 --- a/apps/mobile/src/modules/discover/components/DappRow/DappRow.tsx +++ b/apps/mobile/src/modules/discover/components/DappRow/DappRow.tsx @@ -15,13 +15,13 @@ import { ProjectVerificationIcon } from '@modules/projects' import { PWButton, PWIcon, - PWImage, PWText, PWTouchableOpacity, PWView, } from '@components/core' import { useLanguage } from '@hooks/useLanguage' import type { DiscoverDapp } from '../../types' +import { DappLogo } from '../DappLogo' import { useStyles } from './styles' export type DappRowProps = { @@ -62,19 +62,7 @@ export const DappRow = ({ onPress={onPress} testID={testID ? `${testID}_open` : undefined} > - {dapp.logo ? ( - - ) : ( - - - - )} + ({ row: { @@ -26,15 +25,6 @@ export const useStyles = makeStyles(theme => ({ alignItems: 'center', gap: theme.spacing.lg, }, - logo: { - width: DISCOVER_ROW_LEAD_SIZE, - height: DISCOVER_ROW_LEAD_SIZE, - borderRadius: theme.borderRadius.full, - overflow: 'hidden', - alignItems: 'center', - justifyContent: 'center', - backgroundColor: theme.colors.layerGray, - }, info: { flex: 1, }, diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx index 96947bf06b..5d84f93b94 100644 --- a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/RearrangeFolderSheet.tsx @@ -19,22 +19,23 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context' import { PWButton, PWIcon, - PWImage, PWText, PWTouchableOpacity, PWView, } from '@components/core' import { SheetHeader } from '@modules/bottom-sheet' import { useLanguage } from '@hooks/useLanguage' -import type { DiscoverFavorite } from '../../types' +import { DappLogo } from '../DappLogo' import { - favoriteKeyOf, useRearrangeFolderSheet, + type RearrangeEntry, } from './useRearrangeFolderSheet' import { useStyles } from './styles' export type RearrangeFolderSheetProps = { folderId: string } +const keyOf = (entry: RearrangeEntry) => entry.key + /** * Reorders a folder's dApps and takes some out; open it with * `useBottomSheet().request`. Done resolves the @@ -50,69 +51,53 @@ export const RearrangeFolderSheet = ({ const { remove, reorder } = sheet const renderItem = useCallback( - ({ item, drag, isActive }: RenderItemParams) => { - const key = favoriteKeyOf(item) - return ( - - - remove(key)} - style={styles.remove} - accessibilityLabel={t( - 'discover.folders.rearrange.remove_label', - { name: item.name }, - )} - testID={`discover_rearrange_remove_${key}`} - > - - - {item.logo ? ( - - ) : ( - - - + ({ item, drag, isActive }: RenderItemParams) => ( + + + remove(item.key)} + style={styles.remove} + accessibilityLabel={t( + 'discover.folders.rearrange.remove_label', + { name: item.dapp.name }, )} - - {item.name} - - - - - - - ) - }, + testID={`discover_rearrange_remove_${item.key}`} + > + + + + + {item.dapp.name} + + + + + + + ), [styles, remove, t], ) const handleDragEnd = useCallback( - ({ data }: { data: DiscoverFavorite[] }) => - reorder(data.map(favoriteKeyOf)), + ({ data }: { data: RearrangeEntry[] }) => reorder(data.map(keyOf)), [reorder], ) @@ -134,9 +119,9 @@ export const RearrangeFolderSheet = ({ } /> useDiscoverFavoritesStore.getState() -const keysOf = (dapps: { url: string }[]) => dapps.map(d => d.url) +const keysOf = (entries: { key: string }[]) => entries.map(e => e.key) describe('useRearrangeFolderSheet', () => { let folderId: string @@ -65,7 +65,7 @@ describe('useRearrangeFolderSheet', () => { useRearrangeFolderSheet({ folderId }), ) - expect(keysOf(result.current.dapps)).toEqual([K1, K2]) + expect(keysOf(result.current.entries)).toEqual([K1, K2]) }) it('reorders the working list', () => { @@ -75,7 +75,7 @@ describe('useRearrangeFolderSheet', () => { act(() => result.current.reorder([K2, K1])) - expect(keysOf(result.current.dapps)).toEqual([K2, K1]) + expect(keysOf(result.current.entries)).toEqual([K2, K1]) expect(store().folders[folderId]?.items).toEqual([K1, K2]) }) @@ -86,7 +86,7 @@ describe('useRearrangeFolderSheet', () => { act(() => result.current.remove(K1)) - expect(keysOf(result.current.dapps)).toEqual([K2]) + expect(keysOf(result.current.entries)).toEqual([K2]) expect(store().folders[folderId]?.items).toEqual([K1, K2]) expect(store().isFavorite(K1)).toBe(true) }) @@ -103,6 +103,37 @@ describe('useRearrangeFolderSheet', () => { expect(resolve).toHaveBeenCalledWith([K2]) }) + it("resolves the folder's own keys, even when a url no longer normalises to its key", () => { + // A stored favorite whose url would not normalise back to its key. + useDiscoverFavoritesStore.setState(state => ({ + favorites: { + ...state.favorites, + 'legacy-key': { + url: 'not a url', + name: 'Legacy', + logo: null, + categories: [], + chains: [], + addedAt: 1, + }, + }, + folders: { + ...state.folders, + [folderId]: { + ...state.folders[folderId]!, + items: ['legacy-key', K1], + }, + }, + })) + const { result } = renderHook(() => + useRearrangeFolderSheet({ folderId }), + ) + + act(() => result.current.done()) + + expect(resolve).toHaveBeenCalledWith(['legacy-key', K1]) + }) + it('dismisses on cancel without touching the store', () => { const { result } = renderHook(() => useRearrangeFolderSheet({ folderId }), diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts index 243e2ed554..327eb583d3 100644 --- a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/styles.ts @@ -11,7 +11,6 @@ */ import { makeStyles } from '@rneui/themed' -import { DISCOVER_ROW_LEAD_SIZE } from '../../utils/discoverLayout' type StyleProps = { bottomInset?: number } @@ -37,15 +36,6 @@ export const useStyles = makeStyles( remove: { padding: theme.spacing.xs, }, - logo: { - width: DISCOVER_ROW_LEAD_SIZE, - height: DISCOVER_ROW_LEAD_SIZE, - borderRadius: theme.borderRadius.full, - overflow: 'hidden', - alignItems: 'center', - justifyContent: 'center', - backgroundColor: theme.colors.layerGray, - }, name: { flex: 1, }, diff --git a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts index dd07d4f969..ded18815b0 100644 --- a/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts +++ b/apps/mobile/src/modules/discover/components/RearrangeFolderSheet/useRearrangeFolderSheet.ts @@ -12,60 +12,62 @@ import { useCallback, useState } from 'react' import { useBottomSheetResult } from '@modules/bottom-sheet' -import { - selectFolderFavorites, - useDiscoverFavoritesStore, -} from '../../store/useDiscoverFavoritesStore' +import { useDiscoverFavoritesStore } from '../../store/useDiscoverFavoritesStore' import type { DiscoverFavorite } from '../../types' -import { normalizeDappUrl } from '../../utils/normalizeDappUrl' /** What the sheet resolves with on Done: the folder's favorite keys, in order. */ export type RearrangeFolderSheetResult = string[] +/** A row of the working list: the folder's own key and its favorite. */ +export type RearrangeEntry = { key: string; dapp: DiscoverFavorite } + export type UseRearrangeFolderSheetParams = { folderId: string } export type UseRearrangeFolderSheetResult = { - dapps: DiscoverFavorite[] + entries: RearrangeEntry[] reorder: (keys: string[]) => void remove: (key: string) => void done: () => void cancel: () => void } -/** A favorite's store key; favorites only hold urls that normalise. */ -export const favoriteKeyOf = (dapp: Pick): string => - normalizeDappUrl(dapp.url) ?? dapp.url +const readEntries = (folderId: string): RearrangeEntry[] => { + const { folders, favorites } = useDiscoverFavoritesStore.getState() + return (folders[folderId]?.items ?? []).flatMap(key => { + const dapp = favorites[key] + return dapp ? [{ key, dapp }] : [] + }) +} /** - * The working list of the Rearrange sheet. Read once at mount and kept local - * until Done, so Cancel or a dismiss changes nothing. + * The working list of the Rearrange sheet. Read once at mount, keyed by the + * folder's own keys, and kept local until Done, so Cancel or a dismiss + * changes nothing. */ export const useRearrangeFolderSheet = ({ folderId, }: UseRearrangeFolderSheetParams): UseRearrangeFolderSheetResult => { const { resolve, dismiss } = useBottomSheetResult() - const [dapps, setDapps] = useState(() => - selectFolderFavorites(useDiscoverFavoritesStore.getState(), folderId), - ) + const [entries, setEntries] = useState(() => readEntries(folderId)) const reorder = useCallback((keys: string[]) => { - setDapps(current => { - const byKey = new Map(current.map(d => [favoriteKeyOf(d), d])) + setEntries(current => { + const byKey = new Map(current.map(entry => [entry.key, entry])) return keys.flatMap(key => { - const dapp = byKey.get(key) - return dapp ? [dapp] : [] + const entry = byKey.get(key) + return entry ? [entry] : [] }) }) }, []) const remove = useCallback((key: string) => { - setDapps(current => current.filter(d => favoriteKeyOf(d) !== key)) + setEntries(current => current.filter(entry => entry.key !== key)) }, []) const done = useCallback( - () => resolve(dapps.map(favoriteKeyOf)), - [dapps, resolve], + () => resolve(entries.map(entry => entry.key)), + [entries, resolve], ) const cancel = useCallback(() => dismiss(), [dismiss]) - return { dapps, reorder, remove, done, cancel } + return { entries, reorder, remove, done, cancel } } From fcf65ba8b15ca2be1311a432345af196835797a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 15:55:16 +0300 Subject: [PATCH 09/10] refactor(discover): share folder sheet options and the pressIcon test helper [PER-2760] --- .../add-account-default-chains.spec.tsx | 21 ++------------ .../__integration__/discover-folders.spec.tsx | 13 +-------- .../discover/hooks/useCreateFolder.tsx | 8 ++---- .../modules/discover/hooks/useFolderMenu.tsx | 12 ++------ .../hooks/useToggleDiscoverFavorite.tsx | 8 ++---- .../modules/discover/utils/sheetOptions.ts | 22 +++++++++++++++ apps/mobile/src/test-utils/pressIcon.ts | 28 +++++++++++++++++++ 7 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 apps/mobile/src/modules/discover/utils/sheetOptions.ts create mode 100644 apps/mobile/src/test-utils/pressIcon.ts diff --git a/apps/mobile/src/__integration__/add-account-default-chains.spec.tsx b/apps/mobile/src/__integration__/add-account-default-chains.spec.tsx index 43b86ee174..8b6e2a6a28 100644 --- a/apps/mobile/src/__integration__/add-account-default-chains.spec.tsx +++ b/apps/mobile/src/__integration__/add-account-default-chains.spec.tsx @@ -20,13 +20,7 @@ import { it, vi, } from 'vitest' -import { - act, - fireEvent, - renderHook, - screen, - waitFor, -} from '@testing-library/react' +import { fireEvent, renderHook, screen, waitFor } from '@testing-library/react' import { http, type RequestHandler } from 'msw' import { @@ -51,6 +45,7 @@ import { teardownTestDatabase, } from '@test-utils/database-setup' import { server } from '@test-utils/msw-server' +import { pressIcon } from '@test-utils/pressIcon' import { renderWithNavigation } from '@test-utils/renderWithNavigation' import { isElementDisabled } from '@test-utils/rnw' import { useOnboardingStore } from '@modules/onboarding/hooks/useOnboardingStore' @@ -195,18 +190,6 @@ const theOnlyAccount = (): WalletAccount => { return accounts[0]! } -// A sheet header's close icon is an svg whose onPress jsdom never dispatches. -const pressIcon = (testID: string): void => { - const icon = screen.getByTestId(testID) as unknown as Record< - string, - { onPress: () => void } - > - const propsKey = Object.keys(icon).find(key => - key.startsWith('__reactProps'), - )! - act(() => icon[propsKey]!.onPress()) -} - // Imports the recovery phrase active on Ethereum and Base, up to naming it, // once discovery has added Base. const importRecoveryPhraseUntilNaming = async (): Promise => { diff --git a/apps/mobile/src/__integration__/discover-folders.spec.tsx b/apps/mobile/src/__integration__/discover-folders.spec.tsx index cc70b8261f..6216e08a8f 100644 --- a/apps/mobile/src/__integration__/discover-folders.spec.tsx +++ b/apps/mobile/src/__integration__/discover-folders.spec.tsx @@ -21,6 +21,7 @@ import { act, fireEvent, screen, waitFor } 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' +import { pressIcon } from '@test-utils/pressIcon' import { render } from '@test-utils/render' import { renderWithNavigation } from '@test-utils/renderWithNavigation' import { headeredScreen } from '@routes/screen-options' @@ -84,18 +85,6 @@ const openSwapFolder = async () => { return id } -/** `PWIcon` takes `onPress`, which a DOM click does not reach. */ -const pressIcon = (testID: string): void => { - const icon = screen.getByTestId(testID) as unknown as Record< - string, - { onPress: () => void } - > - const propsKey = Object.keys(icon).find(key => - key.startsWith('__reactProps'), - )! - act(() => icon[propsKey]!.onPress()) -} - const chooseFromMenu = async (choice: string) => { fireEvent.click(screen.getByTestId('discover_folder_menu_button')) fireEvent.click(await screen.findByTestId(`discover_folder_menu_${choice}`)) diff --git a/apps/mobile/src/modules/discover/hooks/useCreateFolder.tsx b/apps/mobile/src/modules/discover/hooks/useCreateFolder.tsx index ae79a50850..ff8823f7e1 100644 --- a/apps/mobile/src/modules/discover/hooks/useCreateFolder.tsx +++ b/apps/mobile/src/modules/discover/hooks/useCreateFolder.tsx @@ -19,6 +19,7 @@ import { type FolderNameSheetResult, } from '../components/FolderNameSheet' import { useDiscoverFavoritesStore } from '../store/useDiscoverFavoritesStore' +import { SCROLLING_AUTO_SHEET_OPTIONS } from '../utils/sheetOptions' /** * Asks for a folder name and creates the folder (holding `initialUrl` when given). @@ -33,12 +34,7 @@ export const useCreateFolder = (): (( async (initialUrl?: string) => { const name = await request({ contents: , - options: { - size: 'auto', - enablePanDownToClose: true, - // PWSheetLayout scrolls only without the auto container. - autoCreateContainer: false, - }, + options: SCROLLING_AUTO_SHEET_OPTIONS, }) if (name === undefined) return undefined const id = createFolder(name, initialUrl) diff --git a/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx b/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx index 8d76717f8a..2fc659dadb 100644 --- a/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx +++ b/apps/mobile/src/modules/discover/hooks/useFolderMenu.tsx @@ -31,16 +31,10 @@ import { type RearrangeFolderSheetResult, } from '../components/RearrangeFolderSheet' import { useDiscoverFavoritesStore } from '../store/useDiscoverFavoritesStore' +import { SCROLLING_AUTO_SHEET_OPTIONS } from '../utils/sheetOptions' export type UseFolderMenuResult = { openMenu: () => Promise } -const AUTO_SHEET = { - size: 'auto', - enablePanDownToClose: true, - // PWSheetLayout scrolls only without the auto container. - autoCreateContainer: false, -} as const - const folderOf = (id: string) => useDiscoverFavoritesStore.getState().folders[id] @@ -59,7 +53,7 @@ export const useFolderMenu = (folderId: string): UseFolderMenuResult => { initialName={folderOf(folderId)?.name} /> ), - options: AUTO_SHEET, + options: SCROLLING_AUTO_SHEET_OPTIONS, }) if (name === undefined || !folderOf(folderId)) return useDiscoverFavoritesStore.getState().renameFolder(folderId, name) @@ -101,7 +95,7 @@ export const useFolderMenu = (folderId: string): UseFolderMenuResult => { if (!folder) return const choice = await request({ contents: 0} />, - options: AUTO_SHEET, + options: SCROLLING_AUTO_SHEET_OPTIONS, }) if (choice === 'rename') await rename() else if (choice === 'rearrange') await rearrange() diff --git a/apps/mobile/src/modules/discover/hooks/useToggleDiscoverFavorite.tsx b/apps/mobile/src/modules/discover/hooks/useToggleDiscoverFavorite.tsx index 9b240a9665..75eadf3422 100644 --- a/apps/mobile/src/modules/discover/hooks/useToggleDiscoverFavorite.tsx +++ b/apps/mobile/src/modules/discover/hooks/useToggleDiscoverFavorite.tsx @@ -22,6 +22,7 @@ import { AddToFolderSheet } from '../components/AddToFolderSheet' import { useDiscoverFavoritesStore } from '../store/useDiscoverFavoritesStore' import type { DiscoverDapp } from '../types' import { dappEventPayload } from '../utils/discoverAnalytics' +import { SCROLLING_AUTO_SHEET_OPTIONS } from '../utils/sheetOptions' // lanekeep-ignore-next-line pera/no-numeric-sizes reason: a duration in ms, not a size const FOLDER_TOAST_DURATION_MS = 5000 @@ -61,12 +62,7 @@ export const useToggleDiscoverFavorite = } void request({ contents: , - options: { - size: 'auto', - enablePanDownToClose: true, - // PWSheetLayout scrolls only without the auto container. - autoCreateContainer: false, - }, + options: SCROLLING_AUTO_SHEET_OPTIONS, }) }, }, diff --git a/apps/mobile/src/modules/discover/utils/sheetOptions.ts b/apps/mobile/src/modules/discover/utils/sheetOptions.ts new file mode 100644 index 0000000000..88275c7d8c --- /dev/null +++ b/apps/mobile/src/modules/discover/utils/sheetOptions.ts @@ -0,0 +1,22 @@ +/* + 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 + */ + +/** + * How the folder sheets built on `PWSheetLayout` open: sized to their content, + * closable by pan-down, and without gorhom's auto container, which would stop + * `PWSheetLayout` from scrolling and pinning its footer. + */ +export const SCROLLING_AUTO_SHEET_OPTIONS = { + size: 'auto', + enablePanDownToClose: true, + autoCreateContainer: false, +} as const diff --git a/apps/mobile/src/test-utils/pressIcon.ts b/apps/mobile/src/test-utils/pressIcon.ts new file mode 100644 index 0000000000..0f2bb18b6f --- /dev/null +++ b/apps/mobile/src/test-utils/pressIcon.ts @@ -0,0 +1,28 @@ +/* + 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 { act, screen } from '@testing-library/react' + +/** + * Presses a `PWIcon` that takes `onPress` (a sheet header's close icon, say): + * it renders an svg, and a DOM click never reaches its `onPress`. + */ +export const pressIcon = (testID: string): void => { + const icon = screen.getByTestId(testID) as unknown as Record< + string, + { onPress: () => void } + > + const propsKey = Object.keys(icon).find(key => + key.startsWith('__reactProps'), + )! + act(() => icon[propsKey]!.onPress()) +} From b75903992cdc7c26fc026f8936a25d1f4be0f197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Mon, 16 Nov 2026 17:04:15 +0300 Subject: [PATCH 10/10] fix(discover): keep reordered favorites in view [PER-2760] --- .../discover/components/FavoriteDappList/FavoriteDappList.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/mobile/src/modules/discover/components/FavoriteDappList/FavoriteDappList.tsx b/apps/mobile/src/modules/discover/components/FavoriteDappList/FavoriteDappList.tsx index 921b37146b..b880497a71 100644 --- a/apps/mobile/src/modules/discover/components/FavoriteDappList/FavoriteDappList.tsx +++ b/apps/mobile/src/modules/discover/components/FavoriteDappList/FavoriteDappList.tsx @@ -39,6 +39,10 @@ export const FavoriteDappList = ({ return ( dapp.url} contentContainerStyle={styles.list}