mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
fix(maps): let users and admins enter a CARTO API key (#2060)
CARTO stamps an "API KEY REQUIRED" watermark onto every basemap tile fetched without a key since 26.08.2026, and both CartoDB presets plus the built-in fallback are CARTO. The key is free and needs no account, so add it as a setting rather than dropping the provider. carto_api_key sits next to mapbox_access_token: per user in Settings/Map, instance-wide under Admin/User Defaults, encrypted at rest, injected from CARTO_API_KEY on managed instances. Unlike Mapbox it never switches the map provider, since a raster key says nothing about the renderer. The key is appended as ?key= at render time instead of being stored in the template, so a saved URL stays portable and survives a rotation. That is also what reaches the four maps that pinned CARTO and ignored the user's tile setting: atlas, collections, the public trip share and the public journey. The two share pages read it from their token-gated payload, the same way baseCurrency already travels. Also lifts the release notice above page chrome. Its overlay sat at z-index 50, under the Vacay mode toolbar at 61, so the dialog was neither on top nor modal. Adds a six-step layer scale to index.css and moves the three notice shells onto it. Closes #2054 Closes #2052
This commit is contained in:
@@ -1,5 +1,19 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { DEFAULT_SETTINGS, useSettingsStore } from './settingsStore'
|
||||
import { settingsApi } from '../api/client'
|
||||
import { clearTileCache } from '../sync/tilePrefetcher'
|
||||
|
||||
vi.mock('../api/client', () => ({
|
||||
settingsApi: {
|
||||
get: vi.fn(),
|
||||
set: vi.fn().mockResolvedValue(undefined),
|
||||
setBulk: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('../sync/tilePrefetcher', () => ({
|
||||
clearTileCache: vi.fn().mockResolvedValue(undefined),
|
||||
}))
|
||||
|
||||
// A fresh instance sends no value for a setting an admin hasn't defaulted, so DEFAULT_SETTINGS
|
||||
// is what a brand-new user actually sees. These guard against the two regressions in the
|
||||
@@ -18,4 +32,61 @@ describe('settings defaults', () => {
|
||||
expect(settings.distance_unit).toBe(DEFAULT_SETTINGS.distance_unit)
|
||||
expect(settings.time_format).toBe(DEFAULT_SETTINGS.time_format)
|
||||
})
|
||||
|
||||
it('SETTINGS-DEFAULTS-003: no CARTO key is shipped, so the field starts empty instead of undefined', () => {
|
||||
expect(DEFAULT_SETTINGS.carto_api_key).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
// The CARTO key lives in its own setting and is appended at render time (#2054). Two things
|
||||
// have to hold on the way out to the server: the key never gets frozen into the saved
|
||||
// template, and a key change invalidates the tile cache, which is keyed by the full URL.
|
||||
describe('settings tile template hygiene', () => {
|
||||
const initialState = useSettingsStore.getState()
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
useSettingsStore.setState(initialState, true)
|
||||
})
|
||||
|
||||
it('SETTINGS-TILE-001: a key pasted into the template is stripped before the template is stored', async () => {
|
||||
await useSettingsStore.getState().updateSetting(
|
||||
'map_tile_url',
|
||||
'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png?key=pasted-secret',
|
||||
)
|
||||
const stored = useSettingsStore.getState().settings.map_tile_url
|
||||
expect(stored).toBe('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png')
|
||||
expect(stored).not.toContain('pasted-secret')
|
||||
expect(settingsApi.set).toHaveBeenCalledWith('map_tile_url', stored)
|
||||
})
|
||||
|
||||
it('SETTINGS-TILE-002: a bulk save strips the key and normalizes the retired OSM shard host', async () => {
|
||||
await useSettingsStore.getState().updateSettings({
|
||||
map_tile_url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?key=pasted-secret',
|
||||
})
|
||||
const stored = useSettingsStore.getState().settings.map_tile_url
|
||||
expect(stored).toBe('https://tile.openstreetmap.org/{z}/{x}/{y}.png')
|
||||
expect(settingsApi.setBulk).toHaveBeenCalledWith({ map_tile_url: stored })
|
||||
})
|
||||
|
||||
it('SETTINGS-TILE-003: changing the CARTO key drops the tile cache', async () => {
|
||||
await useSettingsStore.getState().updateSetting('carto_api_key', 'fresh-key')
|
||||
expect(clearTileCache).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('SETTINGS-TILE-004: re-saving the same key leaves the cache alone', async () => {
|
||||
await useSettingsStore.getState().updateSetting('carto_api_key', 'fresh-key')
|
||||
vi.mocked(clearTileCache).mockClear()
|
||||
await useSettingsStore.getState().updateSetting('carto_api_key', 'fresh-key')
|
||||
expect(clearTileCache).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('SETTINGS-TILE-005: a bulk save clears the cache only when the key actually moves', async () => {
|
||||
await useSettingsStore.getState().updateSettings({ carto_api_key: 'fresh-key' })
|
||||
expect(clearTileCache).toHaveBeenCalledTimes(1)
|
||||
|
||||
vi.mocked(clearTileCache).mockClear()
|
||||
await useSettingsStore.getState().updateSettings({ map_tile_url: '' })
|
||||
expect(clearTileCache).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -4,7 +4,8 @@ import type { Settings } from '../types'
|
||||
import { DEFAULT_APPEARANCE } from '@trek/shared'
|
||||
import { getApiErrorMessage } from '../types'
|
||||
import { SUPPORTED_LANGUAGE_CODES } from '../i18n/supportedLanguages'
|
||||
import { normalizeTileUrl } from '../utils/tileUrl'
|
||||
import { normalizeTileUrl, stripTileApiKey } from '../utils/tileUrl'
|
||||
import { clearTileCache } from '../sync/tilePrefetcher'
|
||||
import { rememberStartDestination, DEFAULT_START_PAGE, DEFAULT_START_TRIP_TAB } from '../utils/startDestination'
|
||||
|
||||
interface SettingsState {
|
||||
@@ -43,6 +44,7 @@ export const DEFAULT_SETTINGS: Settings = {
|
||||
map_provider: 'leaflet',
|
||||
map_base_layer: 'default',
|
||||
map_poi_pill_enabled: true,
|
||||
carto_api_key: '',
|
||||
mapbox_access_token: '',
|
||||
mapbox_style: 'mapbox://styles/mapbox/standard',
|
||||
maplibre_style: '',
|
||||
@@ -68,7 +70,14 @@ let _loadInFlight: Promise<void> | null = null
|
||||
// typed by hand survive in the database until the next load put it back (#1733).
|
||||
function withNormalizedTileUrl<T extends Partial<Settings>>(patch: T): T {
|
||||
if (typeof patch.map_tile_url !== 'string') return patch
|
||||
return { ...patch, map_tile_url: normalizeTileUrl(patch.map_tile_url) }
|
||||
return { ...patch, map_tile_url: cleanTileTemplate(patch.map_tile_url) }
|
||||
}
|
||||
|
||||
// A CARTO key pasted along with a full tile URL is dropped here: the key lives
|
||||
// in its own setting and is appended at render time, so leaving it in the
|
||||
// template would freeze it into the saved value and break on the next rotation.
|
||||
function cleanTileTemplate(url: string): string {
|
||||
return stripTileApiKey(normalizeTileUrl(url))
|
||||
}
|
||||
|
||||
export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||
@@ -105,7 +114,8 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||
|
||||
updateSetting: async (key: keyof Settings, value: Settings[keyof Settings]) => {
|
||||
const next =
|
||||
key === 'map_tile_url' && typeof value === 'string' ? normalizeTileUrl(value) : value
|
||||
key === 'map_tile_url' && typeof value === 'string' ? cleanTileTemplate(value) : value
|
||||
if (key === 'carto_api_key' && next !== get().settings.carto_api_key) void clearTileCache()
|
||||
set((state) => ({
|
||||
settings: { ...state.settings, [key]: next },
|
||||
}))
|
||||
@@ -134,6 +144,11 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||
|
||||
updateSettings: async (settingsObj: Partial<Settings>) => {
|
||||
const patch = withNormalizedTileUrl(settingsObj)
|
||||
// Cached tiles are keyed by their full URL, so a new key leaves the whole
|
||||
// offline cache stranded behind the old one.
|
||||
if ('carto_api_key' in patch && patch.carto_api_key !== get().settings.carto_api_key) {
|
||||
void clearTileCache()
|
||||
}
|
||||
set((state) => ({
|
||||
settings: { ...state.settings, ...patch },
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user