Files
TREK/client/src/components/Journey/useJourneyGpxImport.ts
T
Maurice ede109dce8 feat(journey): complete sharing, photo drafts and Studio maps
Connect the Journey backend to the web and mobile UI, including public access and subscriptions, day recordings, photo drafts and per-map framing. Preserve legacy recording responses and cover the new behavior in every locale.
2026-11-30 16:56:18 +01:00

93 lines
3.4 KiB
TypeScript

import type {
JourneyGpxImportOptions,
JourneyGpxImportResult,
JourneyRecordedDay,
JourneyRecordingSource,
} from '@trek/shared';
import { useCallback, useState } from 'react';
import { useTranslation } from '../../i18n';
import { journeyRecordingsRepo } from '../../repo/journeyRecordingsRepo';
import { useToast } from '../shared/Toast';
import { gpxImportErrorText } from './journeyRecordingModel';
interface GpxImportState {
busy: boolean;
/** Why the last file gave no days, translated; null while nothing failed. */
error: string | null;
/** What the last file kept, day by day, with the points it had to leave out. */
result: JourneyGpxImportResult | null;
}
const IDLE: GpxImportState = { busy: false, error: null, result: null };
/**
* Reading a GPX file into a journey as recorded days (#3085), for the desktop
* page and the phone screen alike. The days the server kept are handed to
* `onKept` at once, so the map draws them without asking again; the other
* people on the journey hear of them through `journey:recording:updated`.
* Online only: the server cuts the file into days.
*/
export function useJourneyGpxImport(
journeyId: number | null,
onKept: (days: readonly JourneyRecordedDay[]) => void,
onRemoved?: (date: string, source?: JourneyRecordingSource) => void
) {
const { t } = useTranslation();
const toast = useToast();
const [state, setState] = useState<GpxImportState>(IDLE);
/** Reads the file, `date` keeping only that local day; answers what was kept, or null when nothing was. */
const importGpx = useCallback(
async (
file: Blob,
options?: JourneyGpxImportOptions,
onFailure?: (message: string) => void
): Promise<JourneyGpxImportResult | null> => {
if (journeyId === null) return null;
setState({ busy: true, error: null, result: null });
try {
const result = await journeyRecordingsRepo.importGpx(journeyId, file, options);
onKept(result.days.map(({ day }) => day));
setState({ busy: false, error: null, result });
return result;
} catch (err) {
const message = gpxImportErrorText(err, t);
setState({ busy: false, error: message, result: null });
onFailure?.(message);
return null;
}
},
[journeyId, onKept, t]
);
const reset = useCallback(() => setState(IDLE), []);
/** One day's route from a file, picked in that day's header; says how it went in a toast. */
const importDayFile = useCallback(
async (file: Blob, date: string) => {
const result = await importGpx(file, { date }, toast.error);
if (!result) return;
toast.success(t('journey.recording.gpx.imported', { count: result.days.length }));
if (result.untimedPoints > 0) toast.info(t('journey.recording.gpx.untimed', { count: result.untimedPoints }));
},
[importGpx, toast, t]
);
/** Drops the day's GPX route; a Dawarich route of the same day stays. */
const removeDay = useCallback(
async (date: string) => {
if (journeyId === null) return;
try {
await journeyRecordingsRepo.remove(journeyId, date, 'gpx');
onRemoved?.(date, 'gpx');
toast.success(t('journey.recording.removedToast'));
} catch {
toast.error(t('journey.recording.gpx.removeFailed'));
}
},
[journeyId, onRemoved, toast, t]
);
return { ...state, importGpx, importDayFile, removeDay, reset };
}