fix(collections): saved-places picker height + list filter, all-saved first-load (#1081)

- Trip "Saved places" picker: drop the fixed 360px cap so the list fills the
  panel instead of stopping half-way, and add list + status filter dropdowns
  (filter by which collection the place is saved in).
- "All saved" showed nothing on first open: setActive(ALL_SAVED) unioned the
  lists from the store, but on first load those aren't fetched yet (loadAll still
  running). Load them first when empty so the union isn't blank.
This commit is contained in:
Maurice
2026-07-01 15:30:17 +02:00
committed by Maurice
parent 56fee19d64
commit fd5258129a
24 changed files with 119 additions and 21 deletions
@@ -1,8 +1,9 @@
import React, { useEffect, useMemo, useState } from 'react'
import { Search, Bookmark, Loader2 } from 'lucide-react'
import React, { useEffect, useMemo, useRef, useState } from 'react'
import { Search, Bookmark, Loader2, ChevronDown, Check, Layers } from 'lucide-react'
import PlaceAvatar from '../shared/PlaceAvatar'
import { collectionsApi } from '../../api/collections'
import type { CollectionPlace } from '@trek/shared'
import { STATUS_META, STATUS_ORDER } from '../../pages/collections/collectionsModel'
import type { CollectionPlace, CollectionStatus } from '@trek/shared'
import type { TranslationFn } from '../../types'
interface LocationBias {
@@ -10,6 +11,8 @@ interface LocationBias {
high: { lat: number; lng: number }
}
interface ListMeta { id: number; name: string; color: string | null }
interface CollectionPickerProps {
/** Trip bounding box used for autocomplete — sorts the saved places by
* proximity to the trip so the relevant ones surface first. */
@@ -26,16 +29,64 @@ function distanceTo(p: CollectionPlace, center: { lat: number; lng: number }): n
return dlat * dlat + dlng * dlng
}
interface Opt { key: string | number; label: string; icon?: React.ReactNode; count?: number }
/** Compact click-away dropdown (Tailwind — this panel lives outside .trek-dash). */
function FilterDropdown({ current, options, onSelect, lead }: {
current: string | number
options: Opt[]
onSelect: (key: string | number) => void
lead: React.ReactNode
}): React.ReactElement {
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
if (!open) return
const onDoc = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false) }
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false) }
document.addEventListener('mousedown', onDoc)
document.addEventListener('keydown', onKey)
return () => { document.removeEventListener('mousedown', onDoc); document.removeEventListener('keydown', onKey) }
}, [open])
const cur = options.find(o => o.key === current) ?? options[0]
return (
<div className="relative min-w-0 flex-1" ref={ref}>
<button type="button" onClick={() => setOpen(o => !o)} aria-haspopup="listbox" aria-expanded={open}
className={`w-full flex items-center gap-1.5 px-2.5 py-1.5 rounded-lg border bg-surface-input text-[12px] font-medium text-content-secondary transition-colors ${open ? 'border-accent' : 'border-edge hover:bg-surface-hover'}`}>
<span className="shrink-0 text-content-faint">{cur.icon ?? lead}</span>
<span className="flex-1 min-w-0 truncate text-left">{cur.label}</span>
<ChevronDown size={13} className="shrink-0 text-content-faint" />
</button>
{open && (
<div role="listbox" className="absolute z-30 left-0 right-0 mt-1 max-h-[240px] overflow-y-auto p-1 rounded-xl border border-edge bg-surface-card shadow-lg flex flex-col gap-0.5">
{options.map(o => (
<button key={o.key} type="button" role="option" aria-selected={o.key === current} onClick={() => { onSelect(o.key); setOpen(false) }}
className={`flex items-center gap-2 px-2 py-1.5 rounded-lg text-[12.5px] text-left transition-colors hover:bg-surface-hover ${o.key === current ? 'text-content font-semibold' : 'text-content-secondary'}`}>
<span className="shrink-0 text-content-faint">{o.icon}</span>
<span className="flex-1 min-w-0 truncate">{o.label}</span>
{o.count != null && <span className="shrink-0 text-[11px] text-content-faint tabular-nums">{o.count}</span>}
{o.key === current && <Check size={13} className="shrink-0 text-accent" />}
</button>
))}
</div>
)}
</div>
)
}
/**
* Right-hand column of the desktop add-place modal: the user's saved collection
* places, searchable and proximity-sorted, so a place saved on an earlier trip
* can be dropped straight into the form. Clones the maps search + scrollable
* list pattern. Desktop only — gated by the caller.
* places, searchable, filterable by list + status, and proximity-sorted, so a
* place saved on an earlier trip can be dropped straight into the form.
* Desktop only — gated by the caller.
*/
export default function CollectionPicker({ bias, onSelect, t }: CollectionPickerProps): React.ReactElement {
const [places, setPlaces] = useState<CollectionPlace[]>([])
const [lists, setLists] = useState<ListMeta[]>([])
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState('')
const [listFilter, setListFilter] = useState<number | 'all'>('all')
const [statusFilter, setStatusFilter] = useState<CollectionStatus | 'all'>('all')
useEffect(() => {
let cancelled = false
@@ -44,19 +95,15 @@ export default function CollectionPicker({ bias, onSelect, t }: CollectionPicker
.then(async (res) => {
const detail = await Promise.all(res.collections.map(c => collectionsApi.get(c.id).catch(() => null)))
if (cancelled) return
const seen = new Set<number>()
const merged: CollectionPlace[] = []
for (const d of detail) {
if (!d) continue
for (const p of d.places) {
if (seen.has(p.id)) continue
seen.add(p.id)
merged.push(p)
}
for (const p of d.places) merged.push(p)
}
setLists(res.collections.map(c => ({ id: c.id, name: c.name, color: c.color ?? null })))
setPlaces(merged)
})
.catch(() => { if (!cancelled) setPlaces([]) })
.catch(() => { if (!cancelled) { setPlaces([]); setLists([]) } })
.finally(() => { if (!cancelled) setLoading(false) })
return () => { cancelled = true }
}, [])
@@ -68,21 +115,41 @@ export default function CollectionPicker({ bias, onSelect, t }: CollectionPicker
const visible = useMemo(() => {
const q = search.trim().toLowerCase()
const list = q
? places.filter(p => p.name.toLowerCase().includes(q) || (p.address ?? '').toLowerCase().includes(q))
: [...places]
const list = places.filter(p => {
if (listFilter !== 'all' && p.collection_id !== listFilter) return false
if (statusFilter !== 'all' && p.status !== statusFilter) return false
if (!q) return true
return p.name.toLowerCase().includes(q) || (p.address ?? '').toLowerCase().includes(q)
})
if (center) list.sort((a, b) => distanceTo(a, center) - distanceTo(b, center))
else list.sort((a, b) => a.name.localeCompare(b.name))
return list
}, [places, search, center])
}, [places, search, center, listFilter, statusFilter])
const listOpts: Opt[] = [
{ key: 'all', label: t('collections.picker.allLists'), icon: <Layers size={13} />, count: places.length },
...lists.map(l => ({
key: l.id,
label: l.name,
icon: <span className="w-2.5 h-2.5 rounded-full inline-block" style={{ background: l.color || '#6366f1' }} />,
count: places.filter(p => p.collection_id === l.id).length,
})),
]
const statusOpts: Opt[] = [
{ key: 'all', label: t('common.all') },
...STATUS_ORDER.map(s => {
const Icon = STATUS_META[s].icon
return { key: s, label: t(STATUS_META[s].labelKey), icon: <Icon size={13} style={{ color: STATUS_META[s].color }} /> }
}),
]
return (
<aside className="w-full sm:w-64 shrink-0 flex flex-col rounded-xl border border-edge bg-surface-secondary overflow-hidden self-stretch">
<div className="flex items-center gap-2 px-3 py-2.5 border-b border-edge">
<div className="flex items-center gap-2 px-3 py-2.5 border-b border-edge shrink-0">
<Bookmark size={15} className="text-accent" />
<span className="text-[13px] font-semibold text-content">{t('collections.picker.title')}</span>
</div>
<div className="p-2.5">
<div className="p-2.5 flex flex-col gap-2 shrink-0">
<div className="relative">
<Search size={14} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-content-faint" />
<input
@@ -92,8 +159,14 @@ export default function CollectionPicker({ bias, onSelect, t }: CollectionPicker
className="w-full pl-8 pr-3 py-1.5 rounded-lg border border-edge bg-surface-input text-content text-[13px] outline-none focus:border-accent"
/>
</div>
{lists.length > 0 && (
<div className="flex gap-2">
<FilterDropdown current={listFilter} options={listOpts} onSelect={k => setListFilter(k as number | 'all')} lead={<Layers size={13} />} />
<FilterDropdown current={statusFilter} options={statusOpts} onSelect={k => setStatusFilter(k as CollectionStatus | 'all')} lead={<Bookmark size={13} />} />
</div>
)}
</div>
<div className="flex-1 min-h-0 overflow-y-auto px-2 pb-2 max-h-[360px]">
<div className="flex-1 min-h-0 overflow-y-auto px-2 pb-2">
{loading ? (
<div className="flex items-center justify-center py-10 text-content-faint">
<Loader2 size={18} className="animate-spin" />
+4 -1
View File
@@ -130,7 +130,10 @@ export const useCollectionStore = create<CollectionState>((set, get) => ({
set({ activeId: ALL_SAVED, members: [], placesLoading: true })
try {
// Client-side union of every list the user owns or co-owns (no server change).
const lists = get().collections
// On first load the lists may not be fetched yet (loadAll still in flight),
// which would union nothing — make sure they're loaded first.
let lists = get().collections
if (lists.length === 0) { await get().loadAll(); lists = get().collections }
const results = await Promise.all(lists.map(l => collectionsApi.get(l.id).catch(() => null)))
const seen = new Set<number>()
const merged: CollectionPlace[] = []
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'كل القوائم',
'collections.select': 'تحديد',
'collections.selectAll': 'تحديد الكل',
'collections.deselectAll': 'إلغاء التحديد',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Todas as listas',
'collections.select': 'Selecionar',
'collections.selectAll': 'Selecionar tudo',
'collections.deselectAll': 'Desmarcar tudo',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Všechny seznamy',
'collections.select': 'Vybrat',
'collections.selectAll': 'Vybrat vše',
'collections.deselectAll': 'Zrušit výběr',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Alle Listen',
'collections.select': 'Auswählen',
'collections.selectAll': 'Alle auswählen',
'collections.deselectAll': 'Auswahl aufheben',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'All lists',
'collections.select': 'Select',
'collections.selectAll': 'Select all',
'collections.deselectAll': 'Deselect all',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Todas las listas',
'collections.select': 'Seleccionar',
'collections.selectAll': 'Seleccionar todo',
'collections.deselectAll': 'Deseleccionar todo',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Toutes les listes',
'collections.select': 'Sélectionner',
'collections.selectAll': 'Tout sélectionner',
'collections.deselectAll': 'Tout désélectionner',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Όλες οι λίστες',
'collections.select': 'Επιλογή',
'collections.selectAll': 'Επιλογή όλων',
'collections.deselectAll': 'Κατάργηση επιλογής',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Összes lista',
'collections.select': 'Kijelölés',
'collections.selectAll': 'Összes kijelölése',
'collections.deselectAll': 'Kijelölés megszüntetése',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Semua daftar',
'collections.select': 'Pilih',
'collections.selectAll': 'Pilih semua',
'collections.deselectAll': 'Batalkan pilihan',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Tutte le liste',
'collections.select': 'Seleziona',
'collections.selectAll': 'Seleziona tutto',
'collections.deselectAll': 'Deseleziona tutto',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'すべてのリスト',
'collections.select': '選択',
'collections.selectAll': 'すべて選択',
'collections.deselectAll': '選択を解除',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': '모든 목록',
'collections.select': '선택',
'collections.selectAll': '모두 선택',
'collections.deselectAll': '선택 해제',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Alle lijsten',
'collections.select': 'Selecteren',
'collections.selectAll': 'Alles selecteren',
'collections.deselectAll': 'Selectie opheffen',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Wszystkie listy',
'collections.select': 'Zaznacz',
'collections.selectAll': 'Zaznacz wszystko',
'collections.deselectAll': 'Odznacz wszystko',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Все списки',
'collections.select': 'Выбрать',
'collections.selectAll': 'Выбрать все',
'collections.deselectAll': 'Снять выделение',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Alla listor',
'collections.select': 'Välj',
'collections.selectAll': 'Välj alla',
'collections.deselectAll': 'Avmarkera alla',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Tüm listeler',
'collections.select': 'Seç',
'collections.selectAll': 'Tümünü seç',
'collections.deselectAll': 'Seçimi kaldır',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Усі списки',
'collections.select': 'Вибрати',
'collections.selectAll': 'Вибрати все',
'collections.deselectAll': 'Зняти вибір',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': 'Tất cả danh sách',
'collections.select': 'Chọn',
'collections.selectAll': 'Chọn tất cả',
'collections.deselectAll': 'Bỏ chọn tất cả',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': '所有清單',
'collections.select': '選擇',
'collections.selectAll': '全選',
'collections.deselectAll': '取消全選',
+1
View File
@@ -1,6 +1,7 @@
import type { TranslationStrings } from '../types';
const collection: TranslationStrings = {
'collections.picker.allLists': '所有列表',
'collections.select': '选择',
'collections.selectAll': '全选',
'collections.deselectAll': '取消全选',