Files
TREK/client/src/mobile/screens/settings/MSettingsMap.tsx
T
Maurice 86513fda0d Managed mode: hand place search, routing, plugins and docs to the operator
On a managed install the operator runs place search and routing, the plugin
runtime, the integrations and the documentation, so the admin and the users
no longer see switches and guides for them:

- The place search and routing card, the transit provider and the search log
  are gone from the admin settings (desktop and mobile). places_provider is a
  managed-locked setting, and the transit provider, search log and wanderer
  routes refuse writes there. Share place details stays the admin's, as a card
  of its own, hidden when TREK_PLACES_CONTRIBUTE switches it off.
- File types and trip reminders move to the right column, so the columns stay
  balanced.
- Plugins disappear from the admin and the user settings, wanderer from Tours.
- The Help & Docs page and its links are not offered. The help center stays and
  leaves out guides about what the operator runs (keys, OIDC, backups, storage,
  releases, plugins, document sync) and about integrations that are off.
- Mapbox GL is offered only with the operator's token, Amap tiles only where
  the operator set Amap up.
- The mobile admin no longer shows OIDC and the WebAuthn relying party fields,
  matching the desktop tab.

For every install:

- Notification matrices list only events the install can raise: no plugin row
  without the plugin runtime, no Vacay, collection, photo, Synology, wanderer or
  packing rows while those are off. On a managed install the release and
  replica alerts are left out.
- The journey entry editor offers External photos only where a photo provider
  is switched on, as the phone already did.
- app-config is asked again after a failed answer. A page opened while the
  server restarted kept every default for the session, which on a managed
  install brought back the operator's settings.
2026-11-03 08:44:45 +01:00

250 lines
11 KiB
TypeScript

