mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
8f53d98d45
Every journey map (the desktop aside, the phone screens and the shared page) now carries four switches: Plan, Recorded, Entries and Photos, each with its count, a spinner while it loads, the warning tone when it could not be read and the reason when it has nothing to show. A switch only hides, in place: the map is not rebuilt and keeps its view, and the choice is kept in this browser per journey (per link on a shared page, by a hash of its token). The plan draws the places the linked trips put on their days as hollow dashed pins and the trips' GPX routes dashed; a place an entry was written about is not drawn twice, the entry's pin carries a check instead. The stays a day report was taken over from show as dots in the colour of their day. Marks on one spot join into a disc with their number and open as a list; a picked place or stay opens a card with its trip, days and times and leads to its entry. A key beside the switches explains every pin and line and says that a plan is no proof of a visit. The phone timelines now show the photos too. The GPX routes come with the map's layers through the linked trips, read only while the journey's track switch is on; the frozen /tracks answer stays Studio's. The share settings on desktop and phone get Trip plan and Recorded routes under Map, off by default and only switchable while the map is shared.
185 lines
7.3 KiB
TypeScript
185 lines
7.3 KiB
TypeScript
import type { JourneyMapPlace, JourneyMapStay } from '@trek/shared';
|
|
import { CalendarRange, ChevronLeft, ChevronRight, Clock, MapPin, NotebookPen, X } from 'lucide-react';
|
|
import { useEffect, useState } from 'react';
|
|
import { useTranslation } from '../../i18n';
|
|
import { SoftPill, WhiteBadge } from '../Planner/planParts';
|
|
import { fs } from '../shared/DialogShell';
|
|
import type { JourneyMapMark } from './journeyMapMarks';
|
|
|
|
interface Props {
|
|
/** What was picked on the map: one mark, or several that lie on one spot. */
|
|
marks: JourneyMapMark[];
|
|
onClose: () => void;
|
|
/** Leads to an entry; absent where the reader cannot open one. */
|
|
onOpenEntry?: (entryId: string) => void;
|
|
/** The title of an entry the reader can see, for the stay that belongs to it. */
|
|
entryTitleOf?: (entryId: number) => string | null;
|
|
}
|
|
|
|
function dayLabel(date: string, locale: string): string {
|
|
return new Date(`${date}T00:00:00`).toLocaleDateString(locale, { day: 'numeric', month: 'short' });
|
|
}
|
|
|
|
function timeLabel(iso: string, locale: string): string {
|
|
const d = new Date(iso);
|
|
return Number.isNaN(d.getTime()) ? '' : d.toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit' });
|
|
}
|
|
|
|
function OpenEntry({ entryId, onOpenEntry }: { entryId: number; onOpenEntry?: (entryId: string) => void }) {
|
|
const { t } = useTranslation();
|
|
if (!onOpenEntry) return null;
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={() => onOpenEntry(String(entryId))}
|
|
className="inline-flex items-center gap-1 self-start rounded-lg bg-accent px-2.5 py-1.5 font-semibold text-accent-text hover:opacity-90"
|
|
style={fs(11.5, 'body')}
|
|
>
|
|
<NotebookPen size={12} strokeWidth={2.2} aria-hidden />
|
|
{t('journey.map.pick.openEntry')}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
function PlaceDetail({ place, onOpenEntry }: { place: JourneyMapPlace; onOpenEntry?: (entryId: string) => void }) {
|
|
const { t, locale } = useTranslation();
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
<SoftPill icon={<CalendarRange size={11} strokeWidth={2} />}>{t('journey.map.pick.planned')}</SoftPill>
|
|
<WhiteBadge>{place.trip_title}</WhiteBadge>
|
|
{place.days.map((day) => (
|
|
<WhiteBadge key={day}>{dayLabel(day, locale)}</WhiteBadge>
|
|
))}
|
|
{place.time && <WhiteBadge icon={<Clock size={10} strokeWidth={2} />}>{place.time}</WhiteBadge>}
|
|
</div>
|
|
{place.entry_id !== null ? (
|
|
<OpenEntry entryId={place.entry_id} onOpenEntry={onOpenEntry} />
|
|
) : (
|
|
<p className="text-content-muted" style={fs(11.5, 'body')}>
|
|
{t('journey.map.pick.notWritten')}
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function StayDetail({
|
|
stay,
|
|
onOpenEntry,
|
|
entryTitleOf,
|
|
}: { stay: JourneyMapStay } & Pick<Props, 'onOpenEntry' | 'entryTitleOf'>) {
|
|
const { t, locale } = useTranslation();
|
|
const entryTitle = entryTitleOf?.(stay.entry_id);
|
|
const from = timeLabel(stay.started_at, locale);
|
|
const to = timeLabel(stay.ended_at, locale);
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
<SoftPill tone="info" icon={<MapPin size={11} strokeWidth={2} />}>
|
|
{t('journey.map.pick.stay')}
|
|
</SoftPill>
|
|
{from && (
|
|
<WhiteBadge icon={<Clock size={10} strokeWidth={2} />}>
|
|
{to ? t('journey.map.pick.timeRange', { from, to }) : from}
|
|
</WhiteBadge>
|
|
)}
|
|
</div>
|
|
{entryTitle && (
|
|
<p className="truncate text-content-secondary" style={fs(11.5, 'body')}>
|
|
{t('journey.map.pick.partOf', { title: entryTitle })}
|
|
</p>
|
|
)}
|
|
<OpenEntry entryId={stay.entry_id} onOpenEntry={onOpenEntry} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function markName(mark: JourneyMapMark): string {
|
|
return mark.kind === 'plan' ? mark.place.name : mark.stay.name;
|
|
}
|
|
|
|
/**
|
|
* What a mark picked on the journey map is (JRN-04), docked over the map's
|
|
* corner: a planned place with its trip, its days and the entry written about
|
|
* it, or a stay with its times and the report it belongs to. Several marks on
|
|
* one spot come as a list first, each with what kind it is, so overlapping
|
|
* hits stay reachable one by one.
|
|
*/
|
|
export default function JourneyMapPickCard({ marks, onClose, onOpenEntry, entryTitleOf }: Props) {
|
|
const { t, locale } = useTranslation();
|
|
const [picked, setPicked] = useState<JourneyMapMark | null>(marks.length === 1 ? marks[0] : null);
|
|
|
|
useEffect(() => {
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') onClose();
|
|
};
|
|
window.addEventListener('keydown', onKey);
|
|
return () => window.removeEventListener('keydown', onKey);
|
|
}, [onClose]);
|
|
|
|
const title = picked ? markName(picked) : t('journey.map.pick.here');
|
|
return (
|
|
<div
|
|
role="dialog"
|
|
aria-label={title}
|
|
data-testid="journey-map-pick"
|
|
className="pointer-events-auto flex w-[280px] max-w-[calc(100%-24px)] flex-col gap-2 rounded-xl border border-edge bg-surface-card p-3 shadow-lg"
|
|
>
|
|
<div className="flex items-start gap-1.5">
|
|
{picked && marks.length > 1 && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setPicked(null)}
|
|
aria-label={t('journey.map.pick.back')}
|
|
className="-ms-1 grid h-6 w-6 flex-none place-items-center rounded-md text-content-muted hover:bg-surface-hover"
|
|
>
|
|
<ChevronLeft size={14} strokeWidth={2.2} className="rtl:rotate-180" />
|
|
</button>
|
|
)}
|
|
<span className="min-w-0 flex-1 font-semibold text-content" style={fs(13, 'body')}>
|
|
{title}
|
|
</span>
|
|
{!picked && <WhiteBadge>{marks.length}</WhiteBadge>}
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
aria-label={t('common.close')}
|
|
className="grid h-6 w-6 flex-none place-items-center rounded-md text-content-muted hover:bg-surface-hover"
|
|
>
|
|
<X size={14} strokeWidth={2.2} />
|
|
</button>
|
|
</div>
|
|
{picked?.kind === 'plan' && <PlaceDetail place={picked.place} onOpenEntry={onOpenEntry} />}
|
|
{picked?.kind === 'stay' && (
|
|
<StayDetail stay={picked.stay} onOpenEntry={onOpenEntry} entryTitleOf={entryTitleOf} />
|
|
)}
|
|
{!picked && (
|
|
<ul className="-mx-1 flex max-h-[220px] flex-col overflow-y-auto">
|
|
{marks.map((mark) => (
|
|
<li key={mark.key}>
|
|
<button
|
|
type="button"
|
|
onClick={() => setPicked(mark)}
|
|
className="flex w-full items-center gap-2 rounded-lg px-1.5 py-1.5 text-start hover:bg-surface-hover"
|
|
>
|
|
<span aria-hidden className="h-2.5 w-2.5 flex-none rounded-full" style={{ background: mark.color }} />
|
|
<span className="min-w-0 flex-1 truncate text-content" style={fs(12, 'body')}>
|
|
{markName(mark)}
|
|
</span>
|
|
<SoftPill tone={mark.kind === 'plan' ? 'neutral' : 'info'}>
|
|
{mark.kind === 'plan' ? t('journey.map.pick.planned') : t('journey.map.pick.stay')}
|
|
</SoftPill>
|
|
{mark.kind === 'plan' && mark.place.days[0] && (
|
|
<WhiteBadge>{dayLabel(mark.place.days[0], locale)}</WhiteBadge>
|
|
)}
|
|
<ChevronRight size={13} strokeWidth={2} className="flex-none text-content-faint rtl:rotate-180" />
|
|
</button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|