mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
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.
This commit is contained in:
@@ -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$/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 {
|
||||
<button type="button" onClick={onSelectLibrary} className={`col-row-btn${libraryActive ? ' on' : ''}`} aria-current={libraryActive ? 'page' : undefined}>
|
||||
<span className="ico tile"><Globe2 size={15} /></span>
|
||||
<span className="nm">{t('collections.library.title')}</span>
|
||||
{libraryCount != null && libraryCount > 0 && <span className="ct">{libraryCount}</span>}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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<LibraryCounts | null>(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;
|
||||
}
|
||||
@@ -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({
|
||||
>
|
||||
<Globe2 size={15} strokeWidth={2} className="flex-none" />
|
||||
<span className="min-w-0 flex-1 truncate">{t('collections.library.title')}</span>
|
||||
{libraryCount != null && libraryCount > 0 && (
|
||||
<span className="font-geist text-[length:calc(11px*var(--fs-scale-caption,1))] font-bold text-m-faint">
|
||||
{libraryCount}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
|
||||
@@ -167,6 +167,7 @@ export default function MCollections() {
|
||||
lists={navLists}
|
||||
library={c.isLibrary}
|
||||
onSelectLibrary={() => selectList('library')}
|
||||
libraryCount={c.libraryCount?.all ?? null}
|
||||
allSaved={!c.isLibrary && c.isAllSaved}
|
||||
activeId={c.activeId}
|
||||
onSelect={selectList}
|
||||
|
||||
@@ -207,7 +207,8 @@ function CollectionsPageDesktop(): React.ReactElement {
|
||||
sharedLists={c.sharedLists}
|
||||
activeId={c.activeId}
|
||||
libraryActive={c.isLibrary}
|
||||
savedCount={c.collections.reduce((n, l) => n + (l.place_count ?? 0), 0)}
|
||||
savedCount={c.libraryCount?.saved ?? c.collections.reduce((n, l) => n + (l.place_count ?? 0), 0)}
|
||||
libraryCount={c.libraryCount?.all ?? null}
|
||||
onSelectLibrary={() => c.handleSelectList('library')}
|
||||
incomingInvites={c.incomingInvites}
|
||||
onSelect={c.handleSelectList}
|
||||
|
||||
@@ -12,9 +12,11 @@ import { downloadCollectionFile, downloadCollectionGpx, downloadCollectionKml, t
|
||||
import { getApiErrorMessage } from '../../types'
|
||||
import { PLACES_SAVED, placesPath } from '../../utils/placesRoutes'
|
||||
import { carryKey, type CopyCarry, type CopyCarryOptions } from '../../components/Collections/useTripCopyPicker'
|
||||
import { useLibraryCount } from '../../components/Collections/useLibraryCount'
|
||||
import { canAddToCollection, canRemoveFromCollection, collectionRoleOf } from '../../utils/collectionAccess'
|
||||
import { addListener, removeListener } from '../../api/websocket'
|
||||
import { useCollectionStore, ALL_SAVED } from '../../store/collectionStore'
|
||||
import { distinctPlaces } from '../../store/collectionUnion'
|
||||
import type { ActiveCollectionId } from '../../store/collectionStore'
|
||||
import { categoriesApi } from '../../api/client'
|
||||
import type { Collection, CollectionStatus, CollectionFile } from '@trek/shared'
|
||||
@@ -97,6 +99,8 @@ export function useCollections() {
|
||||
// that fails leaves it off, so an offline page never drops the route.
|
||||
const [listsLoaded, setListsLoaded] = useState(false)
|
||||
useEffect(() => { loadAll().then(() => setListsLoaded(true), () => setListsLoaded(false)) }, [])
|
||||
// All places and All saved in the rail, each place once; read again when a list changes.
|
||||
const libraryCount = useLibraryCount(collections)
|
||||
|
||||
// Central admin-defined categories, for assigning a category to a saved place.
|
||||
const [categories, setCategories] = useState<Category[]>([])
|
||||
@@ -223,6 +227,8 @@ export function useCollections() {
|
||||
[collections, activeId],
|
||||
)
|
||||
const isAllSaved = activeId === ALL_SAVED
|
||||
// All saved shows a place kept in several lists once; a single list shows its own.
|
||||
const shownPlaces = useMemo(() => (isAllSaved ? distinctPlaces(places) : places), [isAllSaved, places])
|
||||
const isOwner = activeCollection?.is_owner ?? false
|
||||
|
||||
// The current user's permission on the active list drives what the UI offers.
|
||||
@@ -255,8 +261,8 @@ export function useCollections() {
|
||||
|
||||
// Labels are per-collection, so never apply them on the "All saved" union.
|
||||
const visiblePlaces = useMemo(
|
||||
() => sortPlaces(filterPlaces(places, statusFilter, search, categoryFilter, isAllSaved ? [] : labelFilter, ratingFilter), sortMode),
|
||||
[places, statusFilter, search, categoryFilter, isAllSaved, labelFilter, ratingFilter, sortMode],
|
||||
() => sortPlaces(filterPlaces(shownPlaces, statusFilter, search, categoryFilter, isAllSaved ? [] : labelFilter, ratingFilter), sortMode),
|
||||
[shownPlaces, statusFilter, search, categoryFilter, isAllSaved, labelFilter, ratingFilter, sortMode],
|
||||
)
|
||||
// The list's own order can be dragged (#1457) only while every place of it is on
|
||||
// screen in that order: a single list, its own sort, no search or filter hiding
|
||||
@@ -269,9 +275,9 @@ export function useCollections() {
|
||||
reorderPlaces(orderedIds).catch((err: unknown) => toast.error(getApiErrorMessage(err, t('common.error'))))
|
||||
}, [reorderPlaces, toast, t])
|
||||
// Categories actually present in this list, for the category filter dropdown.
|
||||
const categoryOptions = useMemo(() => presentCategories(places), [places])
|
||||
const categoryOptions = useMemo(() => presentCategories(shownPlaces), [shownPlaces])
|
||||
// The active list's labels (with per-label counts) for the filter + manager.
|
||||
const labelOptions = useMemo(() => presentLabels(labels, places), [labels, places])
|
||||
const labelOptions = useMemo(() => presentLabels(labels, shownPlaces), [labels, shownPlaces])
|
||||
// Stable reference so the map doesn't tear down + rebuild every marker on each
|
||||
// unrelated re-render (which would swallow marker clicks mid-rebuild).
|
||||
const mappable = useMemo(() => mappablePlaces(visiblePlaces), [visiblePlaces])
|
||||
@@ -279,8 +285,8 @@ export function useCollections() {
|
||||
// hangs off this rather than off `mappable`: a label with no places left the
|
||||
// filtered set empty, which tore the map out and reflowed the page to full
|
||||
// width. The filter should empty the map, not remove it.
|
||||
const hasMappable = useMemo(() => mappablePlaces(places).length > 0, [places])
|
||||
const counts = useMemo(() => statusCounts(places), [places])
|
||||
const hasMappable = useMemo(() => mappablePlaces(shownPlaces).length > 0, [shownPlaces])
|
||||
const counts = useMemo(() => statusCounts(shownPlaces), [shownPlaces])
|
||||
|
||||
// ── Handlers ────────────────────────────────────────────────────────
|
||||
const handleSelectList = useCallback((id: ActiveCollectionId | 'library') => {
|
||||
@@ -591,11 +597,11 @@ export function useCollections() {
|
||||
listColRef: listCol.ref, listColRect: listCol.rect, mapColRef: mapCol.ref, mapColRect: mapCol.rect, categories,
|
||||
mapLocked, toggleMapLocked,
|
||||
// store data
|
||||
collections, ownedLists, sharedLists, activeCollection, isAllSaved, isOwner,
|
||||
collections, ownedLists, sharedLists, activeCollection, isAllSaved, isOwner, libraryCount,
|
||||
copyCarryOptions,
|
||||
myRole, canEdit, canDelete, reorderEnabled, reorderBlocked, commitOrder,
|
||||
canShare, shareMemberCount,
|
||||
activeId, places, visiblePlaces, mappable, hasMappable, members, incomingInvites, routeInvite, orderedInvites, counts,
|
||||
activeId, places: shownPlaces, visiblePlaces, mappable, hasMappable, members, incomingInvites, routeInvite, orderedInvites, counts,
|
||||
view, statusFilter, categoryFilter, categoryOptions, ratingFilter, sortMode, search, selectedPlaceId, selectMode, selectedIds,
|
||||
labels, labelFilter, labelOptions,
|
||||
loading, placesLoading,
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// FE-STORE-COLUNION-001 to FE-STORE-COLUNION-002: the places of several lists, each place once.
|
||||
import type { CollectionPlace } from '@trek/shared';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { distinctPlaces } from './collectionUnion';
|
||||
|
||||
const saved = (id: number, over: Partial<CollectionPlace> = {}): CollectionPlace => ({
|
||||
id,
|
||||
collection_id: 1,
|
||||
name: `Place ${id}`,
|
||||
status: 'idea',
|
||||
...over,
|
||||
});
|
||||
|
||||
describe('distinctPlaces', () => {
|
||||
it('FE-STORE-COLUNION-001: copies saved from one trip place are one place, the first one read stands for it', () => {
|
||||
const tower = saved(9, { collection_id: 1, name: 'Belem Tower', source_trip_id: 1, source_place_id: 1 });
|
||||
const again = saved(10, { collection_id: 2, name: 'Belem Tower', source_trip_id: 1, source_place_id: 1 });
|
||||
const other = saved(11, { collection_id: 2, source_trip_id: 1, source_place_id: 2 });
|
||||
expect(distinctPlaces([tower, again, other])).toEqual([tower, other]);
|
||||
});
|
||||
|
||||
it('FE-STORE-COLUNION-002: saved places of no trip stay apart, even under one name', () => {
|
||||
const a = saved(1, { name: 'Senso-ji' });
|
||||
const b = saved(2, { collection_id: 2, name: 'Senso-ji' });
|
||||
expect(distinctPlaces([a, b])).toEqual([a, b]);
|
||||
expect(distinctPlaces([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -32,3 +32,19 @@ export async function loadUnionPlaces(ids: number[]): Promise<CollectionPlace[]>
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* The places of several lists with each place once, for the views over all of
|
||||
* them (All saved, the picker's All lists): copies saved from the same trip
|
||||
* place are one place, the way All places groups them, and the first one read
|
||||
* stands for it. Every other saved place stays, even with the same name.
|
||||
*/
|
||||
export function distinctPlaces(places: CollectionPlace[]): CollectionPlace[] {
|
||||
const seen = new Set<string>();
|
||||
return places.filter((p) => {
|
||||
const key = p.source_place_id != null ? `trip:${p.source_place_id}` : `list:${p.id}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -181,6 +181,43 @@ describe('section lists beside the page', () => {
|
||||
expect(onSelect).toHaveBeenNthCalledWith(2, 1);
|
||||
expect(onNewList).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MOB-PANES-018: All places above the lists, with how many places it holds once that is known', () => {
|
||||
const onSelectLibrary = vi.fn();
|
||||
const { rerender } = render(
|
||||
<MCollListNav
|
||||
lists={[]}
|
||||
library
|
||||
onSelectLibrary={onSelectLibrary}
|
||||
libraryCount={7}
|
||||
allSaved={false}
|
||||
activeId={null}
|
||||
onSelect={vi.fn()}
|
||||
onNewList={vi.fn()}
|
||||
t={echo}
|
||||
/>
|
||||
);
|
||||
const all = screen.getByRole('button', { name: /collections.library.title/ });
|
||||
expect(all).toHaveAttribute('aria-current', 'page');
|
||||
expect(all).toHaveTextContent('7');
|
||||
fireEvent.click(all);
|
||||
expect(onSelectLibrary).toHaveBeenCalled();
|
||||
rerender(
|
||||
<MCollListNav
|
||||
lists={[]}
|
||||
onSelectLibrary={onSelectLibrary}
|
||||
libraryCount={null}
|
||||
allSaved={false}
|
||||
activeId={null}
|
||||
onSelect={vi.fn()}
|
||||
onNewList={vi.fn()}
|
||||
t={echo}
|
||||
/>
|
||||
);
|
||||
expect(screen.getByRole('button', { name: /collections.library.title/ })).toHaveTextContent(
|
||||
/^collections.library.title$/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('list tabs on a wide window', () => {
|
||||
|
||||
Reference in New Issue
Block a user