import type { TourFileLeftOut, TourGpxImportFields } from '@trek/shared'; import { AlertTriangle, Ban, Clock, FileDown, Layers, MapPin, Shapes } from 'lucide-react'; import { useId, useMemo } from 'react'; import { OFM_POSITRON } from '../../constants/mapDefaults'; import { useTileUrl } from '../../hooks/useTileUrl'; import { useTranslation } from '../../i18n'; import { useSettingsStore } from '../../store/settingsStore'; import { formatDistance, formatElevation } from '../../utils/units'; import { MapViewAuto } from '../Map/MapViewAuto'; import { SoftPill } from '../Planner/planParts'; import { DialogButton, DialogFooter, DialogHeader, DialogSection, DialogShell, DialogTile, FooterSpacer, fs, NEUTRAL_TINT, } from '../shared/DialogShell'; import { recordedDay, recordedMinutes } from './activity/tourActivityModel'; import { importFields, importMissing, useGpxImportChoices } from './gpxImportChoices'; import { GpxImportTypeSection } from './GpxImportTypeSection'; import { InfoDot, TourNotice } from './tourParts'; import { formatPlannedTourDuration } from './tourPresentation'; import type { GpxPreviewFile, TourGpxImport } from './useTourGpxImport'; const BADGE = 'inline-flex flex-none items-center rounded-full bg-surface-secondary px-2 font-geist tabular-nums leading-[20px] text-content-secondary'; /** ACT-01: when a track was recorded and how long it took, where every point carried a time. */ function RecordedBadge({ track }: { track: GpxPreviewFile['tracks'][number] }) { const { locale } = useTranslation(); const minutes = recordedMinutes(track.started_at, track.ended_at); const day = recordedDay(track.started_at, locale); if (minutes === null || !day) return null; return ( {day} {formatPlannedTourDuration(minutes)} ); } /** * One track of the file as a row: its number, its name, and what it measures * as badges; a track the trip holds says so. The number keeps the lines of a * file apart, they are imported one tour each and never joined. */ function PreviewTrackRow({ name, number, track, }: { name: string; number: number; track: GpxPreviewFile['tracks'][number]; }) { const { t } = useTranslation(); const unit = useSettingsStore((state) => state.settings.distance_unit) ?? 'metric'; const heights = track.elevation_gain_m != null && track.elevation_loss_m != null; return (
  • {track.name || name} {track.duplicate && {t('tours.import.previewDuplicate')}}
    {formatDistance(track.distance_km, unit)} {heights ? ( <> ↑ {formatElevation(track.elevation_gain_m ?? 0, unit)} ↓ {formatElevation(track.elevation_loss_m ?? 0, unit)} ) : ( {t('tours.import.previewNoHeights')} )} {t('tours.import.previewPoints', { count: track.point_count })}
  • ); } /** What all the files held besides their lines, summed by kind; null when they held nothing else. */ function leftOutOf(files: readonly GpxPreviewFile[]): TourFileLeftOut | null { const sum = { points: 0, areas: 0, other: 0 }; for (const { leftOut } of files) { if (!leftOut) continue; sum.points += leftOut.points; sum.areas += leftOut.areas; sum.other += leftOut.other; } return sum.points + sum.areas + sum.other > 0 ? sum : null; } /** * IO-02: what a KML or KMZ file held besides its lines, named before the * import so nothing goes missing unseen: a dashed card with each kind as a * counted badge, and why in the info dot. */ function PreviewLeftOut({ files }: { files: readonly GpxPreviewFile[] }) { const { t } = useTranslation(); const leftOut = leftOutOf(files); if (!leftOut) return null; const kinds = [ { key: 'points', count: leftOut.points, icon: MapPin, labelKey: 'tours.import.leftOutPoints' }, { key: 'areas', count: leftOut.areas, icon: Shapes, labelKey: 'tours.import.leftOutAreas' }, { key: 'other', count: leftOut.other, icon: Layers, labelKey: 'tours.import.leftOutOther' }, ].filter(({ count }) => count > 0); return (
    ); } /** * PAD-02: a GPX file shown before anything is stored. The whole course on a * map, every track with its measures, the tracks the trip already holds named * as skipped, and the type to import them as; a paddle import names its boat * and water, as a planned paddle tour does, a ski import its discipline * (SKI-02); a ride (HOR-02) says before anything is stored that its line stays * as recorded, gets no riding time and is not checked for access. Closing it * stores nothing. `initial` presets the type, as a recording imported for a * tour takes that tour's (ACT-01); left out, it is a hike. A KML or KMZ file * is shown the same way, with what it holds besides (IO-02). */ export default function TourGpxPreviewDialog({ gpxImport, initial, }: { gpxImport: TourGpxImport; initial?: TourGpxImportFields; }) { const { preview } = gpxImport; if (!preview) return null; return ; } const HIKE: TourGpxImportFields = { tour_type: 'hike' }; function PreviewDialog({ gpxImport, files, initial, }: { gpxImport: TourGpxImport; files: GpxPreviewFile[]; initial: TourGpxImportFields; }) { const { t } = useTranslation(); const labelId = useId(); const tileUrl = useTileUrl(OFM_POSITRON); const choices = useGpxImportChoices(initial); const tracks = files.flatMap((file) => file.tracks.map((track) => ({ file: file.file, track }))); const fresh = tracks.filter(({ track }) => !track.duplicate).length; const lines = useMemo( () => files.flatMap((file) => file.tracks.map((track) => track.geometry.map(([lat, lng]): [number, number] => [lat, lng])) ), [files] ); const focusPoints = useMemo(() => lines.flat(), [lines]); const canImport = fresh > 0 && importMissing(choices.chosen) === null && !gpxImport.importing; const fields = importFields(choices.chosen); const title = files.length === 1 ? files[0].file.name : t('tours.import.previewFiles', { count: files.length }); return ( } tint={NEUTRAL_TINT} labelId={labelId} onClose={gpxImport.cancel} eyebrow={t('tours.import.previewTitle')} title={title} sub={t('tours.import.previewHint')} subWraps /> } footer={ {t('common.cancel')} } disabled={!canImport} onClick={() => void gpxImport.confirm(fields)} > {t('tours.import.previewConfirm', { count: fresh })} } >
    file.name).join('|')} followSelection={false} />
      {tracks.map(({ file, track }, index) => ( ))}
    {fresh === 0 && (
    }> {t('tours.import.previewAllDuplicates')}
    )}
    ); }