mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
Tours: day GPX import, wanderer trails and planning over an import
- GPX files drop onto the map or a day; the import lands on that day - wanderer connection per user in Integrations; the workspace offers the user's own trails only, behind an admin switch under the Tours addon - wanderer difficulty is stored on the SAC scale (T1 to T3), backfilled - a GPX or wanderer import can be planned as an editable tour, either in place (the recorded track is kept and can be restored) or as a new tour that remembers the import it came from - rails, rows and detail dialog follow the plan sidebar's cards and badges - admin addon shelves with more than one row fold, collapsed by default - reverse-geocoded waypoint names are cached on the device
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import {
|
||||
WANDERER_TOKEN_MASK,
|
||||
WANDERER_TOKEN_PREFIX,
|
||||
type WandererConnection,
|
||||
type WandererTestResponse,
|
||||
} from '@trek/shared';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { wandererErrorCode, wandererRepo } from '../../repo/wandererRepo';
|
||||
import { useToast } from '../shared/Toast';
|
||||
import { wandererErrorText } from '../Tours/wanderer/wandererErrors';
|
||||
|
||||
/** What the last test found, said in the card; null before a test. */
|
||||
export interface WandererProbe {
|
||||
ok: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
type Translate = (key: string, params?: Record<string, unknown>) => string;
|
||||
|
||||
/** What a test answered, as the line the card shows under its buttons. */
|
||||
function probeOf(result: WandererTestResponse, t: Translate): WandererProbe {
|
||||
if (!result.ok) return { ok: false, message: wandererErrorText(t, result.code ?? 'unknown') };
|
||||
const message =
|
||||
result.trailCount != null ? t('wanderer.test.okCount', { count: result.trailCount }) : t('wanderer.test.ok');
|
||||
return { ok: true, message };
|
||||
}
|
||||
|
||||
/**
|
||||
* The wanderer connection card's logic: the form, saving, testing and
|
||||
* disconnecting, with every failure turned into a sentence the reader can act
|
||||
* on. The token field is never filled from the server; blank keeps the stored one.
|
||||
*/
|
||||
export function useWandererConnection() {
|
||||
const { t } = useTranslation();
|
||||
const toast = useToast();
|
||||
const [url, setUrl] = useState('');
|
||||
const [token, setToken] = useState('');
|
||||
const [allowInsecureTls, setAllowInsecureTls] = useState(false);
|
||||
const [saved, setSaved] = useState<WandererConnection | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [testing, setTesting] = useState(false);
|
||||
const [probe, setProbe] = useState<WandererProbe | null>(null);
|
||||
|
||||
const errorText = useCallback((code: string) => wandererErrorText(t, code), [t]);
|
||||
|
||||
const apply = useCallback((connection: WandererConnection) => {
|
||||
setSaved(connection);
|
||||
setUrl(connection.url);
|
||||
setAllowInsecureTls(connection.allowInsecureTls);
|
||||
setToken('');
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
wandererRepo
|
||||
.getSettings()
|
||||
.then((connection) => {
|
||||
if (!cancelled) apply(connection);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) toast.error(t('wanderer.loadFailed'));
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [apply, t, toast]);
|
||||
|
||||
const typedToken = token.trim();
|
||||
/** A typed token that wanderer would read as no token at all, said before it is sent. */
|
||||
const tokenInvalid =
|
||||
typedToken !== '' && typedToken !== WANDERER_TOKEN_MASK && !typedToken.startsWith(WANDERER_TOKEN_PREFIX);
|
||||
const connected = !!saved?.connected;
|
||||
const canSave = !!url.trim() && !tokenInvalid && (connected || typedToken !== '');
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!canSave) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
const result = await wandererRepo.saveSettings({
|
||||
url: url.trim(),
|
||||
token: typedToken || undefined,
|
||||
allowInsecureTls,
|
||||
});
|
||||
apply(await wandererRepo.getSettings());
|
||||
setProbe(null);
|
||||
toast.success(t('wanderer.saved'));
|
||||
if (result.warning === 'private_ip') toast.info(t('wanderer.privateIp'));
|
||||
} catch (error: unknown) {
|
||||
toast.error(errorText(wandererErrorCode(error)));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [allowInsecureTls, apply, canSave, errorText, t, toast, typedToken, url]);
|
||||
|
||||
const test = useCallback(async () => {
|
||||
if (!url.trim() || tokenInvalid) return;
|
||||
setTesting(true);
|
||||
try {
|
||||
const result = await wandererRepo.test({ url: url.trim(), token: typedToken || undefined, allowInsecureTls });
|
||||
setProbe(probeOf(result, t));
|
||||
if (!typedToken) setSaved(await wandererRepo.getSettings());
|
||||
} catch (error: unknown) {
|
||||
setProbe({ ok: false, message: errorText(wandererErrorCode(error)) });
|
||||
} finally {
|
||||
setTesting(false);
|
||||
}
|
||||
}, [allowInsecureTls, errorText, t, tokenInvalid, typedToken, url]);
|
||||
|
||||
const disconnect = useCallback(async () => {
|
||||
setSaving(true);
|
||||
try {
|
||||
await wandererRepo.disconnect();
|
||||
apply(await wandererRepo.getSettings());
|
||||
setProbe(null);
|
||||
toast.success(t('wanderer.disconnected'));
|
||||
} catch (error: unknown) {
|
||||
toast.error(errorText(wandererErrorCode(error)));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [apply, errorText, t, toast]);
|
||||
|
||||
const lastCheck =
|
||||
saved?.lastCheckedAt && saved.lastCheckOk === false && saved.lastCheckError
|
||||
? errorText(saved.lastCheckError)
|
||||
: null;
|
||||
|
||||
return {
|
||||
url,
|
||||
setUrl,
|
||||
token,
|
||||
setToken,
|
||||
allowInsecureTls,
|
||||
toggleInsecureTls: () => setAllowInsecureTls((value) => !value),
|
||||
connected,
|
||||
loading,
|
||||
saving,
|
||||
testing,
|
||||
probe,
|
||||
tokenInvalid,
|
||||
canSave,
|
||||
lastCheck,
|
||||
save,
|
||||
test,
|
||||
disconnect,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user