mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
2b0ef098fd
lint:dup found the trip place to save dialog mapping copied in the desktop and phone views of All places; it is now saveDraftOf in libraryPairs, with a test.
451 lines
16 KiB
TypeScript
451 lines
16 KiB
TypeScript
import type { CollectionPlace, CollectionStatus, PlacesLibraryEntry, PlacesLibrarySort } from '@trek/shared';
|
|
import {
|
|
ArrowDownUp,
|
|
Bookmark,
|
|
ChevronDown,
|
|
CopyCheck,
|
|
Globe2,
|
|
Layers,
|
|
List,
|
|
Loader2,
|
|
Luggage,
|
|
Map as MapIcon,
|
|
MapPin,
|
|
Search,
|
|
Shapes,
|
|
} from 'lucide-react';
|
|
import { useMemo, useState } from 'react';
|
|
import CollectionMap from '../../../components/Collections/CollectionMap';
|
|
import { saveDraftOf } from '../../../components/Collections/libraryPairs';
|
|
import { originName } from '../../../components/Collections/originName';
|
|
import { usePlaceDuplicates } from '../../../components/Collections/usePlaceDuplicates';
|
|
import { usePlacesLibrary, type LibraryKindFilter } from '../../../components/Collections/usePlacesLibrary';
|
|
import { STATUS_ORDER } from '../../../pages/collections/collectionsModel';
|
|
import { useCollectionStore } from '../../../store/collectionStore';
|
|
import { useSaveToCollectionStore } from '../../../store/saveToCollectionStore';
|
|
import type { Category, TranslationFn } from '../../../types';
|
|
import { placesPath } from '../../../utils/placesRoutes';
|
|
import { categoryMeta, STATUS_SPEC, tint, UNCATEGORIZED_META } from './collectionsMobileModel';
|
|
import { CHIP_IDLE, CHIP_ON, DROP_PANEL, DropRow, EmptyNote, FILTER_CHIP } from './MCollFilterKit';
|
|
import MCollTripPickerSheet from './MCollTripPickerSheet';
|
|
import MDuplicatesSheet from './MDuplicatesSheet';
|
|
import MLibraryPlaceSheet from './MLibraryPlaceSheet';
|
|
|
|
interface MPlacesLibraryProps {
|
|
categories: Category[];
|
|
language: string;
|
|
dark: boolean;
|
|
/** The screen's list or map, shared with the lists so the screen lays itself out for it. */
|
|
view: 'list' | 'map';
|
|
onView: (view: 'list' | 'map') => void;
|
|
navigate: (to: string) => void;
|
|
t: TranslationFn;
|
|
}
|
|
|
|
type Drop = 'kind' | 'status' | 'category' | 'sort';
|
|
|
|
/** A map point per entry with a position; the map speaks CollectionPlace, keyed by a running id. */
|
|
function toMapPlaces(entries: PlacesLibraryEntry[]) {
|
|
const keyOf = new Map<number, string>();
|
|
const places = entries.flatMap((entry, i) => {
|
|
if (entry.lat == null || entry.lng == null) return [];
|
|
keyOf.set(i + 1, entry.key);
|
|
return [
|
|
{
|
|
id: i + 1,
|
|
name: entry.name,
|
|
lat: entry.lat,
|
|
lng: entry.lng,
|
|
address: entry.address,
|
|
image_url: entry.image_url,
|
|
category_id: entry.category?.id ?? null,
|
|
category: entry.category,
|
|
status: 'idea',
|
|
} as unknown as CollectionPlace,
|
|
];
|
|
});
|
|
return { places, keyOf };
|
|
}
|
|
|
|
/** One entry of "All places" as a card: the category tile, name and address, and where it comes from. */
|
|
function Row({ entry, onOpen, t }: { entry: PlacesLibraryEntry; onOpen: () => void; t: TranslationFn }) {
|
|
const meta = categoryMeta(entry.category) ?? UNCATEGORIZED_META;
|
|
const Icon = meta.icon;
|
|
const trips = entry.origins.filter((o) => o.kind === 'trip');
|
|
const lists = entry.origins.filter((o) => o.kind === 'list');
|
|
const first = trips[0] ?? lists[0];
|
|
const more = entry.origins.length - 1;
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={onOpen}
|
|
className="mt-2 flex w-full items-center gap-[11px] rounded-2xl border border-m-rowbr bg-m-sheetop px-3 py-[11px] text-start"
|
|
>
|
|
<span
|
|
className="flex h-10 w-10 flex-none items-center justify-center rounded-xl"
|
|
style={{ background: tint(meta.color, '1f'), color: meta.color }}
|
|
>
|
|
<Icon size={17} strokeWidth={2.2} />
|
|
</span>
|
|
<span className="min-w-0 flex-1">
|
|
<span dir="auto" className="block truncate text-body font-bold text-m-ink">
|
|
{entry.name}
|
|
</span>
|
|
{entry.address && (
|
|
<span className="mt-[2px] flex items-center gap-[3px] truncate text-caption text-m-muted">
|
|
<MapPin size={10} strokeWidth={2.2} className="flex-none" />
|
|
<span dir="auto" className="truncate">
|
|
{entry.address}
|
|
</span>
|
|
</span>
|
|
)}
|
|
</span>
|
|
{first && (
|
|
<span className="flex max-w-[42%] flex-none items-center gap-1 rounded-full border border-m-rowbr bg-m-ic px-2 py-[3px] text-caption font-semibold text-m-muted">
|
|
{first.kind === 'trip' ? (
|
|
<Luggage size={11} className="flex-none" />
|
|
) : (
|
|
<Bookmark size={11} className="flex-none" />
|
|
)}
|
|
<span dir="auto" className="truncate">
|
|
{originName(first)}
|
|
</span>
|
|
{more > 0 && (
|
|
<span className="flex-none text-m-faint">{t('collections.library.moreOrigins', { count: more })}</span>
|
|
)}
|
|
</span>
|
|
)}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
type Library = ReturnType<typeof usePlacesLibrary>;
|
|
|
|
/** The open dropdown's choices: where from, status, category or order. */
|
|
function FilterDrop({
|
|
drop,
|
|
lib,
|
|
categories,
|
|
kindLabel,
|
|
pick,
|
|
t,
|
|
}: {
|
|
drop: Drop;
|
|
lib: Library;
|
|
categories: Category[];
|
|
kindLabel: (k: LibraryKindFilter) => string;
|
|
pick: (fn: () => void) => void;
|
|
t: TranslationFn;
|
|
}) {
|
|
return (
|
|
<>
|
|
{drop === 'kind' &&
|
|
(['all', 'trip', 'list'] as const).map((k) => (
|
|
<DropRow
|
|
key={k}
|
|
icon={
|
|
k === 'trip' ? (
|
|
<Luggage size={15} className="flex-none" />
|
|
) : k === 'list' ? (
|
|
<Bookmark size={15} className="flex-none" />
|
|
) : (
|
|
<Globe2 size={15} className="flex-none" />
|
|
)
|
|
}
|
|
label={kindLabel(k)}
|
|
count={lib.counts[k]}
|
|
on={lib.kind === k}
|
|
onPick={() => pick(() => lib.setKind(k))}
|
|
/>
|
|
))}
|
|
{drop === 'status' && (
|
|
<>
|
|
<DropRow
|
|
icon={<Layers size={15} className="flex-none" />}
|
|
label={t('collections.status.filterAll')}
|
|
on={lib.status === 'all'}
|
|
onPick={() => pick(() => lib.setStatus('all'))}
|
|
/>
|
|
{STATUS_ORDER.map((s: CollectionStatus) => {
|
|
const meta = STATUS_SPEC[s];
|
|
const Icon = meta.icon;
|
|
return (
|
|
<DropRow
|
|
key={s}
|
|
icon={<Icon size={15} className="flex-none" style={{ color: meta.color }} />}
|
|
label={t(meta.labelKey)}
|
|
on={lib.status === s}
|
|
onPick={() => pick(() => lib.setStatus(s))}
|
|
/>
|
|
);
|
|
})}
|
|
</>
|
|
)}
|
|
{drop === 'category' && (
|
|
<>
|
|
<DropRow
|
|
icon={<Shapes size={15} className="flex-none" />}
|
|
label={t('collections.status.filterAll')}
|
|
on={lib.categoryId === 'all'}
|
|
onPick={() => pick(() => lib.setCategoryId('all'))}
|
|
/>
|
|
{categories.map((c) => {
|
|
const meta = categoryMeta(c) ?? UNCATEGORIZED_META;
|
|
const Icon = meta.icon;
|
|
return (
|
|
<DropRow
|
|
key={c.id}
|
|
icon={<Icon size={15} className="flex-none" style={{ color: meta.color }} />}
|
|
label={c.name}
|
|
on={lib.categoryId === c.id}
|
|
onPick={() => pick(() => lib.setCategoryId(c.id))}
|
|
/>
|
|
);
|
|
})}
|
|
</>
|
|
)}
|
|
{drop === 'sort' &&
|
|
(['name', 'updated'] as PlacesLibrarySort[]).map((s) => (
|
|
<DropRow
|
|
key={s}
|
|
icon={<ArrowDownUp size={15} className="flex-none" />}
|
|
label={t(s === 'name' ? 'collections.sort.nameAsc' : 'collections.library.sortUpdated')}
|
|
on={lib.sort === s}
|
|
onPick={() => pick(() => lib.setSort(s))}
|
|
/>
|
|
))}
|
|
</>
|
|
);
|
|
}
|
|
|
|
/** What sits under the filters: offline, an error, loading, empty, the map or the rows. */
|
|
function Results({
|
|
lib,
|
|
view,
|
|
map,
|
|
dark,
|
|
t,
|
|
}: {
|
|
lib: Library;
|
|
view: 'list' | 'map';
|
|
map: ReturnType<typeof toMapPlaces>;
|
|
dark: boolean;
|
|
t: TranslationFn;
|
|
}) {
|
|
return lib.offline ? (
|
|
<EmptyNote scene="collections" title={t('collections.library.offline')} />
|
|
) : lib.error ? (
|
|
<p role="alert" className="mt-3 rounded-xl bg-danger-soft px-3 py-2 text-body text-danger">
|
|
{lib.error}
|
|
</p>
|
|
) : lib.loading && lib.entries.length === 0 ? (
|
|
<div className="flex justify-center py-16 text-m-faint">
|
|
<Loader2 size={20} className="animate-spin" />
|
|
</div>
|
|
) : lib.entries.length === 0 ? (
|
|
<EmptyNote
|
|
scene={lib.filtered ? 'search' : 'collections'}
|
|
title={t(lib.filtered ? 'collections.empty.noMatchTitle' : 'collections.library.emptyTitle')}
|
|
/>
|
|
) : view === 'map' ? (
|
|
<div className="mt-3 min-h-0 flex-1 overflow-hidden rounded-2xl border border-m-rowbr">
|
|
<CollectionMap
|
|
places={map.places}
|
|
selectedPlaceId={null}
|
|
onOpenPlace={(id) => {
|
|
const key = map.keyOf.get(id);
|
|
if (key) lib.show(key);
|
|
}}
|
|
dark={dark}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<div className="wide:grid wide:grid-cols-[repeat(auto-fill,minmax(min(100%,380px),1fr))] wide:gap-x-3">
|
|
{lib.entries.map((entry) => (
|
|
<Row key={entry.key} entry={entry} onOpen={() => lib.show(entry.key)} t={t} />
|
|
))}
|
|
{lib.hasMore && (
|
|
<button
|
|
type="button"
|
|
onClick={() => void lib.loadMore()}
|
|
disabled={lib.loadingMore}
|
|
className="mt-3 flex w-full items-center justify-center gap-2 rounded-full border border-m-rowbr bg-m-sheetop py-[10px] text-body font-semibold text-m-ink"
|
|
>
|
|
{lib.loadingMore && <Loader2 size={15} className="animate-spin" />}
|
|
{t('collections.library.more', { count: lib.total - lib.entries.length })}
|
|
</button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* "All places" on the phone (PLC-07): every place of the user's trips and lists as
|
|
* cards, with the same filter chips as a list (where from, status, category, order),
|
|
* a search and the map. A card or a pin opens the place sheet (PLC-08).
|
|
*/
|
|
export default function MPlacesLibrary({ categories, language, dark, view, onView, navigate, t }: MPlacesLibraryProps) {
|
|
const lib = usePlacesLibrary({ active: true, t });
|
|
const dup = usePlaceDuplicates({ active: true, onChanged: lib.reload, t });
|
|
const [showDup, setShowDup] = useState(false);
|
|
const openSave = useSaveToCollectionStore((s) => s.open);
|
|
const copyToTrip = useCollectionStore((s) => s.copyToTrip);
|
|
const [drop, setDrop] = useState<Drop | null>(null);
|
|
const [copyId, setCopyId] = useState<number | null>(null);
|
|
const map = useMemo(() => toMapPlaces(lib.entries), [lib.entries]);
|
|
|
|
const kindLabel = (k: LibraryKindFilter) =>
|
|
t(
|
|
k === 'all'
|
|
? 'collections.library.kindAll'
|
|
: k === 'trip'
|
|
? 'collections.library.kindTrips'
|
|
: 'collections.library.kindLists'
|
|
);
|
|
const category = lib.categoryId === 'all' ? null : (categories.find((c) => c.id === lib.categoryId) ?? null);
|
|
const pick = (fn: () => void) => {
|
|
fn();
|
|
setDrop(null);
|
|
};
|
|
const chip = (on: boolean) => `${FILTER_CHIP} ${on ? CHIP_ON : CHIP_IDLE}`;
|
|
|
|
return (
|
|
<div className={view === 'map' ? 'flex min-h-0 flex-1 flex-col' : ''}>
|
|
<div className="flex items-center gap-2">
|
|
<span className="flex flex-none rounded-xl border border-m-rowbr bg-m-sheetop p-[3px]">
|
|
{(['list', 'map'] as const).map((v) => (
|
|
<button
|
|
key={v}
|
|
type="button"
|
|
onClick={() => onView(v)}
|
|
aria-label={t(v === 'list' ? 'collections.view.list' : 'collections.view.map')}
|
|
aria-pressed={view === v}
|
|
className={`flex h-[30px] w-[38px] items-center justify-center rounded-[9px] ${view === v ? 'bg-m-act text-m-actfg' : 'text-m-muted'}`}
|
|
>
|
|
{v === 'list' ? <List size={15} strokeWidth={2} /> : <MapIcon size={15} strokeWidth={2} />}
|
|
</button>
|
|
))}
|
|
</span>
|
|
<div className="flex h-9 min-w-0 flex-1 items-center gap-2 rounded-xl border border-m-rowbr bg-m-sheetop px-[13px]">
|
|
<Search size={15} strokeWidth={2.2} className="flex-none text-m-muted" />
|
|
<input
|
|
value={lib.search}
|
|
onChange={(e) => lib.setSearch(e.target.value)}
|
|
enterKeyHint="search"
|
|
placeholder={t('collections.library.search')}
|
|
aria-label={t('collections.library.search')}
|
|
className="min-w-0 flex-1 bg-transparent text-body text-m-ink outline-none placeholder:text-m-faint"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="relative mt-2 flex items-center gap-[6px]">
|
|
<button
|
|
type="button"
|
|
onClick={() => setDrop((d) => (d === 'kind' ? null : 'kind'))}
|
|
aria-expanded={drop === 'kind'}
|
|
className={chip(lib.kind !== 'all')}
|
|
>
|
|
<Globe2 size={13} strokeWidth={2} className="flex-none" />
|
|
<span className="truncate">{kindLabel(lib.kind)}</span>
|
|
<ChevronDown size={12} strokeWidth={2} className="flex-none" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setDrop((d) => (d === 'status' ? null : 'status'))}
|
|
aria-expanded={drop === 'status'}
|
|
className={chip(lib.status !== 'all')}
|
|
>
|
|
<Layers size={13} strokeWidth={2} className="flex-none" />
|
|
<span className="truncate">
|
|
{lib.status === 'all' ? t('collections.status.filterAll') : t(STATUS_SPEC[lib.status].labelKey)}
|
|
</span>
|
|
<ChevronDown size={12} strokeWidth={2} className="flex-none" />
|
|
</button>
|
|
{categories.length > 0 && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setDrop((d) => (d === 'category' ? null : 'category'))}
|
|
aria-expanded={drop === 'category'}
|
|
aria-label={`${t('collections.category')}: ${category?.name ?? t('collections.status.filterAll')}`}
|
|
className={chip(category != null)}
|
|
>
|
|
<Shapes size={13} strokeWidth={2} className="flex-none" />
|
|
<span dir="auto" className="truncate">
|
|
{category?.name ?? t('collections.status.filterAll')}
|
|
</span>
|
|
<ChevronDown size={12} strokeWidth={2} className="flex-none" />
|
|
</button>
|
|
)}
|
|
<button
|
|
type="button"
|
|
onClick={() => setDrop((d) => (d === 'sort' ? null : 'sort'))}
|
|
aria-expanded={drop === 'sort'}
|
|
aria-label={t('collections.library.sortLabel')}
|
|
className={`${chip(lib.sort !== 'name')} !flex-none px-3`}
|
|
>
|
|
<ArrowDownUp size={13} strokeWidth={2} className="flex-none" />
|
|
</button>
|
|
|
|
{dup.pairs.length > 0 && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowDup(true)}
|
|
aria-label={t('collections.duplicates.title')}
|
|
className={`${chip(true)} !flex-none px-3`}
|
|
>
|
|
<CopyCheck size={13} strokeWidth={2} className="flex-none" />
|
|
<span>
|
|
{dup.pairs.length}
|
|
{dup.hasMore ? '+' : ''}
|
|
</span>
|
|
</button>
|
|
)}
|
|
|
|
{drop && (
|
|
<button
|
|
type="button"
|
|
aria-label={t('common.close')}
|
|
className="fixed inset-0 z-10 cursor-default"
|
|
onClick={() => setDrop(null)}
|
|
/>
|
|
)}
|
|
{drop && (
|
|
<div
|
|
className={`absolute inset-x-0 top-[calc(100%+6px)] z-20 max-h-[300px] overflow-y-auto p-[6px] ${DROP_PANEL}`}
|
|
>
|
|
<FilterDrop drop={drop} lib={lib} categories={categories} kindLabel={kindLabel} pick={pick} t={t} />
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<Results lib={lib} view={view} map={map} dark={dark} t={t} />
|
|
|
|
<MLibraryPlaceSheet
|
|
open={lib.selectedKey != null}
|
|
data={lib.detail}
|
|
loading={lib.detailLoading}
|
|
cover={lib.cover}
|
|
language={language}
|
|
onClose={lib.close}
|
|
onOpenList={(collectionId, placeId) => navigate(`${placesPath(collectionId)}?place=${placeId}`)}
|
|
onOpenTrip={(tripId, placeId) => navigate(`/trips/${tripId}?place=${placeId}`)}
|
|
onSaveToList={(origin, d) => openSave(saveDraftOf(origin, d, lib.detail?.entry.category?.id))}
|
|
onCopyToTrip={(id) => {
|
|
lib.close();
|
|
setCopyId(id);
|
|
}}
|
|
t={t}
|
|
/>
|
|
<MDuplicatesSheet open={showDup} d={dup} onClose={() => setShowDup(false)} t={t} />
|
|
<MCollTripPickerSheet
|
|
open={copyId != null}
|
|
count={1}
|
|
onCopy={(tripId, carry) => copyToTrip(tripId, copyId != null ? [copyId] : [], undefined, carry)}
|
|
onClose={() => setCopyId(null)}
|
|
t={t}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|