Files
TREK/client/src/components/Planner/usePlacesColumnFileImport.ts
T
Maurice 31859651d7 feat(help): rewrite the help center for v5 with a Tours section, new pictures in every language, and fix the tour planner bugs it surfaced
Every help screen checked against the v5 redesign. Tours is documented for
the first time, as a screen of its own with twelve screens under it, and
notifications get help of their own. All pictures are taken again on a
realistic demo trip, with short loops where a still cannot show the motion.
The English is plain and every language is written naturally rather than
word for word.

The switcher shows two levels at most, a screen's overview lists its
sub-screens as cards above its guides, each wiki page is linked once, and
the help sidebar is wider.

Fixes found along the way: Save to Collection keeps a tour's route, a track
dropped on the places column opens the Tours import, tour Share on desktop,
the unsaved tour draft lives on the account, a route click no longer adds
an end point, Trip places leaves the trip's tours out, Escape closes a
stage's day menu, confirms before destructive deletes, DWD warnings sharing
one id kept apart, a booking's date moves with it, and smaller fixes across
notifications, bookings, the day plan, stays, Atlas, Collections, Vacay and
the admin screens.
2026-10-22 22:29:35 +02:00

87 lines
3.3 KiB
TypeScript

import type { TourListItem } from '@trek/shared';
import { useState } from 'react';
import { useTranslation } from '../../i18n';
import { useToast } from '../shared/Toast';
import { isTourFile, useTourGpxImport } from '../Tours/useTourGpxImport';
/** Stable identity: the dialog reads the files on opening, and a fresh [] would open it again on every render. */
const NO_FILES: readonly File[] = [];
export interface PlacesColumnFileImportOptions {
tripId: number;
/** The Tours addon: with it on, a line belongs to a tour and the file goes to the Tours import first. */
toursEnabled: boolean;
canEdit: boolean;
/** The imported tours' place ids, so the places pool leaves them out at once. */
onToursChanged?: (placeIds?: number[]) => void | Promise<void>;
/** The new tours are in, after the trip's places were read again. */
onImported?: (tours: TourListItem[]) => void;
}
/**
* Where a file picked or dropped on the places column goes, one answer for the
* desktop column and the phone's import sheet.
*
* With the Tours addon off nothing changes: the file opens the places import,
* the first one of a drop as ever, and an empty pick opens it without one.
*
* With it on, a GPX, KML or KMZ file holds a route or a track, and tracks are
* tours now, so it goes into the Tours import preview, the same dialog the
* Tours tab opens. A file the preview finds no line in is a list of points,
* which the places import has always taken: it opens there, once the preview
* is closed, with those files already chosen. A file neither reads is named in
* a toast instead of being dropped without a word.
*/
export function usePlacesColumnFileImport({
tripId,
toursEnabled,
canEdit,
onToursChanged,
onImported,
}: PlacesColumnFileImportOptions) {
const { t } = useTranslation();
const toast = useToast();
// null while the places import is closed; [] for an opening without a file.
const [placesFiles, setPlacesFiles] = useState<readonly File[] | null>(null);
const tourImport = useTourGpxImport({
tripId,
canEdit: canEdit && toursEnabled,
onToursChanged,
onImported,
onNoTrack: setPlacesFiles,
});
/** A drop on the column, or a pick from the phone's sheet. */
const takeFiles = (files: readonly File[]) => {
if (!canEdit || files.length === 0) return;
if (!toursEnabled) {
setPlacesFiles([files[0]]);
return;
}
const routed = files.filter(isTourFile);
if (routed.length < files.length) toast.error(t('places.importFileUnsupported'));
if (routed.length > 0) void tourImport.importFiles(routed);
};
/** The column's "Import file" entry: the Tours file picker with the addon on, the places import without it. */
const pickFile = () => {
if (!canEdit) return;
if (toursEnabled) tourImport.pick();
else setPlacesFiles(NO_FILES);
};
const setPlacesImportOpen = (open: boolean) => setPlacesFiles(open ? NO_FILES : null);
const closePlacesImport = () => setPlacesFiles(null);
return {
tourImport,
// Never over the Tours preview: a mixed drop shows its tracks first, then the points.
placesImportOpen: placesFiles !== null && !tourImport.preview && !tourImport.importing,
placesImportFiles: placesFiles ?? NO_FILES,
setPlacesImportOpen,
closePlacesImport,
takeFiles,
pickFile,
};
}