refactor(collections): share the save to list picker logic between desktop and phone

useSaveToCollection holds the store driven picker that
SaveToCollectionModal and MSaveToCollectionSheet each carried: loading
the lists and the membership for a new target, toggling the place in or
out of a list, the per list status, marking it visited everywhere and
opening the collections page. Both views keep their own markup.

The two copies did the same thing, so no option was needed. The load
effect now lists the membership query beside the target instead of
silencing the exhaustive deps rule; the query is derived from the
target alone, so it runs exactly as before.
This commit is contained in:
Maurice
2026-10-09 14:26:42 +02:00
parent a0a4a24395
commit cf5b5c1245
4 changed files with 478 additions and 272 deletions
@@ -1,15 +1,9 @@
import { useEffect, useMemo, useState, useCallback } from 'react'
import { useNavigate } from 'react-router'
import { Bookmark, BookmarkCheck, Check, CheckCircle2, Loader2, Plus, X } from 'lucide-react'
import MSheet from '../../mobile/components/MSheet'
import MIconBtn from '../../mobile/components/MIconBtn'
import { useToast } from '../shared/Toast'
import { useTranslation } from '../../i18n'
import { collectionsApi } from '../../api/collections'
import { useSaveToCollectionStore } from '../../store/saveToCollectionStore'
import { getApiErrorMessage } from '../../utils/apiError'
import { STATUS_META, nextStatus } from '../../pages/collections/collectionsModel'
import type { Collection, CollectionMembership, CollectionStatus } from '@trek/shared'
import { VISITED_EVERYWHERE_BUSY, useSaveToCollection } from './useSaveToCollection'
/**
* Mobile counterpart of SaveToCollectionModal — the same store-driven list
@@ -18,135 +12,8 @@ import type { Collection, CollectionMembership, CollectionStatus } from '@trek/s
* detail sheet. Rendered instead of the desktop modal on phones (see App.tsx).
*/
export default function MSaveToCollectionSheet() {
const target = useSaveToCollectionStore(s => s.target)
const close = useSaveToCollectionStore(s => s.close)
const bumpVersion = useSaveToCollectionStore(s => s.bumpVersion)
const { target, close, lists, loading, busyId, savedByCollection, unvisited, handleStatus, handleVisitedEverywhere, handleToggle, openCollections } = useSaveToCollection()
const { t } = useTranslation()
const toast = useToast()
const navigate = useNavigate()
const [lists, setLists] = useState<Collection[]>([])
const [membership, setMembership] = useState<CollectionMembership | null>(null)
const [loading, setLoading] = useState(false)
const [busyId, setBusyId] = useState<number | null>(null)
const membershipQuery = useMemo(() => {
if (!target) return null
return {
google_place_id: target.google_place_id ?? undefined,
google_ftid: target.google_ftid ?? undefined,
name: target.name,
lat: target.lat ?? undefined,
lng: target.lng ?? undefined,
}
}, [target])
const refreshMembership = useCallback(async () => {
if (!membershipQuery) return
try {
setMembership(await collectionsApi.membership(membershipQuery))
} catch {
setMembership({ saved: false, lists: [] })
}
}, [membershipQuery])
// Load lists + membership whenever the picker opens for a new target.
useEffect(() => {
if (!target) return
let cancelled = false
setLoading(true)
setMembership(null)
void Promise.all([
collectionsApi.list().catch(() => ({ collections: [], incomingInvites: [] })),
membershipQuery
? collectionsApi.membership(membershipQuery).catch(() => ({ saved: false, lists: [] as CollectionMembership['lists'] }))
: Promise.resolve({ saved: false, lists: [] as CollectionMembership['lists'] }),
])
.then(([listRes, m]) => {
if (cancelled) return
setLists(listRes.collections)
setMembership(m)
})
.finally(() => { if (!cancelled) setLoading(false) })
return () => { cancelled = true }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target])
const savedByCollection = new Map<number, CollectionMembership['lists'][number]>()
for (const l of membership?.lists ?? []) savedByCollection.set(l.collection_id, l)
/** Lists holding this place that the viewer may edit and that are not visited yet. */
const unvisited = (membership?.lists ?? []).filter(l => l.can_edit && l.status !== 'visited')
const handleStatus = async (entry: CollectionMembership['lists'][number], next: CollectionStatus) => {
if (busyId != null) return
setBusyId(entry.collection_id)
try {
await collectionsApi.setStatus(entry.place_id, next)
await refreshMembership()
bumpVersion()
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')))
} finally {
setBusyId(null)
}
}
const handleVisitedEverywhere = async () => {
if (busyId != null || unvisited.length === 0) return
setBusyId(-1)
try {
const { updated } = await collectionsApi.setStatusMany(unvisited.map(l => l.place_id), 'visited')
toast.success(t('collections.markedVisited', { count: updated }))
await refreshMembership()
bumpVersion()
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')))
} finally {
setBusyId(null)
}
}
const handleToggle = async (list: Collection) => {
if (busyId != null || !target) return
const savedPlaceId = savedByCollection.get(list.id)?.place_id
setBusyId(list.id)
try {
if (savedPlaceId != null) {
await collectionsApi.deletePlace(savedPlaceId)
toast.success(t('collections.removedFromList', { name: list.name }))
} else {
await collectionsApi.savePlace({
collection_id: list.id,
source_trip_id: target.source_trip_id ?? null,
source_place_id: target.source_place_id ?? null,
name: target.name,
description: target.description ?? null,
lat: target.lat ?? null,
lng: target.lng ?? null,
address: target.address ?? null,
category_id: target.category_id ?? null,
price: target.price ?? null,
currency: target.currency ?? null,
notes: target.notes ?? null,
image_url: target.image_url ?? null,
google_place_id: target.google_place_id ?? null,
google_ftid: target.google_ftid ?? null,
osm_id: target.osm_id ?? null,
website: target.website ?? null,
phone: target.phone ?? null,
force: true,
})
toast.success(t('collections.addedToList', { name: list.name }))
}
await refreshMembership()
bumpVersion()
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')))
} finally {
setBusyId(null)
}
}
return (
<MSheet open={!!target} onClose={close} variant="card" material="glass" ariaLabel={t('collections.pickList')}>
@@ -181,7 +48,7 @@ export default function MSaveToCollectionSheet() {
<p className="mb-3 font-geist text-[0.75rem] text-m-faint">{t('collections.noListsYet')}</p>
<button
type="button"
onClick={() => { close(); navigate('/collections') }}
onClick={openCollections}
className="inline-flex items-center gap-1.5 rounded-full bg-m-act px-4 py-[9px] text-[0.75rem] font-semibold text-m-actfg"
>
<Plus size={14} strokeWidth={2.2} /> {t('collections.newList')}
@@ -262,7 +129,7 @@ export default function MSaveToCollectionSheet() {
disabled={busyId != null}
className="flex w-full items-center justify-center gap-1.5 rounded-[14px] border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] py-[10px] text-[0.78125rem] font-semibold text-m-ink disabled:opacity-60"
>
{busyId === -1
{busyId === VISITED_EVERYWHERE_BUSY
? <Loader2 size={14} className="animate-spin" />
: <CheckCircle2 size={14} strokeWidth={2.2} />}
{unvisited.length > 1 ? t('collections.markVisitedAll') : t('collections.markVisited')}
@@ -274,7 +141,7 @@ export default function MSaveToCollectionSheet() {
<div className="flex flex-none items-center justify-between gap-2 border-t border-[color:var(--m-rowbr)] px-[18px] py-3">
<button
type="button"
onClick={() => { close(); navigate('/collections') }}
onClick={openCollections}
className="text-[0.78125rem] font-semibold text-[color:var(--m-act)]"
>
{t('collections.viewInCollection')}