From 2204e851fab6db64d8163e4ba641fd2fde37c855 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 30 Nov 2026 13:09:01 +0100 Subject: [PATCH] feat(places): All saved shows each place once, All places shows its count A place saved into several lists from the same trip place showed once per list in All saved and counted that often, while All places groups the copies as one place. All saved now shows such a place once, the first copy read standing for it, and the rail counts All saved and All places the way All places groups them, on the desktop and the phone. --- .../components/Collections/ListsRail.test.tsx | 13 ++++++- .../src/components/Collections/ListsRail.tsx | 5 ++- .../Collections/useLibraryCount.test.ts | 37 +++++++++++++++++++ .../components/Collections/useLibraryCount.ts | 33 +++++++++++++++++ .../screens/collections/MCollListNav.tsx | 8 ++++ .../screens/collections/MCollections.tsx | 1 + client/src/pages/CollectionsPage.tsx | 3 +- .../src/pages/collections/useCollections.ts | 22 +++++++---- client/src/store/collectionUnion.test.ts | 29 +++++++++++++++ client/src/store/collectionUnion.ts | 16 ++++++++ .../unit/mobile/largeScreenPanes.test.tsx | 37 +++++++++++++++++++ 11 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 client/src/components/Collections/useLibraryCount.test.ts create mode 100644 client/src/components/Collections/useLibraryCount.ts create mode 100644 client/src/store/collectionUnion.test.ts diff --git a/client/src/components/Collections/ListsRail.test.tsx b/client/src/components/Collections/ListsRail.test.tsx index 58bf6f1ef..891378789 100644 --- a/client/src/components/Collections/ListsRail.test.tsx +++ b/client/src/components/Collections/ListsRail.test.tsx @@ -1,4 +1,4 @@ -// FE-COMP-LISTSRAIL-001 to FE-COMP-LISTSRAIL-010 +// FE-COMP-LISTSRAIL-001 to FE-COMP-LISTSRAIL-013 import React from 'react'; import { render, screen, within } from '../../../tests/helpers/render'; import userEvent from '@testing-library/user-event'; @@ -142,4 +142,15 @@ describe('ListsRail', () => { expect(screen.getByRole('button', { name: 'New list' })).toHaveTextContent('New list'); expect(importBtn).toHaveTextContent(''); }); + + it('FE-COMP-LISTSRAIL-012: All places shows how many places it holds once that is known', () => { + renderRail({ onSelectLibrary: vi.fn(), libraryCount: 7, savedCount: 8 }); + expect(screen.getByRole('button', { name: /All places/ })).toHaveTextContent('All places7'); + expect(screen.getByRole('button', { name: /All saved/ })).toHaveTextContent('All saved8'); + }); + + it('FE-COMP-LISTSRAIL-013: without a known count All places shows no number', () => { + renderRail({ onSelectLibrary: vi.fn(), libraryCount: null }); + expect(screen.getByRole('button', { name: /All places/ })).toHaveTextContent(/^All places$/); + }); }); diff --git a/client/src/components/Collections/ListsRail.tsx b/client/src/components/Collections/ListsRail.tsx index a220467fb..5e951b5eb 100644 --- a/client/src/components/Collections/ListsRail.tsx +++ b/client/src/components/Collections/ListsRail.tsx @@ -14,6 +14,8 @@ interface ListsRailProps { activeId: ActiveCollectionId /** How many places the lists hold together, beside "All saved". */ savedCount?: number + /** How many places All places holds, each once; null while unknown. */ + libraryCount?: number | null /** The overview of every place is open (PLC-07); it sits above the lists. */ libraryActive?: boolean onSelectLibrary?: () => void @@ -49,7 +51,7 @@ function ListRow({ list, active, onSelect }: { list: Collection; active: boolean */ export default function ListsRail(props: ListsRailProps): React.ReactElement { const { - ownedLists, sharedLists, activeId, incomingInvites, libraryActive = false, onSelectLibrary, savedCount = 0, + ownedLists, sharedLists, activeId, incomingInvites, libraryActive = false, onSelectLibrary, savedCount = 0, libraryCount = null, onSelect, onNewList, onImportList, onAcceptInvite, onDeclineInvite, t, } = props @@ -78,6 +80,7 @@ export default function ListsRail(props: ListsRailProps): React.ReactElement { )} diff --git a/client/src/components/Collections/useLibraryCount.test.ts b/client/src/components/Collections/useLibraryCount.test.ts new file mode 100644 index 000000000..e149aa897 --- /dev/null +++ b/client/src/components/Collections/useLibraryCount.test.ts @@ -0,0 +1,37 @@ +// FE-COMP-LIBCOUNT-001 to FE-COMP-LIBCOUNT-002: how many places All places holds, for its row beside the lists. +import { renderHook, waitFor } from '@testing-library/react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { libraryEntry, libraryPage } from '../../../tests/helpers/placesLibrary'; +import { placesLibraryRepo } from '../../repo/placesLibraryRepo'; +import { useLibraryCount } from './useLibraryCount'; + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('useLibraryCount', () => { + it('FE-COMP-LIBCOUNT-001: reads the counts with one entry, and again when the lists change', async () => { + const list = vi + .spyOn(placesLibraryRepo, 'list') + .mockResolvedValueOnce(libraryPage([libraryEntry()], { counts: { all: 7, trip: 2, list: 6 } })) + .mockResolvedValueOnce(libraryPage([libraryEntry()], { counts: { all: 8, trip: 2, list: 7 } })); + const first = [{ id: 1 }]; + const { result, rerender } = renderHook(({ lists }) => useLibraryCount(lists), { initialProps: { lists: first } }); + expect(result.current).toBeNull(); + await waitFor(() => expect(result.current).toEqual({ all: 7, saved: 6 })); + expect(list).toHaveBeenCalledWith({ limit: 1 }, expect.any(AbortSignal)); + + rerender({ lists: first }); + expect(list).toHaveBeenCalledTimes(1); + rerender({ lists: [{ id: 1 }, { id: 2 }] }); + await waitFor(() => expect(result.current).toEqual({ all: 8, saved: 7 })); + }); + + it('FE-COMP-LIBCOUNT-002: a refused read shows no number', async () => { + vi.spyOn(placesLibraryRepo, 'list').mockRejectedValue(new Error('offline')); + const { result } = renderHook(() => useLibraryCount([])); + await waitFor(() => expect(placesLibraryRepo.list).toHaveBeenCalled()); + expect(result.current).toBeNull(); + }); +}); diff --git a/client/src/components/Collections/useLibraryCount.ts b/client/src/components/Collections/useLibraryCount.ts new file mode 100644 index 000000000..01cb09f05 --- /dev/null +++ b/client/src/components/Collections/useLibraryCount.ts @@ -0,0 +1,33 @@ +import { useEffect, useState } from 'react'; + +import { placesLibraryRepo } from '../../repo/placesLibraryRepo'; + +/** How many places All places holds, and how many of them are saved in a list; each place once. */ +export interface LibraryCounts { + all: number; + saved: number; +} + +/** + * The counts beside All places and All saved in the rail: each place once, + * however many lists and trips keep it. Read again whenever `lists` changes + * (a place saved, moved or deleted there); null until they are known, and + * offline, where All places cannot be read. + */ +export function useLibraryCount(lists: unknown): LibraryCounts | null { + const [count, setCount] = useState(null); + + useEffect(() => { + const ctrl = new AbortController(); + placesLibraryRepo + .list({ limit: 1 }, ctrl.signal) + .then((page) => setCount({ all: page.counts.all, saved: page.counts.list })) + .catch(() => { + // Offline or refused: the row shows no number rather than a stale or a wrong one. + if (!ctrl.signal.aborted) setCount(null); + }); + return () => ctrl.abort(); + }, [lists]); + + return count; +} diff --git a/client/src/mobile/screens/collections/MCollListNav.tsx b/client/src/mobile/screens/collections/MCollListNav.tsx index 0450fc4ba..149a2d590 100644 --- a/client/src/mobile/screens/collections/MCollListNav.tsx +++ b/client/src/mobile/screens/collections/MCollListNav.tsx @@ -24,6 +24,8 @@ interface MCollListNavProps { /** "All places" (PLC-07) is open; it sits above the saved places. */ library?: boolean; onSelectLibrary?: () => void; + /** How many places All places holds, each once; null while unknown. */ + libraryCount?: number | null; allSaved: boolean; activeId: number | typeof ALL_SAVED | null; onSelect: (id: number | typeof ALL_SAVED) => void; @@ -41,6 +43,7 @@ export default function MCollListNav({ lists, library = false, onSelectLibrary, + libraryCount = null, allSaved, activeId, onSelect, @@ -60,6 +63,11 @@ export default function MCollListNav({ > {t('collections.library.title')} + {libraryCount != null && libraryCount > 0 && ( + + {libraryCount} + + )} )}