mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
2e1dca9fe9
Settings, Integrations, wanderer gains an optional background check of the trails you imported, off until you switch it on. While on, TREK reads the short record of each such trail at most once a day (never its GPX file) and leaves a hint when the trail may have moved on: "may have changed" when only wanderer's change time moved, "changed" when its name or description proves it, and "not checkable" when the trail is gone, no longer yours or carries no change time. Nothing is ever said to be unchanged. A new hint arrives as a notification that opens the tour (a new ?place= link in the planner); the tour's provenance shows it with the source check right beside it, and the card lists all open hints in a dialog, each leading to its tour. The check reads the source in full as before and spends the hint; taking a change in stays the confirmed reimport. No tour is ever changed or deleted. The check only asks through your own connection, only about trails your account imported into trips you can still open, and stops when you switch it off, remove the connection, wanderer refuses the token (until a test passes or a new token is saved) or the admin switches wanderer off; hints and snapshots stay. A run asks at most 25 connections for at most 10 records each, one after another, and an instance that fails (an outage, a rate limit) is left alone for an hour, doubling up to two days. Server: migration 046120 adds the switch and the run bookkeeping to wanderer_connections and the tour_source_hints table (one row per snapshot, for its importer, gone with it). WandererWatchService and an hourly wanderer-watch job on the CronRegistrar live in the tours domain and read through WandererService; GET and PUT /api/integrations/wanderer/watch and GET /api/trips/:tripId/tours/wanderer/sources/:placeId/hint (place_edit, like the check) answer with @trek/shared contracts. A new user notification event, wanderer_source_hint, can be muted per channel. Saving a new token now clears what the last check said about the old one. There is no MCP tool, as for the rest of the personal wanderer connection. All 27 locales carry the strings, and the admin wiki page says what the check does.
108 lines
3.9 KiB
TypeScript
108 lines
3.9 KiB
TypeScript
import type { TourListItem, TourSourceCheckResponse } from '@trek/shared';
|
|
import { useCallback, useState } from 'react';
|
|
import { useTranslation } from '../../../i18n';
|
|
import { wandererErrorCode, wandererRepo } from '../../../repo/wandererRepo';
|
|
import { useToast } from '../../shared/Toast';
|
|
import { wandererErrorText } from '../wanderer/wandererErrors';
|
|
|
|
interface SourceCheckOptions {
|
|
tripId: number;
|
|
placeId: number;
|
|
/** The new snapshot a confirmed reimport brought in, for the lists and the map to take it in. */
|
|
onReimported?: (tour: TourListItem) => Promise<void> | void;
|
|
}
|
|
|
|
/**
|
|
* MF-03: asking the source of a wanderer snapshot, on purpose, whether the
|
|
* trail changed since it came in. The check only reads; nothing in the trip
|
|
* changes until the user confirms a reimport, which goes the way every
|
|
* wanderer import goes and comes in as a new snapshot beside this one.
|
|
*/
|
|
export function useSourceCheck({ tripId, placeId, onReimported }: SourceCheckOptions) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const [checking, setChecking] = useState(false);
|
|
const [reimporting, setReimporting] = useState(false);
|
|
// The answer and the question belong to the tour they were asked for:
|
|
// another tour shown in the same dialog starts unchecked.
|
|
// `answered` stays once the source answered for this tour, through a reimport
|
|
// too: the server then holds no background hint on it any more (WAN-02).
|
|
const [state, setState] = useState<{
|
|
placeId: number;
|
|
result: TourSourceCheckResponse | null;
|
|
confirming: boolean;
|
|
answered: boolean;
|
|
}>({ placeId, result: null, confirming: false, answered: false });
|
|
const result = state.placeId === placeId ? state.result : null;
|
|
const confirming = state.placeId === placeId && state.confirming;
|
|
const answered = state.placeId === placeId && state.answered;
|
|
const setResult = useCallback(
|
|
(next: TourSourceCheckResponse | null) =>
|
|
setState((current) => ({
|
|
placeId,
|
|
result: next,
|
|
confirming: false,
|
|
answered: (current.placeId === placeId && current.answered) || next !== null,
|
|
})),
|
|
[placeId]
|
|
);
|
|
const setConfirming = useCallback(
|
|
(next: boolean) =>
|
|
setState((current) => ({
|
|
...current,
|
|
placeId,
|
|
confirming: next,
|
|
answered: current.placeId === placeId && current.answered,
|
|
})),
|
|
[placeId]
|
|
);
|
|
|
|
const check = useCallback(async () => {
|
|
if (checking) return;
|
|
setChecking(true);
|
|
try {
|
|
setResult(await wandererRepo.checkSource(tripId, placeId));
|
|
} catch (error: unknown) {
|
|
setResult(null);
|
|
toast.error(wandererErrorText(t, wandererErrorCode(error)));
|
|
} finally {
|
|
setChecking(false);
|
|
}
|
|
}, [checking, placeId, setResult, t, toast, tripId]);
|
|
|
|
const reimport = useCallback(async () => {
|
|
const payload = result?.reimport;
|
|
if (!payload || reimporting) return;
|
|
setReimporting(true);
|
|
try {
|
|
const { tour } = await wandererRepo.importTrail(tripId, payload);
|
|
setConfirming(false);
|
|
setResult(null);
|
|
await onReimported?.(tour);
|
|
toast.success(t('tours.provenance.reimported'));
|
|
} catch (error: unknown) {
|
|
setConfirming(false);
|
|
toast.error(wandererErrorText(t, wandererErrorCode(error)));
|
|
} finally {
|
|
setReimporting(false);
|
|
}
|
|
}, [onReimported, reimporting, result, setConfirming, setResult, t, toast, tripId]);
|
|
|
|
return {
|
|
checking,
|
|
result,
|
|
check,
|
|
/** The source answered a check of this tour; a background hint on it is spent. */
|
|
answered,
|
|
/** A reimport can be offered: the trail was read and differs, and someone takes the new snapshot in. */
|
|
canReimport: !!result?.reimport && !!onReimported,
|
|
confirming,
|
|
askReimport: useCallback(() => setConfirming(true), [setConfirming]),
|
|
cancelReimport: useCallback(() => setConfirming(false), [setConfirming]),
|
|
reimporting,
|
|
reimport,
|
|
};
|
|
}
|
|
|
|
export type SourceCheck = ReturnType<typeof useSourceCheck>;
|