import React, { useState, Suspense } from 'react'
import { Box, Check, ChevronDown, Globe2, Layers, Map, Save } from 'lucide-react'
import { useTranslation } from '../../../i18n'
import { MapView } from '../../../components/Map/MapView'
// Same as the desktop map settings tab: on demand, and paired with one engine.
import ErrorBoundary from '../../../components/shared/ErrorBoundary'
import { GlMapPreviewMapbox, GlMapPreviewMaplibre } from '../../../components/Map/glLazy'
import { defaultStyleForProvider, getStylePresets } from '../../../components/Map/glProviders'
import { PREVIEW_CENTER, PREVIEW_ZOOM, type MapProvider } from '../../../components/Settings/mapSettingsModel'
import { useMapSettingsForm } from '../../../components/Settings/useMapSettingsForm'
import { withTileApiKey } from '../../../utils/tileUrl'
import MToggle from '../../components/MToggle'
import { MSetCard, MSetEyebrow, MSetSelectRow, MSetInput, MSetButton, MSetHint, MSetRow } from './MSettingsUi'
import MSetPickerSheet from './MSetPickerSheet'
/**
* "Map" section — MapSettingsTab parity: provider, raster tiles, Mapbox token,
* GL style, 3D + quality toggles and a live style preview.
*/
export default function MSettingsMap() {
const { t } = useTranslation()
const {
managed, presets: tilePresets, canMapbox, saving, provider, changeProvider, mapTileUrl, setMapTileUrl, mapboxToken, setMapboxToken,
cartoKey, setCartoKey, cartoNeedsKey, mapboxStyle, setMapboxStyle, mapbox3d, setMapbox3d,
mapboxQuality, setMapboxQuality, previewPlaces, glPreview, webgl2Missing, save,
} = useMapSettingsForm({ mirrorStyleBeforeSave: true })
const [presetOpen, setPresetOpen] = useState(false)
const [styleOpen, setStyleOpen] = useState(false)
const providers: { id: MapProvider; name: string; sub: string; icon: typeof Layers }[] = [
{ id: 'leaflet', name: 'Leaflet', sub: t('settings.mapLeafletSubtitle'), icon: Layers },
...(canMapbox ? [{ id: 'mapbox-gl' as const, name: 'Mapbox GL', sub: t('settings.mapMapboxSubtitle'), icon: Box }] : []),
{ id: 'maplibre-gl', name: 'MapLibre GL', sub: t('settings.mapMapLibreSubtitle'), icon: Globe2 },
]
const presets = provider === 'leaflet' ? [] : getStylePresets(provider)
const selectedPreset = presets.find((p) => p.url === mapboxStyle)
const chevron = <ChevronDown size={13} strokeWidth={2} className="flex-none text-m-faint" />
// One chunk per engine — see components/Map/glLazy.tsx.
const GlMapPreview = provider === 'maplibre-gl' ? GlMapPreviewMaplibre : GlMapPreviewMapbox
return (
<MSetCard title={t('settings.map')} icon={Map}>
<MSetEyebrow className="mb-[6px]">{t('settings.mapProvider')}</MSetEyebrow>
<div className="flex flex-col gap-[6px]">
{providers.map((p) => {
const active = provider === p.id
return (
<button
key={p.id}
type="button"
onClick={() => changeProvider(p.id)}
className={`flex items-center gap-[10px] rounded-xl border px-[13px] py-[10px] text-start ${
active
? 'border-transparent bg-m-act text-m-actfg'
: 'border-[color:var(--m-rowbr)] bg-[color:var(--m-sheet)] text-m-ink'
}`}
>
<p.icon size={17} strokeWidth={2} className="flex-none" />
<span className="min-w-0 flex-1">
<span className="block text-[0.8125rem] font-bold">{p.name}</span>
<span className={`block font-geist text-[0.625rem] ${active ? 'opacity-70' : 'text-m-muted'}`}>{p.sub}</span>
</span>
{active && <Check size={15} strokeWidth={2.5} className="flex-none" />}
</button>
)
})}
</div>
<MSetHint>{t('settings.mapProviderHint')}</MSetHint>
{provider === 'leaflet' && (
<>
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.mapTemplate')}</MSetEyebrow>
<MSetSelectRow
label={tilePresets.find((p) => p.url === mapTileUrl)?.name || t('settings.mapTemplatePlaceholder.select')}
trailing={chevron}
onClick={() => setPresetOpen(true)}
/>
<MSetInput
mono
className="mt-[6px]"
value={mapTileUrl}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setMapTileUrl(e.target.value)}
placeholder="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<MSetHint>{t('settings.mapDefaultHint')}</MSetHint>
{/* A managed install brings its own key, same as the Mapbox token below. */}
{!managed && (
<>
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.mapCartoKey')}</MSetEyebrow>
<MSetInput
mono
value={cartoKey}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setCartoKey(e.target.value)}
spellCheck={false}
autoComplete="off"
/>
<MSetHint>
{t('settings.mapCartoKeyHint')}{' '}
<a href="https://carto.com/basemaps/apikey/" target="_blank" rel="noreferrer" className="underline">
{t('settings.mapCartoKeyLink')}
</a>
</MSetHint>
{cartoNeedsKey && (
<p className="mt-[6px] font-geist text-[0.625rem] leading-relaxed text-[color:var(--m-st-pending)]">
{t('settings.mapCartoKeyMissing')}
</p>
)}
</>
)}
</>
)}
{provider !== 'leaflet' && (
<>
{provider === 'mapbox-gl' && !managed && (
<>
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.mapMapboxToken')}</MSetEyebrow>
<MSetInput mono value={mapboxToken} onChange={(e) => setMapboxToken(e.target.value)} placeholder="pk.eyJ1Ijoi..." />
<MSetHint>
{t('settings.mapMapboxTokenHint')}{' '}
<a href="https://account.mapbox.com/access-tokens/" target="_blank" rel="noreferrer" className="underline">
{t('settings.mapMapboxTokenLink')}
</a>
</MSetHint>
</>
)}
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.mapStyle')}</MSetEyebrow>
<MSetSelectRow
label={
selectedPreset
? selectedPreset.name
: provider === 'maplibre-gl'
? t('settings.mapOpenFreeMapStylePlaceholder')
: t('settings.mapStylePlaceholder')
}
trailing={chevron}
onClick={() => setStyleOpen(true)}
/>
<MSetInput
mono
className="mt-[6px]"
value={mapboxStyle}
onChange={(e) => setMapboxStyle(e.target.value)}
placeholder={defaultStyleForProvider(provider)}
/>
<MSetHint>{provider === 'maplibre-gl' ? t('settings.mapOpenFreeMapStyleHint') : t('settings.mapStyleHint')}</MSetHint>
{provider === 'mapbox-gl' && (
<div className="mt-1">
<MSetRow
label={t('settings.map3dBuildings')}
sub={t('settings.map3dHint')}
trailing={<MToggle checked={mapbox3d} onChange={setMapbox3d} ariaLabel={t('settings.map3dBuildings')} />}
/>
<MSetRow
label={t('settings.mapHighQuality')}
sub={`${t('settings.mapHighQualityHint')} ${t('settings.mapHighQualityWarning')}`}
trailing={<MToggle checked={mapboxQuality} onChange={setMapboxQuality} ariaLabel={t('settings.mapHighQuality')} />}
/>
<p className="rounded-xl border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] p-3 font-geist text-[0.625rem] leading-relaxed text-m-muted">
<strong className="text-m-ink">{t('settings.mapTipLabel')}</strong> {t('settings.mapTip')}
</p>
</div>
)}
</>
)}
<div className="relative mt-3 h-[200px] w-full overflow-hidden rounded-xl">
{glPreview ? (
/* See MapSettingsTab: the preview gets its own net. */
<ErrorBoundary boundaryId="settings:map-preview" resetKeys={[provider]} fallback={<div className="h-full w-full bg-m-card" />}>
<Suspense fallback={<div className="h-full w-full bg-m-card animate-pulse" />}>
<GlMapPreview
provider={provider}
token={mapboxToken}
style={mapboxStyle}
lat={PREVIEW_CENTER[0]}
lng={PREVIEW_CENTER[1]}
zoom={PREVIEW_ZOOM}
enable3d={provider === 'mapbox-gl' && mapbox3d}
quality={provider === 'mapbox-gl' && mapboxQuality}
/>
</Suspense>
</ErrorBoundary>
) : (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
React.createElement(MapView as any, {
places: previewPlaces,
dayPlaces: [],
route: null,
routeSegments: null,
selectedPlaceId: null,
onMarkerClick: null,
onMapClick: null,
onMapContextMenu: null,
// As on the desktop tab: the field holds what is being edited, so the
// key goes back on before the preview resolves the template.
tileUrl: withTileApiKey(mapTileUrl, cartoKey),
fitKey: null,
dayOrderMap: [],
leftWidth: 0,
rightWidth: 0,
hasInspector: false,
})
)}
</div>
{webgl2Missing && <MSetHint>{t('map.webgl2.fallback')}</MSetHint>}
<MSetButton className="mt-3" onClick={save} disabled={saving}>
<Save size={14} />
{t('settings.saveMap')}
</MSetButton>
<MSetPickerSheet
open={presetOpen}
onClose={() => setPresetOpen(false)}
title={t('settings.mapTemplate')}
value={mapTileUrl}
onSelect={setMapTileUrl}
options={tilePresets.map((p) => ({ value: p.url, label: p.name }))}
/>
<MSetPickerSheet
open={styleOpen}
onClose={() => setStyleOpen(false)}
title={t('settings.mapStyle')}
value={mapboxStyle}
onSelect={setMapboxStyle}
options={presets.map((p) => ({
value: p.url,
label: (
<span className="flex flex-wrap items-center gap-1">
{p.name}
{(p.tags || []).map((tag) => (
<span key={tag} className="rounded bg-[color:var(--m-ic)] px-[5px] py-[2px] font-geist text-[0.5625rem] font-bold uppercase tracking-wide text-m-muted">
{tag}
</span>
))}
</span>
),
}))}
/>
</MSetCard>
)
}