mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
d758f4c592
The desktop dialog and the phone sheet of a place show its overview, notes, tours and bookings, photos and files and its history in tabs, with the tags of each list and trip, a markdown notes editor, uploads and the place picture. The rating bar of All places is the rating row the lists show, with its voters, and votes once for every copy. All places filters by a lowest rating and sorts by it, and the edit form changes the first copy the reader may edit, which the server carries to the others.
153 lines
6.4 KiB
TypeScript
153 lines
6.4 KiB
TypeScript
import { ChevronRight, Pencil } from 'lucide-react';
|
||
import React from 'react';
|
||
import Markdown from 'react-markdown';
|
||
import remarkBreaks from 'remark-breaks';
|
||
import remarkGfm from 'remark-gfm';
|
||
import { useTranslation } from '../../i18n';
|
||
import type { Reservation } from '../../types';
|
||
import { formatTime, splitReservationDateTime } from '../../utils/formatters';
|
||
import { BlurredCode } from '../shared/BookingCode';
|
||
import { fs } from '../shared/DialogShell';
|
||
import { markdownLinkComponents } from '../shared/markdownLink';
|
||
import { Tooltip } from '../shared/Tooltip';
|
||
import { Field, TypeTile, toneOf, toneTint } from './bookings/bookingParts';
|
||
import { parseMeta } from './bookings/bookingsModel';
|
||
import { SoftPill } from './planParts';
|
||
|
||
/** The facts of a booking's metadata that the card has no field for, one pill each. */
|
||
function bookingMetaParts(res: Reservation, t: (key: string) => string): string[] {
|
||
const meta = parseMeta(res);
|
||
const parts: string[] = [];
|
||
if (meta.airline && meta.flight_number) parts.push(`${meta.airline} ${meta.flight_number}`);
|
||
else if (meta.flight_number) parts.push(meta.flight_number);
|
||
if (meta.departure_airport && meta.arrival_airport) parts.push(`${meta.departure_airport} → ${meta.arrival_airport}`);
|
||
if (meta.train_number) parts.push(meta.train_number);
|
||
if (meta.platform) parts.push(`${t('reservations.meta.platform')} ${meta.platform}`);
|
||
if (meta.check_in_time) parts.push(`${t('reservations.meta.checkIn')} ${meta.check_in_time}`);
|
||
if (meta.check_out_time) parts.push(`${t('reservations.meta.checkOut')} ${meta.check_out_time}`);
|
||
return parts;
|
||
}
|
||
|
||
/**
|
||
* A booking on a stop of the place inspector as a small booking card: the head
|
||
* band tinted by its status, then its date, time and code as fields. With
|
||
* `open` it opens the booking's detail or its editor (`opens`) on a click,
|
||
* Enter or Space; without it the card is a read-only summary.
|
||
*/
|
||
export default function LinkedBookingCard({
|
||
res,
|
||
timeFormat,
|
||
open,
|
||
opens = 'editor',
|
||
}: {
|
||
res: Reservation;
|
||
timeFormat: string;
|
||
open?: () => void;
|
||
opens?: 'detail' | 'editor';
|
||
}) {
|
||
const { t, locale } = useTranslation();
|
||
const openLabel = t(opens === 'detail' ? 'roadtrip.ride.open' : 'inspector.editRes');
|
||
// A stop can carry several bookings (#2201), so a card that opens its booking is
|
||
// named by it; the editor's card keeps its old name.
|
||
const cardName = opens === 'detail' ? `${openLabel}: ${res.title}` : openLabel;
|
||
const OpenGlyph = opens === 'detail' ? ChevronRight : Pencil;
|
||
const confirmed = res.status === 'confirmed';
|
||
const { date, time: startTime } = splitReservationDateTime(res.reservation_time);
|
||
const { time: endTime } = splitReservationDateTime(res.reservation_end_time);
|
||
const metaParts = bookingMetaParts(res, t);
|
||
const hasFields = !!(date || startTime || endTime || res.confirmation_number);
|
||
const card = (
|
||
<div
|
||
role={open ? 'button' : undefined}
|
||
// No press-scale on the composite card: shrinking it mid-click slides the
|
||
// links inside out from under the pointer (#2158).
|
||
data-no-press
|
||
aria-label={open ? cardName : undefined}
|
||
tabIndex={open ? 0 : undefined}
|
||
onClick={open}
|
||
onKeyDown={
|
||
open
|
||
? (e: React.KeyboardEvent) => {
|
||
if (e.key === 'Enter' || e.key === ' ') {
|
||
e.preventDefault();
|
||
open();
|
||
}
|
||
}
|
||
: undefined
|
||
}
|
||
className={`group flex flex-col overflow-hidden rounded-xl border border-edge-faint bg-surface-secondary text-start ${open ? 'cursor-pointer transition-shadow hover:shadow-md focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-[color:var(--text-primary)]' : ''}`}
|
||
>
|
||
<div className="flex items-center gap-2 px-2.5 py-1.5" style={{ background: toneTint(toneOf(res)) }}>
|
||
<TypeTile type={res.type} size={24} />
|
||
<span className="min-w-0 flex-1 truncate font-bold text-content" style={fs(12.5, 'body')}>
|
||
{res.title}
|
||
</span>
|
||
<SoftPill tone={confirmed ? 'success' : 'warning'} caps>
|
||
{confirmed ? t('reservations.confirmed') : t('reservations.pending')}
|
||
</SoftPill>
|
||
{open && (
|
||
<OpenGlyph
|
||
size={12}
|
||
strokeWidth={2}
|
||
aria-hidden
|
||
className="flex-none text-content-faint opacity-60 transition-opacity group-hover:opacity-100 group-focus-visible:opacity-100"
|
||
/>
|
||
)}
|
||
</div>
|
||
{(hasFields || metaParts.length > 0 || res.notes) && (
|
||
<div className="flex flex-col gap-2 border-t border-edge-faint px-2.5 pb-2.5 pt-2">
|
||
{hasFields && (
|
||
<div className="flex gap-2">
|
||
{date && (
|
||
<Field label={t('reservations.date')} className="flex-1">
|
||
{new Date(date + 'T00:00:00Z').toLocaleDateString(locale, {
|
||
weekday: 'short',
|
||
day: 'numeric',
|
||
month: 'short',
|
||
timeZone: 'UTC',
|
||
})}
|
||
</Field>
|
||
)}
|
||
{(startTime || endTime) && (
|
||
<Field label={t('reservations.time')} className="flex-1" tabular>
|
||
{startTime ? formatTime(startTime, locale, timeFormat) : ''}
|
||
{endTime ? ` – ${formatTime(endTime, locale, timeFormat)}` : ''}
|
||
</Field>
|
||
)}
|
||
{res.confirmation_number && (
|
||
<Field label={t('reservations.confirmationCode')} className="flex-1" tabular>
|
||
<BlurredCode className="font-geist">{res.confirmation_number}</BlurredCode>
|
||
</Field>
|
||
)}
|
||
</div>
|
||
)}
|
||
{metaParts.length > 0 && (
|
||
<div className="flex flex-wrap gap-1">
|
||
{metaParts.map((part, i) => (
|
||
<SoftPill key={i}>{part}</SoftPill>
|
||
))}
|
||
</div>
|
||
)}
|
||
{res.notes && (
|
||
<div
|
||
className="collab-note-md text-content-muted"
|
||
style={{ ...fs(11.5, 'body'), lineHeight: 1.45, wordBreak: 'break-word', overflowWrap: 'anywhere' }}
|
||
>
|
||
<Markdown remarkPlugins={[remarkGfm, remarkBreaks]} components={markdownLinkComponents}>
|
||
{res.notes}
|
||
</Markdown>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
return open ? (
|
||
<Tooltip label={openLabel} placement="top">
|
||
{card}
|
||
</Tooltip>
|
||
) : (
|
||
card
|
||
);
|
||
}
|