mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
feat(places): one place view for All places and the lists
The desktop dialog and the phone sheet of a place show its overview, notes, tours and bookings, photos and files and its history in tabs, with the tags of each list and trip, a markdown notes editor, uploads and the place picture. The rating bar of All places is the rating row the lists show, with its voters, and votes once for every copy. All places filters by a lowest rating and sorts by it, and the edit form changes the first copy the reader may edit, which the server carries to the others.
This commit is contained in:
@@ -84,10 +84,17 @@ function useLibraryFilters() {
|
||||
const [categoryId, setCategoryId] = useState<number | 'all'>('all');
|
||||
const [sort, setSort] = useState<PlacesLibrarySort>('name');
|
||||
const [status, setStatus] = useState<CollectionStatus | 'all'>('all');
|
||||
// PLC-21: the reader's own tags, any of which an entry must carry.
|
||||
const [tagIds, setTagIds] = useState<number[]>([]);
|
||||
const toggleTag = useCallback(
|
||||
(id: number) => setTagIds((ids) => (ids.includes(id) ? ids.filter((x) => x !== id) : [...ids, id])),
|
||||
// At least this many stars on the average of the votes, as the lists filter; unrated places drop out.
|
||||
const [minRating, setMinRating] = useState<number | 'all'>('all');
|
||||
// PLC-21: tag names of the lists and trips, any of which an entry must carry, whatever their case.
|
||||
const [tagNames, setTagNames] = useState<string[]>([]);
|
||||
const toggleTagName = useCallback(
|
||||
(name: string) =>
|
||||
setTagNames((names) => {
|
||||
const key = name.trim().toLowerCase();
|
||||
const kept = names.filter((n) => n.trim().toLowerCase() !== key);
|
||||
return kept.length < names.length || key === '' ? kept : [...names, name.trim()];
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
@@ -103,20 +110,28 @@ function useLibraryFilters() {
|
||||
...(kind !== 'all' ? { kind } : {}),
|
||||
...(categoryId !== 'all' ? { category_id: categoryId } : {}),
|
||||
...(status !== 'all' ? { status } : {}),
|
||||
...(tagIds.length > 0 ? { tag_ids: tagIds } : {}),
|
||||
...(tagNames.length > 0 ? { tags: tagNames } : {}),
|
||||
...(minRating !== 'all' ? { min_rating: minRating } : {}),
|
||||
sort,
|
||||
limit: PAGE,
|
||||
}),
|
||||
[query, kind, categoryId, status, tagIds, sort]
|
||||
[query, kind, categoryId, status, tagNames, minRating, sort]
|
||||
);
|
||||
const filtered = query !== '' || kind !== 'all' || categoryId !== 'all' || status !== 'all' || tagIds.length > 0;
|
||||
const filtered =
|
||||
query !== '' ||
|
||||
kind !== 'all' ||
|
||||
categoryId !== 'all' ||
|
||||
status !== 'all' ||
|
||||
tagNames.length > 0 ||
|
||||
minRating !== 'all';
|
||||
const resetFilters = useCallback(() => {
|
||||
setSearch('');
|
||||
setQuery('');
|
||||
setKind('all');
|
||||
setCategoryId('all');
|
||||
setStatus('all');
|
||||
setTagIds([]);
|
||||
setTagNames([]);
|
||||
setMinRating('all');
|
||||
}, []);
|
||||
|
||||
return {
|
||||
@@ -132,8 +147,10 @@ function useLibraryFilters() {
|
||||
setSort,
|
||||
status,
|
||||
setStatus,
|
||||
tagIds,
|
||||
toggleTag,
|
||||
tagNames,
|
||||
toggleTagName,
|
||||
minRating,
|
||||
setMinRating,
|
||||
filtered,
|
||||
resetFilters,
|
||||
},
|
||||
@@ -286,6 +303,44 @@ function useLibraryCover(detail: PlacesLibraryDetailResponse | null): string | n
|
||||
return entry && photo?.key === entry.key ? photo.url : null;
|
||||
}
|
||||
|
||||
type TagOption = { name: string; color: string | null };
|
||||
|
||||
/** The tag names seen so far with those of these entries added, by case-folded name; the same map when none is new. */
|
||||
function withTagsOf(
|
||||
seen: ReadonlyMap<string, TagOption>,
|
||||
entries: readonly PlacesLibraryEntry[]
|
||||
): ReadonlyMap<string, TagOption> {
|
||||
let next: Map<string, TagOption> | null = null;
|
||||
for (const entry of entries)
|
||||
for (const tag of entry.tags ?? []) {
|
||||
const key = tag.name.toLowerCase();
|
||||
if (seen.has(key) || next?.has(key)) continue;
|
||||
next ??= new Map(seen);
|
||||
next.set(key, { name: tag.name, color: tag.color ?? null });
|
||||
}
|
||||
return next ?? seen;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tag names the filter offers: every tag seen on a place since the page
|
||||
* opened (a filter that narrows the list does not take its own options away),
|
||||
* and every picked one, sorted by name.
|
||||
*/
|
||||
export function useTagOptions(entries: readonly PlacesLibraryEntry[], picked: readonly string[]) {
|
||||
// What was seen is kept beside the entries it was read from, so new entries add to it while rendering.
|
||||
const [held, setHeld] = useState(() => ({ entries, seen: withTagsOf(new Map(), entries) }));
|
||||
let seen = held.seen;
|
||||
if (held.entries !== entries) {
|
||||
seen = withTagsOf(held.seen, entries);
|
||||
setHeld({ entries, seen });
|
||||
}
|
||||
return useMemo(() => {
|
||||
const all = new Map(seen);
|
||||
for (const name of picked) if (!all.has(name.toLowerCase())) all.set(name.toLowerCase(), { name, color: null });
|
||||
return [...all.values()].sort((x, y) => x.name.localeCompare(y.name));
|
||||
}, [seen, picked]);
|
||||
}
|
||||
|
||||
/**
|
||||
* "All places" (PLC-07, PLC-08): every place of the user's trips and lists in one
|
||||
* overview, behind the desktop view and the phone screen, which render their own
|
||||
@@ -309,11 +364,13 @@ export function usePlacesLibrary({ active, t }: { active: boolean; t: Translatio
|
||||
}, []);
|
||||
const { entries, total, counts, error, offline } = pages.page;
|
||||
const mappable = useMemo(() => entries.filter(entryHasPosition), [entries]);
|
||||
const tagOptions = useTagOptions(entries, pages.controls.tagNames);
|
||||
|
||||
return {
|
||||
...pages.controls,
|
||||
entries,
|
||||
mappable,
|
||||
tagOptions,
|
||||
total,
|
||||
counts,
|
||||
hasMore: pages.hasMore,
|
||||
|
||||
Reference in New Issue
Block a user