mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
3659297af1
A route planned in Google My Maps or Google Earth can now become a tour without converting it to GPX first. The import button and a file dropped on the tours workspace take .kml and .kmz files beside .gpx, through the same preview, the same import routes and the same transaction as a GPX file. The preview shows every line of the file on its own, numbered, with its distance and heights; two lines of one placemark are numbered after its name and never joined. Heights count only where they are heights: kept for an absolute line, dropped for any other altitude mode and for the all-zero placeholder My Maps writes, so a missing height stays unknown. What the file holds besides its lines (pins, areas, overlays, links to other files) is counted in a dashed card, and nothing is fetched from elsewhere. The KML reading reuses the place import's codec. A KMZ only ever has its KML entry unpacked, counted byte by byte against the decompressed cap whatever its header claims. A file that cannot be read is refused with its reason (damaged, an archive without KML, too large unpacked), and the toast names the file and the reason. A KML or KMZ import keeps the gpx origin of a file import; its provenance names the real format from the stored file name.
171 lines
6.9 KiB
TypeScript
171 lines
6.9 KiB
TypeScript
import type { TourFileLeftOut, TourGpxImportFields, TourGpxPreviewTrack, TourListItem } from '@trek/shared';
|
|
import { useCallback, useState } from 'react';
|
|
import { useTranslation } from '../../i18n';
|
|
import { tourRepo } from '../../repo/tourRepo';
|
|
import { useTripStore } from '../../store/tripStore';
|
|
import { getApiErrorCode, getApiErrorMessage } from '../../utils/apiError';
|
|
import { useToast } from '../shared/Toast';
|
|
|
|
/** The server refuses anything larger; saying so first spares the upload. */
|
|
const MAX_TOUR_FILE_BYTES = 10 * 1024 * 1024;
|
|
/** The files a tour can be imported from: GPX, and since IO-02 KML and the KMZ archive it comes zipped in. */
|
|
const TOUR_FILE_EXTENSIONS = ['.gpx', '.kml', '.kmz'];
|
|
const NO_TRACK = 'No track or route found in GPX file';
|
|
|
|
const isTourFile = (file: Pick<File, 'name'>) =>
|
|
TOUR_FILE_EXTENSIONS.some((extension) => file.name.toLowerCase().endsWith(extension));
|
|
|
|
type Translate = ReturnType<typeof useTranslation>['t'];
|
|
|
|
/** The toast that names a file the server could not read, by the reason it gave. */
|
|
function fileErrorMessage(error: unknown, name: string, t: Translate): string {
|
|
switch (getApiErrorCode(error)) {
|
|
case 'malformed':
|
|
case 'archive_invalid':
|
|
return t('tours.import.unreadableIn', { name });
|
|
case 'archive_without_kml':
|
|
return t('tours.import.archiveNoKmlIn', { name });
|
|
case 'archive_too_large':
|
|
return t('tours.import.archiveTooLargeIn', { name });
|
|
case 'no_track':
|
|
return t('tours.import.noTrackIn', { name });
|
|
default:
|
|
// A GPX file without a track has always been refused by its message alone.
|
|
return getApiErrorMessage(error, '') === NO_TRACK
|
|
? t('tours.import.noTrackIn', { name })
|
|
: t('tours.import.errorIn', { name });
|
|
}
|
|
}
|
|
|
|
/** One file as its preview read it: every track or route it holds, each as the import would store it. */
|
|
export interface GpxPreviewFile {
|
|
file: File;
|
|
tracks: TourGpxPreviewTrack[];
|
|
/** What a KML or KMZ file held besides its lines, which no tour is made of; absent for GPX. */
|
|
leftOut?: TourFileLeftOut;
|
|
}
|
|
|
|
interface TourGpxImportOptions {
|
|
tripId: number;
|
|
canEdit: boolean;
|
|
/** The imported place ids, so lists can leave them out of the places at once. */
|
|
onToursChanged?: (placeIds?: number[]) => void | Promise<void>;
|
|
/** The new tours, once the trip has been read again with their places. */
|
|
onImported?: (tours: TourListItem[]) => void;
|
|
}
|
|
|
|
/**
|
|
* The one way a GPX, KML or KMZ file becomes a tour, whichever control it came
|
|
* through: the import button's file picker or a file dropped onto the tours
|
|
* workspace.
|
|
*
|
|
* PAD-02: nothing is stored on choosing a file. Each file is read first and
|
|
* shown (its whole course, what it measures, which tracks the trip already
|
|
* holds); only confirming the preview imports, as the type chosen there.
|
|
* Cancelling leaves the trip as it was. A file that cannot be read is named
|
|
* in a toast and left out of the preview.
|
|
*/
|
|
export function useTourGpxImport({ tripId, canEdit, onToursChanged, onImported }: TourGpxImportOptions) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const refreshPlaces = useTripStore((state) => state.refreshPlaces);
|
|
const [importing, setImporting] = useState(false);
|
|
const [preview, setPreview] = useState<GpxPreviewFile[] | null>(null);
|
|
|
|
const fileError = useCallback(
|
|
(error: unknown, file: File) => toast.error(fileErrorMessage(error, file.name, t)),
|
|
[t, toast]
|
|
);
|
|
|
|
const importFiles = useCallback(
|
|
async (files: readonly File[]) => {
|
|
if (!canEdit || files.length === 0 || importing || preview) return;
|
|
const supported = files.filter(isTourFile);
|
|
if (supported.length < files.length) toast.error(t('tours.import.unsupported'));
|
|
const fitting = supported.filter((file) => file.size <= MAX_TOUR_FILE_BYTES);
|
|
if (fitting.length < supported.length)
|
|
toast.error(t('places.importFileTooLarge', { maxMb: MAX_TOUR_FILE_BYTES / 1024 / 1024 }));
|
|
if (fitting.length === 0) return;
|
|
|
|
setImporting(true);
|
|
const read: GpxPreviewFile[] = [];
|
|
try {
|
|
for (const file of fitting) {
|
|
try {
|
|
const preview = await tourRepo.previewGpx(tripId, file);
|
|
read.push({ file, tracks: preview.tracks, leftOut: preview.left_out });
|
|
} catch (error: unknown) {
|
|
fileError(error, file);
|
|
}
|
|
}
|
|
if (read.length > 0) setPreview(read);
|
|
} finally {
|
|
setImporting(false);
|
|
}
|
|
},
|
|
[canEdit, fileError, importing, preview, t, toast, tripId]
|
|
);
|
|
|
|
/** The previewed files imported as the chosen type, summed up in one set of toasts. */
|
|
const confirm = useCallback(
|
|
async (fields: TourGpxImportFields) => {
|
|
if (!canEdit || !preview || importing) return;
|
|
setImporting(true);
|
|
const imported: TourListItem[] = [];
|
|
let skipped = 0;
|
|
let caution = false;
|
|
try {
|
|
for (const { file } of preview) {
|
|
try {
|
|
const result = await tourRepo.importGpx(tripId, file, fields);
|
|
imported.push(...result.tours);
|
|
skipped += result.skipped;
|
|
caution ||= result.caution;
|
|
} catch (error: unknown) {
|
|
fileError(error, file);
|
|
}
|
|
}
|
|
setPreview(null);
|
|
if (imported.length > 0) {
|
|
// The server's place:created skips this socket, so the places are read
|
|
// again here for the new ones and their lines. Only the places: a whole
|
|
// trip reload would blank the workspace and rebuild its map.
|
|
await onToursChanged?.(imported.map((tour) => tour.place_id));
|
|
await refreshPlaces(tripId);
|
|
toast.success(t('tours.import.success', { count: imported.length }));
|
|
if (caution) toast.info(t('tours.import.caution'));
|
|
onImported?.(imported);
|
|
}
|
|
if (skipped > 0) {
|
|
toast.warning(
|
|
imported.length > 0 ? t('tours.import.skipped', { count: skipped }) : t('tours.import.allSkipped')
|
|
);
|
|
}
|
|
} finally {
|
|
setImporting(false);
|
|
}
|
|
},
|
|
[canEdit, fileError, importing, onImported, onToursChanged, preview, refreshPlaces, t, toast, tripId]
|
|
);
|
|
|
|
/** The preview closed: nothing was stored. */
|
|
const cancel = useCallback(() => {
|
|
if (!importing) setPreview(null);
|
|
}, [importing]);
|
|
|
|
/** Opens the file picker; built per click, so no view carries a hidden input for it. */
|
|
const pick = useCallback(() => {
|
|
if (!canEdit || importing || preview) return;
|
|
const input = document.createElement('input');
|
|
input.type = 'file';
|
|
input.accept = TOUR_FILE_EXTENSIONS.join(',');
|
|
input.multiple = true;
|
|
input.addEventListener('change', () => void importFiles(Array.from(input.files ?? [])), { once: true });
|
|
input.click();
|
|
}, [canEdit, importFiles, importing, preview]);
|
|
|
|
return { importing, importFiles, pick, preview, confirm, cancel };
|
|
}
|
|
|
|
export type TourGpxImport = ReturnType<typeof useTourGpxImport>;
|