Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

5.6 KB
Raw Blame
import { useRef, useState } from 'react'
import { Check, Link2, Loader2, MapPin, Ticket, TrainFront } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import MSheet from '../../../components/MSheet'
import { PHONE_FILE_LINK_RULES, toggleFileLink, type FileLinkField } from '../../../../components/Files/fileActions'
import { linkedFileIds } from '../../../../components/Files/fileListRules'
import type { TripFile } from '../../../../types'
import { translateApiError } from '../../../../i18n'
import type { TripPlanner } from '../MTripShell'
import { Eyebrow, TileHeader } from '../sheets/MTripSheetUi'

interface MFileLinkSheetProps {
  planner: TripPlanner
  /** null closes the sheet; kept mounted through the exit animation via heldRef. */
  file: TripFile | null
  onClose: () => void
}

/**
 * Link picker for a file (spec 03 §5.3 "Verknüpfen", §7.3 m:n): toggles the
 * file's place_id/reservation_id (first link) or an addLink/removeLink file_link
 * record (further links) through toggleFileLink, the two-tier rule
 * FileManagerAssignModal.tsx runs on desktop, flattened to a single list per
 * section instead of grouping places by day (v1 simplification, see report).
 */
export default function MFileLinkSheet({ planner, file, onClose }: MFileLinkSheetProps) {
  const { t, tripId, places, reservations, TRANSPORT_TYPES, tripActions, toast } = planner
  const open = file != null

  const heldRef = useRef<TripFile | null>(file)
  if (file) heldRef.current = file
  const shown = file ?? heldRef.current

  const [busyKey, setBusyKey] = useState<string | null>(null)

  if (!shown) return <MSheet open={false} onClose={onClose} variant="card" material="glass" />

  const placeIds = new Set(linkedFileIds(shown, 'place_id'))
  const resIds = new Set(linkedFileIds(shown, 'reservation_id'))

  const refresh = () => tripActions.loadFiles(tripId)

  const toggle = async (field: FileLinkField, targetId: number) => {
    if (busyKey) return
    const key = `${field === 'place_id' ? 'p' : 'r'}${targetId}`
    setBusyKey(key)
    try {
      await toggleFileLink(tripId, shown, field, targetId, PHONE_FILE_LINK_RULES)
      refresh()
    } catch (err) {
      toast.error(translateApiError(t, err, 'files.toast.assignError'))
    } finally {
      setBusyKey(null)
    }
  }

  const togglePlace = (placeId: number) => toggle('place_id', placeId)
  const toggleReservation = (resId: number) => toggle('reservation_id', resId)

  const bookingReservations = reservations.filter(r => !TRANSPORT_TYPES.has(r.type))
  const transportReservations = reservations.filter(r => TRANSPORT_TYPES.has(r.type))
  const isEmpty = places.length === 0 && reservations.length === 0

  return (
    <MSheet open={open} onClose={onClose} variant="card" material="glass" ariaLabel={t('files.linkTitle')}>
      <div className="flex-none px-[18px] pt-4">
        <TileHeader
          icon={<Link2 size={19} strokeWidth={1.8} />}
          title={t('files.linkTitle')}
          sub={shown.original_name}
          onClose={onClose}
          closeLabel={t('common.close')}
        />
      </div>

      <div className="m-scroll-fade mt-2 min-h-0 flex-1 overflow-y-auto px-[18px] pb-[18px] pt-[14px]">
        {isEmpty && (
          <div className="py-8 text-center font-geist text-[0.78125rem] text-m-faint">{t('files.linkEmpty')}</div>
        )}

        {places.length > 0 && (
          <>
            <Eyebrow className="mb-[6px] mt-3">{t('files.assignPlace')}</Eyebrow>
            <div className="flex flex-col gap-1">
              {places.map(p => (
                <LinkRow key={`p${p.id}`} icon={MapPin} label={p.name} active={placeIds.has(p.id)} busy={busyKey === `p${p.id}`} onClick={() => togglePlace(p.id)} />
              ))}
            </div>
          </>
        )}

        {bookingReservations.length > 0 && (
          <>
            <Eyebrow className="mb-[6px] mt-3">{t('files.assignBooking')}</Eyebrow>
            <div className="flex flex-col gap-1">
              {bookingReservations.map(r => (
                <LinkRow key={`r${r.id}`} icon={Ticket} label={r.title} active={resIds.has(r.id)} busy={busyKey === `r${r.id}`} onClick={() => toggleReservation(r.id)} />
              ))}
            </div>
          </>
        )}

        {transportReservations.length > 0 && (
          <>
            <Eyebrow className="mb-[6px] mt-3">{t('files.assignTransport')}</Eyebrow>
            <div className="flex flex-col gap-1">
              {transportReservations.map(r => (
                <LinkRow key={`r${r.id}`} icon={TrainFront} label={r.title} active={resIds.has(r.id)} busy={busyKey === `r${r.id}`} onClick={() => toggleReservation(r.id)} />
              ))}
            </div>
          </>
        )}
      </div>
    </MSheet>
  )
}

function LinkRow({ icon: Icon, label, active, busy, onClick }: {
  icon: LucideIcon
  label: string
  active: boolean
  busy: boolean
  onClick: () => void
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      disabled={busy}
      className={`flex w-full items-center gap-[10px] rounded-[13px] border px-3 py-[10px] text-start disabled:opacity-60 ${
        active ? 'border-[color:var(--m-act)] bg-[color:var(--m-ic)]' : 'border-[color:var(--m-rowbr)] bg-m-card'
      }`}
    >
      <Icon size={14} strokeWidth={2} className="flex-none text-m-muted" />
      <span className="min-w-0 flex-1 truncate text-[0.8125rem] font-semibold text-m-ink">{label}</span>
      {busy ? (
        <Loader2 size={14} className="flex-none animate-spin text-m-faint" />
      ) : active ? (
        <Check size={15} strokeWidth={2.5} className="flex-none text-m-act" />
      ) : null}
    </button>
  )
}