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.

4.6 KB
Raw Blame
import { File, FileText, Files, FolderOpen, Image as ImageIcon, Star, StickyNote, Ticket, FileSpreadsheet } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import {
  PHONE_FILE_FILTER_RULES, getFileTypeCategory, linkedFileTargets,
  matchesFileFilter as matchesSharedFileFilter, type FileTypeCategory,
} from '../../../../components/Files/fileListRules'
import type { Place, Reservation, TranslationFn, TripFile } from '../../../../types'

/**
 * Pure view-model helpers for the mobile Files tab — the real-data
 * counterpart to the demo's FILES rows (spec 03 §5). Type categorisation,
 * filter matching/counting, the starred-first sort and the link-row label
 * builder live here so MFilesTab and the sheets stay layout-only, same split
 * as transportsModel.ts for the Transports tab.
 */

export type FileFilterId = 'all' | 'pdf' | 'image' | 'doc' | 'starred' | 'collab'

interface FileTypeMeta {
  category: FileTypeCategory
  icon: LucideIcon
  /** Type-tile accent — the tile background is this colour at ~13% alpha (spec 03 §5.2, Z. 3654). */
  color: string
  /** Short uppercase tile caption (Geist 7px/800, spec 03 §5.2). */
  label: string
}

const TYPE_META: Record<FileTypeCategory, Omit<FileTypeMeta, 'category'>> = {
  pass: { icon: Ticket, color: '#8B5CF6', label: 'PASS' },
  pdf: { icon: FileText, color: '#D6273B', label: 'PDF' },
  image: { icon: ImageIcon, color: '#3BA55C', label: 'IMG' },
  xls: { icon: FileSpreadsheet, color: '#2FA37A', label: 'XLS' },
  other: { icon: File, color: '#6b7280', label: 'FILE' },
}

export function getFileTypeMeta(file: Pick<TripFile, 'mime_type' | 'original_name'>): FileTypeMeta {
  const category = getFileTypeCategory(file)
  return { category, ...TYPE_META[category] }
}

/** Filter grid definition, in display order (spec 03 §5.1; 'collab' is the optional 6th tile, §5.5/§7.4). */
export const FILE_FILTERS: { id: FileFilterId; icon: LucideIcon; labelKey: string }[] = [
  { id: 'all', icon: FolderOpen, labelKey: 'files.filterAll' },
  { id: 'pdf', icon: FileText, labelKey: 'files.filterPdf' },
  { id: 'image', icon: ImageIcon, labelKey: 'files.filterImages' },
  { id: 'doc', icon: Files, labelKey: 'files.filterDocs' },
  { id: 'starred', icon: Star, labelKey: 'files.filterStarred' },
  { id: 'collab', icon: StickyNote, labelKey: 'files.filterCollab' },
]

/**
 * 'doc' is the demo's "XLS+PASS" bucket (spec 03 §5.1) widened to every
 * non-PDF, non-media file so the tiles fully partition `files` — a plain
 * Word/text/zip attachment (which only gets the generic fallback icon on the
 * row, spec's "Fallback file") still counts as a "Doc" instead of being
 * invisible to every specific filter except All (open point, see report).
 */
export function matchesFileFilter(file: TripFile, filter: FileFilterId): boolean {
  return matchesSharedFileFilter(file, filter, PHONE_FILE_FILTER_RULES)
}

/** Starred files first, stable otherwise (spec 03 §5.2: "Sortierung: markierte Dateien zuerst"). */
export function sortFilesStarredFirst(files: TripFile[]): TripFile[] {
  return [...files].sort((a, b) => (b.starred ? 1 : 0) - (a.starred ? 1 : 0))
}

/** Compact "16. Jul" date, matching the rest of the mobile trip screen (e.g. transportsModel's day chips). */
export function formatFileDate(dateStr: string | null | undefined, locale: string): string {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  if (Number.isNaN(date.getTime())) return ''
  return date.toLocaleDateString(locale, { day: 'numeric', month: 'short' })
}

/** What a file is linked to, as the row shows it: the kind of thing, and its name. */
export interface FileLinkLabel {
  kind: string
  name?: string
}

/**
 * The link row's descriptors (spec 03 §5.2): one kind and name per linked place /
 * reservation (transport vs. booking split like FileManagerRow.tsx's SourceBadge
 * list) plus a Collab-Notes entry when note_id is set. The row draws each as a
 * badge of its own; joined into one string with dots, a kind and a name and the
 * next kind read as one run.
 */
export function buildFileLinkLabels(
  file: TripFile,
  places: Place[],
  reservations: Reservation[],
  transportTypes: Set<string>,
  t: TranslationFn,
): FileLinkLabel[] {
  const linked = linkedFileTargets(file, places, reservations, transportTypes)
  const labels: FileLinkLabel[] = linked.places.map(place => ({ kind: t('files.sourcePlan'), name: place.name }))
  for (const { reservation, transport } of linked.reservations) {
    const key = transport ? 'files.sourceTransport' : 'files.sourceBooking'
    labels.push({ kind: t(key), name: reservation.title || undefined })
  }

  if (file.note_id != null) labels.push({ kind: t('files.sourceCollab') })

  return labels
}