import { useEffect, useState } from 'react' import Markdown from 'react-markdown' import remarkGfm from 'remark-gfm' import remarkBreaks from 'remark-breaks' import { Route, NotebookPen, CalendarDays, Clock, Pencil, RefreshCw } from 'lucide-react' import ConfirmDialog from '../shared/ConfirmDialog' import { DialogShell, DialogSection, DialogFooter, FooterSpacer, DialogButton, DeleteButton, PILL, fs } from '../shared/DialogShell' import { Segmented, TEXTAREA } from '../shared/dialogParts' import { Tooltip } from '../shared/Tooltip' import { BookingDialogHeader, TypePill } from './bookings/BookingDialogShell' import { TransitLegs } from './bookings/transitParts' import { deleteBodyKey } from './bookings/bookingsModel' import { useTranslation } from '../../i18n' import { useSettingsStore } from '../../store/settingsStore' import { useTripStore } from '../../store/tripStore' import { TransitTitle } from './transitDisplay' import { TRANSIT_NOTE_TOOLS, transitJourneyFacts, useTransitNoteFormatting, type TransitJourneyModalProps } from './transitJourneyModel' /** * The journey view for an automated public-transit entry (#1065): a roomy modal * around the stop-by-stop itinerary. The title renames inline right in the * header, notes get the full width with markdown support, and "Change route" * re-enters the transit search pre-seeded with this journey's route. "Edit * details" hands off to the full transport editor for the booking fields * (travelers, costs, files, code, status — #2148). */ export default function TransitJourneyModal({ reservation, onClose, onSave, onDelete, onChangeRoute, onEditDetails, canEdit }: TransitJourneyModalProps) { const { t, locale } = useTranslation() const timeFormat = useSettingsStore(st => st.settings.time_format) || '24h' const budgetItems = useTripStore(st => st.budgetItems) const res = reservation const [title, setTitle] = useState(res.title || '') const [editingTitle, setEditingTitle] = useState(false) const [notes, setNotes] = useState(res.notes || '') // Existing notes open rendered; the write tab is for editing. const [notesTab, setNotesTab] = useState<'write' | 'preview'>(() => (res.notes ? 'preview' : 'write')) const [saving, setSaving] = useState(false) const [confirmDelete, setConfirmDelete] = useState(false) const { notesRef, applyMd } = useTransitNoteFormatting(notes, setNotes) useEffect(() => { setTitle(res.title || '') setNotes(res.notes || '') setEditingTitle(false) setNotesTab(res.notes ? 'preview' : 'write') }, [res.id]) const dirty = title !== (res.title || '') || notes !== (res.notes || '') const save = async () => { if (!title.trim()) return setSaving(true) try { await onSave({ title: title.trim(), notes: notes.trim() || null }) onClose() } finally { setSaving(false) } } const { dateStr, timeText, legs, statTiles } = transitJourneyFacts(res, t, locale, timeFormat) const titleId = `transit-journey-${res.id}` const header = ( } onTitleClick={canEdit ? () => setEditingTitle(true) : undefined} titleTooltip={t('reservations.rename')} titleInput={editingTitle ? { value: title, onChange: setTitle, label: t('reservations.titleLabel'), autoFocus: true, onBlur: () => setEditingTitle(false), onKeyDown: e => { if (e.key === 'Enter') setEditingTitle(false) // Spent here, so the dialog stays open and only the rename is dropped. if (e.key === 'Escape') { e.preventDefault(); setTitle(res.title || ''); setEditingTitle(false) } }, } : undefined} pills={( <> {dateStr && {dateStr}} {timeText && {timeText}} )} /> ) const footer = ( {canEdit && }>{t('transit.changeRoute')}} {canEdit && onEditDetails && }>{t('transit.editDetails')}} {canEdit && setConfirmDelete(true)} />} {canEdit ? {saving ? t('common.saving') : t('common.save')} : {t('common.close')}} ) const noteTools = canEdit && ( {notesTab === 'write' && ( {TRANSIT_NOTE_TOOLS.map(({ Icon, label, action }) => ( ))} )} value={notesTab} onChange={setNotesTab} label={t('reservations.notes')} options={[{ value: 'write', label: t('common.edit') }, { value: 'preview', label: t('common.preview') }]} /> ) return ( <> {statTiles.length > 0 && (
{statTiles.map(({ value, label }) => (
{value}
{label}
))}
)} {legs.length > 0 && ( )} {canEdit && notesTab === 'write' ? (