mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
172 lines
8.0 KiB
TypeScript
172 lines
8.0 KiB
TypeScript
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 = (
|
|
<BookingDialogHeader
|
|
tone="transit"
|
|
type="transit"
|
|
labelId={titleId}
|
|
onClose={onClose}
|
|
title={<TransitTitle title={title} iconSize={16} />}
|
|
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={(
|
|
<>
|
|
<TypePill type="transit" />
|
|
{dateStr && <span className={PILL}><CalendarDays size={13} strokeWidth={2.2} className="text-content-faint" />{dateStr}</span>}
|
|
{timeText && <span className={PILL}><Clock size={13} strokeWidth={2.2} className="text-content-faint" /><span className="tabular-nums">{timeText}</span></span>}
|
|
</>
|
|
)}
|
|
/>
|
|
)
|
|
const footer = (
|
|
<DialogFooter>
|
|
{canEdit && <DialogButton onClick={onChangeRoute} icon={<RefreshCw size={14} strokeWidth={2} />}>{t('transit.changeRoute')}</DialogButton>}
|
|
{canEdit && onEditDetails && <DialogButton onClick={onEditDetails} icon={<Pencil size={14} strokeWidth={2} />}>{t('transit.editDetails')}</DialogButton>}
|
|
<FooterSpacer />
|
|
{canEdit && <DeleteButton onClick={() => setConfirmDelete(true)} />}
|
|
{canEdit
|
|
? <DialogButton variant="primary" onClick={save} disabled={saving || !title.trim() || !dirty}>{saving ? t('common.saving') : t('common.save')}</DialogButton>
|
|
: <DialogButton variant="primary" onClick={onClose}>{t('common.close')}</DialogButton>}
|
|
</DialogFooter>
|
|
)
|
|
const noteTools = canEdit && (
|
|
<span className="flex items-center gap-2">
|
|
{notesTab === 'write' && (
|
|
<span className="flex gap-px rounded-[10px] bg-surface-tertiary p-[3px]">
|
|
{TRANSIT_NOTE_TOOLS.map(({ Icon, label, action }) => (
|
|
<Tooltip key={label} label={label} placement="top">
|
|
<button type="button" onClick={() => applyMd(action)} aria-label={label}
|
|
className="grid h-7 w-7 place-items-center rounded-[8px] text-content-muted hover:bg-surface-card hover:text-content">
|
|
<Icon size={13} strokeWidth={2} />
|
|
</button>
|
|
</Tooltip>
|
|
))}
|
|
</span>
|
|
)}
|
|
<Segmented<'write' | 'preview'>
|
|
value={notesTab}
|
|
onChange={setNotesTab}
|
|
label={t('reservations.notes')}
|
|
options={[{ value: 'write', label: t('common.edit') }, { value: 'preview', label: t('common.preview') }]}
|
|
/>
|
|
</span>
|
|
)
|
|
return (
|
|
<>
|
|
<DialogShell onClose={onClose} labelledBy={titleId} blocked={confirmDelete} header={header} footer={footer}>
|
|
{statTiles.length > 0 && (
|
|
<div className="flex gap-2">
|
|
{statTiles.map(({ value, label }) => (
|
|
<div key={label} className="min-w-0 flex-1 rounded-[12px] bg-surface-tertiary px-3 py-2.5 text-center">
|
|
<div className="truncate font-bold tabular-nums tracking-[-0.01em] text-content" style={fs(18, 'subtitle')}>{value}</div>
|
|
<div className="mt-0.5 truncate font-geist text-content-faint" style={fs(10.5)}>{label}</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
{legs.length > 0 && (
|
|
<DialogSection icon={Route} label={t('transit.itinerary')}>
|
|
<TransitLegs legs={legs} />
|
|
</DialogSection>
|
|
)}
|
|
<DialogSection icon={NotebookPen} label={t('reservations.notes')} action={noteTools}>
|
|
{canEdit && notesTab === 'write' ? (
|
|
<textarea
|
|
ref={notesRef}
|
|
value={notes}
|
|
onChange={e => setNotes(e.target.value)}
|
|
placeholder={t('reservations.notesPlaceholder')}
|
|
aria-label={t('reservations.notes')}
|
|
className={TEXTAREA}
|
|
style={{ minHeight: 130, resize: 'vertical', lineHeight: 1.55 }}
|
|
/>
|
|
) : (
|
|
<div className={`rounded-[10px] border border-edge-faint bg-surface-card px-3.5 py-3 ${canEdit ? 'min-h-[130px]' : ''}`}>
|
|
{notes.trim()
|
|
? <div className="collab-note-md break-words text-content" style={fs(13, 'body')}><Markdown remarkPlugins={[remarkGfm, remarkBreaks]}>{notes}</Markdown></div>
|
|
: <span className="text-content-faint" style={fs(12.5, 'body')}>{t('reservations.notesPlaceholder')}</span>}
|
|
</div>
|
|
)}
|
|
</DialogSection>
|
|
</DialogShell>
|
|
<ConfirmDialog
|
|
isOpen={confirmDelete}
|
|
onClose={() => setConfirmDelete(false)}
|
|
onConfirm={async () => { setConfirmDelete(false); await onDelete(); onClose() }}
|
|
title={t('reservations.confirm.deleteTitle')}
|
|
message={t(deleteBodyKey(res.id, budgetItems), { name: res.title })}
|
|
/>
|
|
</>
|
|
)
|
|
}
|