mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
69eacd5316
The MapLibre style picker offers two more entries after the OpenFreeMap styles: OpenFreeMap that follows the light or dark theme (trek:ofm-auto), and the user's own map template (trek:template), which brings the template field under the picker, and the CARTO key for a CARTO template. Where the trip map will draw the template with the classic 2D map (Amap, a host the CSP does not name) the form says so and the preview shows that map. The preview draws exactly what the trip map will. Every place the forms load or save a MapLibre style keeps a keyword now, where it used to turn it into Liberty on the first unchanged save: the user form on desktop and phone (load, save, the phone's mirrored field, a provider switch there and back) and the admin defaults (provider switch, preset pick, typed style). The rules live in mapSettingsModel, so both shells render from one decision: - the template keyword with an empty template saves Liberty; - switching from Leaflet with a template and an empty MapLibre slot preselects the template, shown and not saved until Save; - Leaflet never writes a keyword into the Mapbox slot, and Mapbox shows its default for one. New strings in all locales.
223 lines
7.6 KiB
TypeScript
223 lines
7.6 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import { adminApi } from '../../api/client';
|
|
import { useWritesInOrder } from '../../hooks/useWritesInOrder';
|
|
import { useTranslation } from '../../i18n';
|
|
import { useAuthStore } from '../../store/authStore';
|
|
import { useSettingsStore } from '../../store/settingsStore';
|
|
import type { DateFormat, DistanceUnit, Place, WeekStart } from '../../types';
|
|
import { normalizeTileUrl } from '../../utils/tileUrl';
|
|
import { defaultStyleForProvider, styleSettingKey } from '../Map/glProviders';
|
|
import {
|
|
type MapProvider,
|
|
glStylePresetsFor,
|
|
mapboxOffered,
|
|
mapPresetsFor,
|
|
normalizeGlSelection,
|
|
normalizeProvider,
|
|
styleForProvider,
|
|
} from '../Settings/mapSettingsModel';
|
|
import { useToast } from '../shared/Toast';
|
|
import type { RoutingDefaults } from './RoutingInstanceFields';
|
|
|
|
export type Defaults = RoutingDefaults & {
|
|
temperature_unit?: string;
|
|
distance_unit?: DistanceUnit;
|
|
dark_mode?: string | boolean;
|
|
time_format?: string;
|
|
week_start?: WeekStart;
|
|
date_format?: DateFormat;
|
|
default_currency?: string;
|
|
blur_booking_codes?: boolean;
|
|
map_tile_url?: string;
|
|
carto_api_key?: string;
|
|
map_provider?: string;
|
|
mapbox_access_token?: string;
|
|
mapbox_style?: string;
|
|
maplibre_style?: string;
|
|
mapbox_3d_enabled?: boolean;
|
|
mapbox_quality_mode?: boolean;
|
|
};
|
|
|
|
export interface DefaultUserSettingsOptions {
|
|
/** How a failed save or reset turns into the toast's text; each shell keeps its own. */
|
|
errorMessage: (err: unknown, fallback: string) => string;
|
|
/** The preview place's creation stamp; each shell keeps its own format. */
|
|
previewCreatedAt: () => string;
|
|
}
|
|
|
|
/**
|
|
* The admin's defaults for every user, behind both admin shells (the desktop tab and
|
|
* the phone screen render their own markup over this): loading them, the per change
|
|
* auto save, the reset to the built in value, and the map fields that are edited
|
|
* locally and saved on blur or on a pick.
|
|
*/
|
|
export function useDefaultUserSettings({ errorMessage, previewCreatedAt }: DefaultUserSettingsOptions) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const [defaults, setDefaults] = useState<Defaults>({});
|
|
const [loaded, setLoaded] = useState(false);
|
|
const [mapTileUrl, setMapTileUrl] = useState('');
|
|
const managed = useAuthStore((s) => s.managed);
|
|
const hasAmapKey = useAuthStore((s) => s.hasAmapKey);
|
|
// The operator's token reaches the admin's own settings the same way it reaches everybody's.
|
|
const operatorMapboxToken = useSettingsStore((s) => s.settings.mapbox_access_token);
|
|
const [mapboxToken, setMapboxToken] = useState('');
|
|
const [cartoKey, setCartoKey] = useState('');
|
|
const [mapboxStyle, setMapboxStyle] = useState('');
|
|
|
|
useEffect(() => {
|
|
adminApi
|
|
.getDefaultUserSettings()
|
|
.then((data: Defaults) => {
|
|
const provider = normalizeProvider(data.map_provider);
|
|
setDefaults(data);
|
|
setMapTileUrl(normalizeTileUrl(data.map_tile_url || ''));
|
|
setMapboxToken(data.mapbox_access_token || '');
|
|
setCartoKey(data.carto_api_key || '');
|
|
setMapboxStyle(
|
|
provider === 'leaflet'
|
|
? data.mapbox_style || ''
|
|
: styleForProvider(provider, provider === 'maplibre-gl' ? data.maplibre_style : data.mapbox_style)
|
|
);
|
|
setLoaded(true);
|
|
})
|
|
.catch(() => setLoaded(true));
|
|
}, []);
|
|
|
|
// A field's save on blur and the reset clicked right after it reach the server in
|
|
// that order, or the save landing last would store the old value again.
|
|
const inOrder = useWritesInOrder();
|
|
|
|
const save = (patch: Partial<Defaults>) =>
|
|
inOrder(async () => {
|
|
try {
|
|
const updated = await adminApi.updateDefaultUserSettings(patch as Record<string, unknown>);
|
|
setDefaults(updated);
|
|
toast.success(t('admin.defaultSettings.saved'));
|
|
} catch (err: unknown) {
|
|
toast.error(errorMessage(err, t('common.error')));
|
|
}
|
|
});
|
|
|
|
const reset = (key: keyof Defaults) =>
|
|
inOrder(async () => {
|
|
try {
|
|
const updated = await adminApi.updateDefaultUserSettings({ [key]: null });
|
|
setDefaults(updated);
|
|
if (key === 'map_tile_url') setMapTileUrl('');
|
|
if (key === 'mapbox_access_token') setMapboxToken('');
|
|
if (key === 'carto_api_key') setCartoKey('');
|
|
if (key === 'mapbox_style' || key === 'maplibre_style') {
|
|
const provider = normalizeProvider(defaults.map_provider);
|
|
setMapboxStyle(provider === 'leaflet' ? '' : defaultStyleForProvider(provider));
|
|
}
|
|
toast.success(t('admin.defaultSettings.reset'));
|
|
} catch (err: unknown) {
|
|
toast.error(errorMessage(err, t('common.error')));
|
|
}
|
|
});
|
|
|
|
const isSet = (key: keyof Defaults) => defaults[key] !== undefined;
|
|
|
|
// Built once per mount, like the preview it feeds.
|
|
const [mapPreviewPlaces] = useState((): Place[] => [adminPreviewPlace(previewCreatedAt())]);
|
|
|
|
const mapProvider = normalizeProvider(defaults.map_provider);
|
|
// MapLibre also offers its keywords: a default that follows the colour scheme, or the template above.
|
|
const glStylePresets = mapProvider === 'leaflet' ? [] : glStylePresetsFor(mapProvider, t);
|
|
const styleKey: keyof Defaults = mapProvider === 'maplibre-gl' ? 'maplibre_style' : 'mapbox_style';
|
|
|
|
const saveMapProvider = (nextProvider: MapProvider) => {
|
|
const patch: Partial<Defaults> = { map_provider: nextProvider };
|
|
if (nextProvider !== 'leaflet') {
|
|
// Load + save the new provider's own style slot so the other provider's style is kept.
|
|
const slot = nextProvider === 'maplibre-gl' ? defaults.maplibre_style : defaults.mapbox_style;
|
|
const nextStyle = styleForProvider(nextProvider, slot);
|
|
setMapboxStyle(nextStyle);
|
|
patch[styleSettingKey(nextProvider)] = nextStyle;
|
|
}
|
|
void save(patch);
|
|
};
|
|
|
|
/** A tile preset picked from the list: shown and saved at once. */
|
|
const pickTilePreset = (value: string) => {
|
|
if (value) {
|
|
setMapTileUrl(value);
|
|
void save({ map_tile_url: value });
|
|
}
|
|
};
|
|
|
|
/** A style preset picked from the list: shown and saved at once. */
|
|
const pickStylePreset = (value: string) => {
|
|
if (value) {
|
|
setMapboxStyle(value);
|
|
void save({ [styleKey]: value });
|
|
}
|
|
};
|
|
|
|
/** A typed style is normalized for the provider before it is saved; a MapLibre keyword stays. */
|
|
const commitStyle = () => {
|
|
// The style input never renders for leaflet; this only narrows the provider type.
|
|
if (mapProvider === 'leaflet') return;
|
|
const nextStyle = normalizeGlSelection(mapProvider, mapboxStyle);
|
|
setMapboxStyle(nextStyle);
|
|
void save({ [styleKey]: nextStyle });
|
|
};
|
|
|
|
return {
|
|
defaults,
|
|
loaded,
|
|
managed,
|
|
presets: mapPresetsFor(managed, hasAmapKey),
|
|
canMapbox: mapboxOffered(managed, operatorMapboxToken),
|
|
isSet,
|
|
save,
|
|
reset,
|
|
mapTileUrl,
|
|
setMapTileUrl,
|
|
mapboxToken,
|
|
setMapboxToken,
|
|
cartoKey,
|
|
setCartoKey,
|
|
mapboxStyle,
|
|
setMapboxStyle,
|
|
mapPreviewPlaces,
|
|
mapProvider,
|
|
glStylePresets,
|
|
styleKey,
|
|
saveMapProvider,
|
|
pickTilePreset,
|
|
pickStylePreset,
|
|
commitStyle,
|
|
};
|
|
}
|
|
|
|
/** The one place the admin's tile preview shows. */
|
|
function adminPreviewPlace(createdAt: string): Place {
|
|
return {
|
|
id: 1,
|
|
trip_id: 1,
|
|
name: 'Preview center',
|
|
description: null,
|
|
notes: null,
|
|
lat: 48.8566,
|
|
lng: 2.3522,
|
|
address: null,
|
|
category_id: null,
|
|
price: null,
|
|
currency: null,
|
|
image_url: null,
|
|
google_place_id: null,
|
|
osm_id: null,
|
|
route_geometry: null,
|
|
place_time: null,
|
|
end_time: null,
|
|
duration_minutes: null,
|
|
transport_mode: null,
|
|
website: null,
|
|
phone: null,
|
|
created_at: createdAt,
|
|
};
|
|
}
|