import type { PaddleAccess, PaddleCheck, PaddleObstacleKind, TourObstacleCheck, TourObstacleEntry, TourOwnEntry, } from '@trek/shared'; import { AlertTriangle, CloudOff, LifeBuoy, ListChecks, Loader2, PencilLine, Save, WifiOff } from 'lucide-react'; import { useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react'; import { useTranslation } from '../../../i18n'; import { useSettingsStore } from '../../../store/settingsStore'; import { formatDistance } from '../../../utils/units'; import { SoftPill } from '../../Planner/planParts'; import { fs } from '../../shared/DialogShell'; import { Tooltip } from '../../shared/Tooltip'; import { FoldButton, RailCard, TourNotice } from '../tourParts'; import { FigureBadge } from './FigureBadge'; import { ObstacleEntryDialog } from './ObstacleEntryDialog'; import { ChecklistGlyph, ObstacleAccessBadge, ObstacleStatusBadges, PaddleCheckDataLine, PaddleCheckPill, } from './PaddleCheckBadges'; import { checklistRows, formatCheckDate, type ChecklistRow } from './paddleCheckModel'; import { PaddleObstacleNotice } from './PaddleObstacleNotice'; import type { PaddleChecklist, PaddleChecklistStatus } from './usePaddleChecklist'; type Translate = ReturnType['t']; /** What the card says above the rows: the tour's own state, unsaved changes, or an answer from the cache. */ type CheckNotice = Exclude | 'dirty' | 'stale'; const ICON = { size: 13, strokeWidth: 2 } as const; /** Each state's sentence and icon; while the list loads it is a status with a spinner. */ const NOTICES: Record = { new: { textKey: 'tours.paddle.check.saveFirst', icon: }, loading: { textKey: 'tours.paddle.obstacles.loading', icon: , role: 'status', }, 'offline-empty': { textKey: 'tours.paddle.check.offlineEmpty', icon: }, error: { textKey: 'tours.paddle.obstacles.error', icon: }, dirty: { textKey: 'tours.paddle.check.unsaved', icon: }, stale: { textKey: 'tours.paddle.check.stale', icon: }, }; /** A carrying way the data forbids (`access=no` or `portage:access=no`), as the access rule it runs into. */ const FORBIDDEN_WAY: PaddleAccess = { value: 'no', source: 'access', condition: null, forbidden: true, private: false }; const ROW = 'flex w-full items-start gap-2 rounded-[10px] px-1.5 py-1.5 text-start transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-accent'; const ROW_SELECTED = 'bg-surface-hover ring-1 ring-inset ring-accent'; /** The one notice that applies: the tour's state first, then unsaved route changes, then a list from the cache. */ function noticeOf({ status, dirty, stale }: Pick): CheckNotice | null { if (status === 'off') return null; if (status !== 'ready') return status; if (dirty) return 'dirty'; return stale ? 'stale' : null; } function kindLabel(kind: PaddleObstacleKind, t: Translate): string { return t(`tours.paddle.obstacle.kind.${kind}`); } /** Metres along the route in the user's distance unit. */ function useDistanceLabel(): (metres: number) => string { const unit = useSettingsStore((state) => state.settings.distance_unit) ?? 'metric'; return (metres) => formatDistance(metres / 1000, unit); } function rowKey(row: ChecklistRow, index: number): string { return row.type === 'obstacle' ? row.entry.key : `own:${row.own.kind}:${row.own.from_m}:${index}`; } /** The sentence the checklist's state calls for. */ function CheckNoticeLine({ notice, evaluatedAt }: { notice: CheckNotice; evaluatedAt: string | null }) { const { t, locale } = useTranslation(); const { textKey, icon, role } = NOTICES[notice]; const time = evaluatedAt ? formatCheckDate(evaluatedAt, locale) : ''; return ( {t(textKey, { time })} ); } /** A row of the checklist: one full width button with the glyph, the title and a line of badges that wraps between them. */ function CheckRowButton({ selected = false, dimmed = false, onClick, glyph, title, children, }: { selected?: boolean; dimmed?: boolean; onClick: () => void; glyph: ReactNode; title: string; children: ReactNode; }) { const ref = useRef(null); // An entry picked on the map comes into view in the rail. useEffect(() => { if (selected) ref.current?.scrollIntoView?.({ block: 'nearest' }); }, [selected]); return ( ); } /** The SUP advice at a lock: check a portage rather than lock through. */ function SupLockBadge() { const { t } = useTranslation(); return ( }> {t('tours.paddle.check.supLock')} ); } /** Marks a row as the paddler's own plan, never a fact from a data source. */ function OwnBadge() { const { t } = useTranslation(); return ( {t('tours.paddle.check.own')} ); } /** The obstacles an own portage carries round, each a small glyph its tooltip names. */ function CoveredChips({ keys, entries }: { keys: readonly string[]; entries: readonly TourObstacleEntry[] }) { const { t } = useTranslation(); const covered = keys .map((key) => entries.find((entry) => entry.key === key)) .filter((entry): entry is TourObstacleEntry => entry !== undefined); return ( <> {covered.map((entry) => { const label = kindLabel(entry.kind, t); return ( ); })} ); } /** A carrying way the data records as private or forbidden, in the warning tone the obstacle card gives rules. */ function ConflictBadge({ conflict }: { conflict: NonNullable }) { const { t } = useTranslation(); if (conflict === 'forbidden') return ; return {t('tours.paddle.obstacleCard.privateWay')}; } /** One obstacle: what it is, where on the route, and where its decision stands. */ function PaddleCheckRow({ entry, selected, onSelect, }: { entry: TourObstacleEntry; selected: boolean; onSelect: (entry: TourObstacleEntry) => void; }) { const { t } = useTranslation(); const distance = useDistanceLabel(); const near = entry.relation === 'near'; return ( onSelect(entry)} glyph={} title={entry.name ?? kindLabel(entry.kind, t)} > {entry.along_m !== null && {distance(entry.along_m)}} {near && {t('tours.paddle.obstacleCard.near')}} {entry.advice !== null && } ); } /** * The paddler's own portage or lock among the obstacles. A portage says how * far it carries and what it carries round; a lock the portage carries round * is dimmed and says so. */ function OwnCheckRow({ own, entries, onSelect, }: { own: TourOwnEntry; entries: readonly TourObstacleEntry[]; onSelect: (own: TourOwnEntry) => void; }) { const { t } = useTranslation(); const distance = useDistanceLabel(); const portage = own.kind === 'portage'; const bypassed = !portage && own.bypassed; return ( onSelect(own)} glyph={} title={kindLabel(own.kind, t)} > {distance(own.from_m)} {portage && } {portage && } {own.conflict && } {bypassed && {t('tours.paddle.lockBypassed')}} {own.advice !== null && } ); } /** One row along the route: an obstacle picks its entry, an own mark its place on the map. */ function RouteRow({ row, checklist, entries, }: { row: ChecklistRow; checklist: PaddleChecklist; entries: readonly TourObstacleEntry[]; }) { if (row.type === 'own') return ; return ( ); } /** * The decisions the route or the data no longer carries, folded away under * their count. A selection among them opens the fold, until the paddler folds * it again for that selection. */ function EarlierDecisions({ entries, selectedKey, onSelect, }: { entries: readonly TourObstacleEntry[]; selectedKey: string | null; onSelect: (entry: TourObstacleEntry) => void; }) { const { t } = useTranslation(); const id = useId(); const [choice, setChoice] = useState<{ open: boolean; key: string | null }>({ open: false, key: null }); const holdsSelected = entries.some((entry) => entry.key === selectedKey); const expanded = (holdsSelected && choice.key !== selectedKey) || choice.open; const label = t('tours.paddle.check.earlier'); return (
{label} {entries.length} setChoice({ open: !expanded, key: selectedKey })} />
); } /** A route without rows: nothing known where the data covers it, no data where there is none; else the data line speaks. */ function EmptyLine({ summary }: { summary: PaddleCheck | null }) { const { t, locale } = useTranslation(); let text: string | null = null; if (summary?.data === 'none') text = t('tours.paddle.obstacles.none'); else if (summary?.data === 'covered') { const date = summary.data_as_of ? formatCheckDate(summary.data_as_of, locale) : t('tours.paddle.obstacles.dateUnknown'); text = t('tours.paddle.obstacles.coveredEmpty', { date }); } if (!text) return null; return (

{text}

); } /** The rows along the route, then the earlier decisions; dimmed, never hidden, while they may not match the route. */ function CheckList({ checklist, check }: { checklist: PaddleChecklist; check: TourObstacleCheck }) { const { rows, earlier } = useMemo(() => checklistRows(check), [check]); // Dimmed while it may not match the route, and while the list it came from reads again. const dimmed = checklist.dirty || checklist.stale || checklist.status === 'loading'; return (
{rows.length > 0 ? (
    {rows.map((row, index) => (
  • ))}
) : ( )} {earlier.length > 0 && ( )}
); } /** * PAD-06 in the planner rail: the obstacle checklist of the open paddle tour. * The obstacles and the paddler's own portages and locks stand in route * order, each a row that picks it on the map. One line says what state the * list is in; while the route has unsaved changes or the list comes from the * cache, the rows stay readable but dimmed. The card closes on the line that * a checklist is no safety or legal clearance, and opens an entry the map * cannot show in a dialog of its own. */ export function PaddleCheckCard({ checklist }: { checklist: PaddleChecklist }) { const { t } = useTranslation(); if (checklist.status === 'off') return null; const notice = noticeOf(checklist); const { summary, check } = checklist; return ( <> } title={t('tours.paddle.check.title')} foldId="paddle-check" aside={summary ? : null} >
{notice && } {notice === 'new' && } {summary && } {check && }

{t('tours.paddle.check.notClearance')}

{/* Outside the card's body: an obstacle picked on the map opens its entry with the card folded too. */} {checklist.dialogKey !== null && } ); }