refactor(collections): share the copy to trip picker between desktop and phone

useTripCopyPicker loads the trips on every opening, filters them by the search,
formats the date range and turns the copy result into toasts for both the
dialog and the sheet. The dialog keeps its guard against an empty selection.
This commit is contained in:
Maurice
2026-10-09 10:05:57 +02:00
parent 1a4f986cc3
commit 634dd411c5
4 changed files with 262 additions and 128 deletions
@@ -1,22 +1,9 @@
import { useEffect, useMemo, useState } from 'react'
import { CalendarDays, Copy, Loader2, MapPin, Search } from 'lucide-react'
import type { TranslationFn } from '../../../types'
import { tripsApi } from '../../../api/client'
import { useTranslation } from '../../../i18n'
import { useToast } from '../../../components/shared/Toast'
import { getApiErrorMessage } from '../../../utils/apiError'
import { formatDate } from '../../../utils/formatters'
import { useTripCopyPicker } from '../../../components/Collections/useTripCopyPicker'
import MSheet from '../../components/MSheet'
import { SheetHeader } from './MCollSheetKit'
interface TripOption {
id: number
title: string
start_date?: string | null
end_date?: string | null
cover_image?: string | null
}
interface MCollTripPickerSheetProps {
open: boolean
count: number
@@ -30,53 +17,8 @@ interface MCollTripPickerSheetProps {
* selected places and reconciles the dedup result into toasts.
*/
export default function MCollTripPickerSheet({ open, count, onCopy, onClose, t }: MCollTripPickerSheetProps) {
const toast = useToast()
const { language } = useTranslation()
const [trips, setTrips] = useState<TripOption[]>([])
const [loading, setLoading] = useState(false)
const [search, setSearch] = useState('')
const [busyTripId, setBusyTripId] = useState<number | null>(null)
useEffect(() => {
if (!open) return
let cancelled = false
setLoading(true)
setSearch('')
tripsApi.list()
.then((res: { trips?: TripOption[] }) => { if (!cancelled) setTrips(res.trips ?? []) })
.catch(() => { if (!cancelled) setTrips([]) })
.finally(() => { if (!cancelled) setLoading(false) })
return () => { cancelled = true }
}, [open])
const filtered = useMemo(() => {
const q = search.trim().toLowerCase()
if (!q) return trips
return trips.filter(tr => (tr.title ?? '').toLowerCase().includes(q))
}, [trips, search])
const dateRange = (tr: TripOption): string => {
const s = formatDate(tr.start_date, language)
const e = formatDate(tr.end_date, language)
if (s && e) return `${s} – ${e}`
return s || e || ''
}
const handleCopy = async (tripId: number) => {
if (busyTripId != null) return
setBusyTripId(tripId)
try {
const res = await onCopy(tripId)
if (res.copied > 0) toast.success(t('collections.copiedCount', { count: res.copied }))
if (res.skipped.length > 0) toast.info(t('collections.skippedDuplicates', { count: res.skipped.length }))
if (res.copied === 0 && res.skipped.length === 0) toast.info(t('collections.copyNothing'))
onClose()
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')))
} finally {
setBusyTripId(null)
}
}
const { loading, search, setSearch, filtered, busyTripId, dateRange, handleCopy } =
useTripCopyPicker({ open, onCopy, onClose, t })
const title = t('collections.copyN', { count })