import React, { useState, useEffect, useRef, useId, Suspense, type ReactNode } from 'react' import { Map, Save, Layers, Box, ChevronDown, Check, Globe2, type LucideIcon } from 'lucide-react' import { useTranslation } from '../../i18n' import CustomSelect from '../shared/CustomSelect' import { fs } from '../shared/DialogShell' import { EditorField, INPUT } from '../shared/dialogParts' import { POPOVER } from '../Packing/packingPopoverStyles' import { MapView } from '../Map/MapView' // The preview loads on demand, and paired with a single engine — a Leaflet-only // install pays for neither, and a GL install pays for one instead of both. import ErrorBoundary from '../shared/ErrorBoundary' import { GlMapPreviewMapbox, GlMapPreviewMaplibre } from '../Map/glLazy' import Section from './Section' import ToggleSwitch from './ToggleSwitch' import { SETTINGS_BUTTON_PRIMARY, SettingRow, SettingRows, SettingsHint, StatusPill } from './settingsKit' import { withTileApiKey } from '../../utils/tileUrl' import { defaultStyleForProvider, getStylePresets, type GlMapProvider } from '../Map/glProviders' import { MAP_PRESETS, PREVIEW_CENTER, PREVIEW_ZOOM } from './mapSettingsModel' import { useMapSettingsForm } from './useMapSettingsForm' /** A field holding a URL, token or key: the box look in Geist, so the characters read apart. */ const CODE_INPUT = `${INPUT} font-geist` /** A style's tag (3D, Satellite, Terrain…): a small quiet chip, so a scan of the list finds the kind of map. */ function TagChip({ tag }: { tag: string }) { return ( {tag} ) } function StyleDropdown({ value, provider, onChange }: { value: string; provider: GlMapProvider; onChange: (v: string) => void }) { const { t } = useTranslation() const [open, setOpen] = useState(false) const ref = useRef(null) const presets = getStylePresets(provider) useEffect(() => { if (!open) return const onDoc = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false) } document.addEventListener('mousedown', onDoc) return () => document.removeEventListener('mousedown', onDoc) }, [open]) const selected = presets.find(p => p.url === value) const placeholder = provider === 'maplibre-gl' ? t('settings.mapOpenFreeMapStylePlaceholder') : t('settings.mapStylePlaceholder') return (
{open && (
{presets.map(preset => { const isActive = preset.url === value return ( ) })}
)}
) } /** One engine to pick: an icon tile, its name and what it is, outlined while chosen. */ function ProviderTile({ active, onClick, icon: Icon, name, subtitle, badge }: { active: boolean onClick: () => void icon: LucideIcon name: ReactNode subtitle: string badge?: ReactNode }) { return ( ) } export default function MapSettingsTab(): React.ReactElement { const { t } = useTranslation() const { managed, saving, provider, changeProvider, mapTileUrl, setMapTileUrl, mapboxToken, setMapboxToken, cartoKey, setCartoKey, cartoNeedsKey, mapboxStyle, setMapboxStyle, mapbox3d, setMapbox3d, mapboxQuality, setMapboxQuality, previewPlaces, glPreview, webgl2Missing, save: saveMapSettings, } = useMapSettingsForm() // One chunk per engine — see components/Map/glLazy.tsx. const GlMapPreview = provider === 'maplibre-gl' ? GlMapPreviewMaplibre : GlMapPreviewMapbox // Ties each eyebrow label to its field, so a click on the label focuses it. const fieldId = useId() // 3D is available on every style now — pure satellite uses the // mapbox-streets-v8 tileset as a fallback building source. const supports3d = true const link = 'font-medium text-content-secondary underline decoration-edge underline-offset-2 hover:text-content' return (
{/* Provider picker — big tiles so the choice is obvious */}
changeProvider('leaflet')} icon={Layers} name="Leaflet" subtitle={t('settings.mapLeafletSubtitle')} /> changeProvider('mapbox-gl')} icon={Box} name={<>MapboxMapbox GL} subtitle={t('settings.mapMapboxSubtitle')} // Only on ≥sm; on a narrow window there's no room next to the title. badge={{t('settings.mapExperimental')}} /> changeProvider('maplibre-gl')} icon={Globe2} name={<>MapLibreMapLibre GL} subtitle={t('settings.mapMapLibreSubtitle')} />
{/* Leaflet settings */} {provider === 'leaflet' && (
{ if (value) setMapTileUrl(value) }} placeholder={t('settings.mapTemplatePlaceholder.select')} options={MAP_PRESETS.map(p => ({ value: p.url, label: p.name }))} /> ) => setMapTileUrl(e.target.value)} placeholder="https://tile.openstreetmap.org/{z}/{x}/{y}.png" className={CODE_INPUT} />
)} {/* Same deal as the Mapbox token: a managed install brings its own key. */} {provider === 'leaflet' && !managed && ( {t('settings.mapCartoKeyHint')}{' '} {t('settings.mapCartoKeyLink')} {cartoNeedsKey && ( {t('settings.mapCartoKeyMissing')} )} } > ) => setCartoKey(e.target.value)} spellCheck={false} autoComplete="off" className={CODE_INPUT} /> )} {/* GL settings */} {provider !== 'leaflet' && ( <> {/* The token comes with the instance on a managed install, injected when the settings are read. A field here would only let somebody save a worse one. */} {provider === 'mapbox-gl' && !managed && ( {t('settings.mapMapboxTokenHint')}{' '} {t('settings.mapMapboxTokenLink')} } > setMapboxToken(e.target.value)} placeholder="pk.eyJ1Ijoi..." className={CODE_INPUT} /> )}
setMapboxStyle(e.target.value)} placeholder={defaultStyleForProvider(provider)} className={CODE_INPUT} />
{provider === 'mapbox-gl' && ( <> { if (supports3d) setMapbox3d(!mapbox3d) }} label={t('settings.map3dBuildings')} /> } /> {t('settings.mapHighQuality')} {t('settings.mapExperimental')} } hint={ <> {t('settings.mapHighQualityHint')}{' '} {t('settings.mapHighQualityWarning')} } control={ setMapboxQuality(!mapboxQuality)} label={t('settings.mapHighQuality')} />} />
{t('settings.mapTipLabel')} {t('settings.mapTip')}
)} )}
{glPreview ? ( /* A net of its own: the preview is the one place a user flips providers live, so it is the likeliest chunk to fail — and a broken preview must not take the rest of the settings tab with it. */ }> }> ) : ( // 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, // With the key on it, or the preview resolves the template as a // keyless CARTO one and quietly shows the app default instead of // the basemap being configured. The fields hold what the user is // editing rather than what useTileUrl already resolved, so the key // has to be put back on here. tileUrl: withTileApiKey(mapTileUrl, cartoKey), fitKey: null, dayOrderMap: [], leftWidth: 0, rightWidth: 0, hasInspector: false, }) )}
{webgl2Missing && {t('map.webgl2.fallback')}}
) }