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 = // One chunk per engine — see components/Map/glLazy.tsx. const GlMapPreview = provider === 'maplibre-gl' ? GlMapPreviewMaplibre : GlMapPreviewMapbox return ( {t('settings.mapProvider')}
{providers.map((p) => { const active = provider === p.id return ( ) })}
{t('settings.mapProviderHint')} {provider === 'leaflet' && ( <> {t('settings.mapTemplate')} p.url === mapTileUrl)?.name || t('settings.mapTemplatePlaceholder.select')} trailing={chevron} onClick={() => setPresetOpen(true)} /> ) => setMapTileUrl(e.target.value)} placeholder="https://tile.openstreetmap.org/{z}/{x}/{y}.png" /> {t('settings.mapDefaultHint')} {/* A managed install brings its own key, same as the Mapbox token below. */} {!managed && ( <> {t('settings.mapCartoKey')} ) => setCartoKey(e.target.value)} spellCheck={false} autoComplete="off" /> {t('settings.mapCartoKeyHint')}{' '} {t('settings.mapCartoKeyLink')} {cartoNeedsKey && (

{t('settings.mapCartoKeyMissing')}

)} )} )} {provider !== 'leaflet' && ( <> {provider === 'mapbox-gl' && !managed && ( <> {t('settings.mapMapboxToken')} setMapboxToken(e.target.value)} placeholder="pk.eyJ1Ijoi..." /> {t('settings.mapMapboxTokenHint')}{' '} {t('settings.mapMapboxTokenLink')} )} {t('settings.mapStyle')} setStyleOpen(true)} /> setMapboxStyle(e.target.value)} placeholder={defaultStyleForProvider(provider)} /> {provider === 'maplibre-gl' ? t('settings.mapOpenFreeMapStyleHint') : t('settings.mapStyleHint')} {provider === 'mapbox-gl' && (
} /> } />

{t('settings.mapTipLabel')} {t('settings.mapTip')}

)} )}
{glPreview ? ( /* See MapSettingsTab: the preview gets its own net. */ }> }> ) : ( // 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, }) )}
{webgl2Missing && {t('map.webgl2.fallback')}} {t('settings.saveMap')} setPresetOpen(false)} title={t('settings.mapTemplate')} value={mapTileUrl} onSelect={setMapTileUrl} options={tilePresets.map((p) => ({ value: p.url, label: p.name }))} /> setStyleOpen(false)} title={t('settings.mapStyle')} value={mapboxStyle} onSelect={setMapboxStyle} options={presets.map((p) => ({ value: p.url, label: ( {p.name} {(p.tags || []).map((tag) => ( {tag} ))} ), }))} />
) }