mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat(map): setting to hide the Mapbox basemap points of interest (#1761)
Mapbox Standard labels shops, restaurants and sights of its own; they look like trip places but cannot be picked. A Hide Points of Interest switch next to the other Mapbox options, on the desktop and the phone, turns them off on the trip map and the preview without a rebuild. The Standard config handling moves into useStandardBasemapConfig.
This commit is contained in:
@@ -26,7 +26,8 @@ import { ReservationMapboxOverlay } from './reservationsMapbox'
|
||||
import { useTransportRoutes } from '../../hooks/useTransportRoutes'
|
||||
import { visibleRouteReservations } from '../../utils/reservationRoutes'
|
||||
import { safeHexColor } from '../../utils/safeColor'
|
||||
import { MAPBOX_DEFAULT_STYLE, styleForActiveProvider, basemapLanguage, type GlMapProvider } from './glProviders'
|
||||
import { MAPBOX_DEFAULT_STYLE, styleForActiveProvider, type GlMapProvider } from './glProviders'
|
||||
import { useStandardBasemapConfig } from './useStandardBasemapConfig'
|
||||
import { buildGlMap, initialMapStyle } from './glSupport'
|
||||
import { mapboxTileCacheRequest, tileCacheActive } from '../../native/tileCache'
|
||||
import LocationButton from './LocationButton'
|
||||
@@ -450,7 +451,6 @@ export function MapViewGL({
|
||||
const mapbox3d = useSettingsStore(s => s.settings.mapbox_3d_enabled !== false)
|
||||
const mapboxQuality = useSettingsStore(s => s.settings.mapbox_quality_mode === true)
|
||||
const showEndpointLabels = useSettingsStore(s => s.settings.map_booking_labels) === true
|
||||
const mapLang = useSettingsStore(s => s.settings.language)
|
||||
const isMapLibre = glProvider === 'maplibre-gl'
|
||||
const glStyle = styleOverride ?? styleForActiveProvider(glProvider, rawMapboxStyle, rawMaplibreStyle)
|
||||
const enableMapbox3d = !isMapLibre && mapbox3d
|
||||
@@ -1452,15 +1452,8 @@ export function MapViewGL({
|
||||
}
|
||||
}, [glProvider, glStyle, mapboxToken, enableMapbox3d, mapboxQuality]) // rebuild on provider/style changes only
|
||||
|
||||
// Pin the basemap label language to the UI language so labels don't fall back to the
|
||||
// browser/OS locale and stack multiple scripts per place (e.g. "India/भारत/India", #1299).
|
||||
// Mapbox Standard exposes this via a basemap config property; classic and MapLibre styles
|
||||
// are left as-is. Runs on load (mapReady) and whenever the UI language changes.
|
||||
useEffect(() => {
|
||||
const map = mapRef.current
|
||||
if (!map || !mapReady || isMapLibre || !isStandardFamily(glStyle)) return
|
||||
try { map.setConfigProperty('basemap', 'language', basemapLanguage(mapLang)) } catch { /* style/SDK may not support the basemap language property */ }
|
||||
}, [mapLang, mapReady, isMapLibre, glStyle])
|
||||
// The label language (#1299) and the basemap's own points of interest (#1761) on Mapbox Standard.
|
||||
useStandardBasemapConfig(mapRef, mapReady, !isMapLibre && isStandardFamily(glStyle))
|
||||
|
||||
// Photo loading, shared with the Leaflet MapView.
|
||||
const photoUrls = useMarkerThumbs(places)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { buildSettings } from '../../../tests/helpers/factories';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import type { Settings } from '../../types';
|
||||
import { setBasemapConfig, useStandardBasemapConfig } from './useStandardBasemapConfig';
|
||||
|
||||
const map = { setConfigProperty: vi.fn() };
|
||||
const mapRef = { current: map };
|
||||
|
||||
function settle(patch: Partial<Settings>) {
|
||||
act(() => {
|
||||
useSettingsStore.setState({ settings: { ...useSettingsStore.getState().settings, ...patch } });
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllStores();
|
||||
map.setConfigProperty.mockReset();
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ language: 'de' }) });
|
||||
});
|
||||
|
||||
describe('useStandardBasemapConfig', () => {
|
||||
it('FE-MAP-BASEMAPCFG-001: a ready Standard map gets the label language and keeps its points of interest', () => {
|
||||
renderHook(() => useStandardBasemapConfig(mapRef, true, true));
|
||||
|
||||
expect(map.setConfigProperty).toHaveBeenCalledWith('basemap', 'language', 'de');
|
||||
expect(map.setConfigProperty).toHaveBeenCalledWith('basemap', 'showPointOfInterestLabels', true);
|
||||
});
|
||||
|
||||
it('FE-MAP-BASEMAPCFG-002: the setting hides the points of interest and shows them again, without a rebuild (#1761)', () => {
|
||||
renderHook(() => useStandardBasemapConfig(mapRef, true, true));
|
||||
map.setConfigProperty.mockClear();
|
||||
|
||||
settle({ map_hide_basemap_pois: true });
|
||||
expect(map.setConfigProperty).toHaveBeenCalledWith('basemap', 'showPointOfInterestLabels', false);
|
||||
// The language is left alone by a change that is not about it.
|
||||
expect(map.setConfigProperty).not.toHaveBeenCalledWith('basemap', 'language', expect.anything());
|
||||
|
||||
settle({ map_hide_basemap_pois: false });
|
||||
expect(map.setConfigProperty).toHaveBeenLastCalledWith('basemap', 'showPointOfInterestLabels', true);
|
||||
});
|
||||
|
||||
it('FE-MAP-BASEMAPCFG-003: nothing is set before the map is ready or on a style without that config', () => {
|
||||
const { rerender } = renderHook(({ ready, standard }) => useStandardBasemapConfig(mapRef, ready, standard), {
|
||||
initialProps: { ready: false, standard: true },
|
||||
});
|
||||
rerender({ ready: true, standard: false });
|
||||
expect(map.setConfigProperty).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MAP-BASEMAPCFG-004: a style or SDK that refuses the property is passed over', () => {
|
||||
map.setConfigProperty.mockImplementation(() => {
|
||||
throw new Error('unknown config');
|
||||
});
|
||||
expect(() => setBasemapConfig(map, 'showPointOfInterestLabels', false)).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { useEffect, type RefObject } from 'react';
|
||||
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { basemapLanguage } from './glProviders';
|
||||
|
||||
/** The one call of a Mapbox map this needs; a MapLibre map never gets here. */
|
||||
interface BasemapConfigurable {
|
||||
setConfigProperty: (importId: string, name: string, value: unknown) => unknown;
|
||||
}
|
||||
|
||||
/** Sets one property of a Mapbox Standard basemap's config, where the style has it. */
|
||||
export function setBasemapConfig(map: BasemapConfigurable, name: string, value: unknown): void {
|
||||
try {
|
||||
map.setConfigProperty('basemap', name, value);
|
||||
} catch {
|
||||
/* a style or SDK without this basemap config property */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's settings on a Mapbox Standard basemap's own config, set without rebuilding
|
||||
* the map: once it is ready, and again whenever a setting changes.
|
||||
*
|
||||
* The label language follows the UI language, so labels don't fall back to the browser
|
||||
* or OS locale and stack several scripts per place (#1299). The basemap's own points of
|
||||
* interest can be hidden (#1761): Standard labels shops, restaurants and sights of its
|
||||
* own, which look like the trip's places but cannot be picked and sit a little off the
|
||||
* same place added from the search. Classic Mapbox styles and MapLibre have no such
|
||||
* config and are left as they are (`standard` is false for them).
|
||||
*/
|
||||
export function useStandardBasemapConfig(
|
||||
mapRef: RefObject<BasemapConfigurable | null>,
|
||||
ready: boolean,
|
||||
standard: boolean
|
||||
): void {
|
||||
const language = useSettingsStore((s) => s.settings.language);
|
||||
const hidePois = useSettingsStore((s) => s.settings.map_hide_basemap_pois === true);
|
||||
|
||||
useEffect(() => {
|
||||
const map = mapRef.current;
|
||||
if (!map || !ready || !standard) return;
|
||||
setBasemapConfig(map, 'language', basemapLanguage(language));
|
||||
}, [mapRef, ready, standard, language]);
|
||||
|
||||
useEffect(() => {
|
||||
const map = mapRef.current;
|
||||
if (!map || !ready || !standard) return;
|
||||
setBasemapConfig(map, 'showPointOfInterestLabels', !hidePois);
|
||||
}, [mapRef, ready, standard, hidePois]);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-COMP-MAP-001 to FE-COMP-MAP-035
|
||||
// FE-COMP-MAP-001 to FE-COMP-MAP-036
|
||||
import { render, screen, waitFor, within, fireEvent } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
@@ -19,8 +19,8 @@ vi.mock('../Map/MapView', () => ({
|
||||
// The GL preview boots a real mapbox/maplibre instance; the tab only cares that
|
||||
// it gets the right provider/style, so render those as data attributes.
|
||||
vi.mock('./MapboxPreview', () => ({
|
||||
default: ({ provider, style, token, enable3d, quality }: {
|
||||
provider?: string; style: string; token?: string; enable3d: boolean; quality?: boolean;
|
||||
default: ({ provider, style, token, enable3d, quality, hidePois }: {
|
||||
provider?: string; style: string; token?: string; enable3d: boolean; quality?: boolean; hidePois?: boolean;
|
||||
}) => (
|
||||
<div
|
||||
data-testid="gl-preview"
|
||||
@@ -29,6 +29,7 @@ vi.mock('./MapboxPreview', () => ({
|
||||
data-token={token}
|
||||
data-3d={String(enable3d)}
|
||||
data-quality={String(quality)}
|
||||
data-hide-pois={String(hidePois)}
|
||||
/>
|
||||
),
|
||||
}));
|
||||
@@ -467,4 +468,18 @@ describe('MapSettingsTab – CARTO key', () => {
|
||||
|
||||
expect(screen.queryByText(/API KEY REQUIRED/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-MAP-036: under Mapbox the basemap points of interest can be hidden, in the preview and on save (#1761)', async () => {
|
||||
const user = userEvent.setup();
|
||||
const updateSettings = vi.fn().mockResolvedValue(undefined);
|
||||
seedStore(useSettingsStore, { settings: buildSettings(), updateSettings });
|
||||
render(<MapSettingsTab />);
|
||||
|
||||
await user.click(screen.getByText('Mapbox GL'));
|
||||
await user.click(screen.getByRole('button', { name: 'Hide Points of Interest' }));
|
||||
expect(await screen.findByTestId('gl-preview')).toHaveAttribute('data-hide-pois', 'true');
|
||||
|
||||
await user.click(screen.getByText('Save Map'));
|
||||
expect(updateSettings).toHaveBeenCalledWith(expect.objectContaining({ map_hide_basemap_pois: true }));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -132,7 +132,8 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
const {
|
||||
managed, presets, canMapbox, saving, provider, changeProvider, mapTileUrl, setMapTileUrl, mapboxToken, setMapboxToken,
|
||||
cartoKey, setCartoKey, cartoNeedsKey, mapboxStyle, setMapboxStyle, mapbox3d, setMapbox3d,
|
||||
mapboxQuality, setMapboxQuality, previewPlaces, glPreview, webgl2Missing, save: saveMapSettings,
|
||||
mapboxQuality, setMapboxQuality, hideBasemapPois, setHideBasemapPois, previewPlaces, glPreview, webgl2Missing,
|
||||
save: saveMapSettings,
|
||||
} = useMapSettingsForm()
|
||||
// One chunk per engine — see components/Map/glLazy.tsx.
|
||||
const GlMapPreview = provider === 'maplibre-gl' ? GlMapPreviewMaplibre : GlMapPreviewMapbox
|
||||
@@ -307,6 +308,11 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
}
|
||||
control={<ToggleSwitch on={mapboxQuality} onToggle={() => setMapboxQuality(!mapboxQuality)} label={t('settings.mapHighQuality')} />}
|
||||
/>
|
||||
<SettingRow
|
||||
label={t('settings.mapHidePois')}
|
||||
hint={t('settings.mapHidePoisHint')}
|
||||
control={<ToggleSwitch on={hideBasemapPois} onToggle={() => setHideBasemapPois(!hideBasemapPois)} label={t('settings.mapHidePois')} />}
|
||||
/>
|
||||
</SettingRows>
|
||||
|
||||
<div className="rounded-[12px] border border-edge-faint bg-surface-card px-3.5 py-3">
|
||||
@@ -337,6 +343,7 @@ export default function MapSettingsTab(): React.ReactElement {
|
||||
zoom={PREVIEW_ZOOM}
|
||||
enable3d={provider === 'mapbox-gl' && mapbox3d && supports3d}
|
||||
quality={provider === 'mapbox-gl' && mapboxQuality}
|
||||
hidePois={provider === 'mapbox-gl' && hideBasemapPois}
|
||||
/>
|
||||
</Suspense>
|
||||
</ErrorBoundary>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-COMP-GLPREVIEW-001 to FE-COMP-GLPREVIEW-014
|
||||
// FE-COMP-GLPREVIEW-001 to FE-COMP-GLPREVIEW-015
|
||||
import { render, screen, act } from '../../../tests/helpers/render';
|
||||
|
||||
type ClickHandler = (e: { lngLat: { lat: number; lng: number } }) => void;
|
||||
@@ -20,6 +20,7 @@ const glMap = vi.hoisted(() => ({
|
||||
remove: vi.fn(),
|
||||
jumpTo: vi.fn(),
|
||||
setTerrain: vi.fn(),
|
||||
setConfigProperty: vi.fn(),
|
||||
}));
|
||||
|
||||
const mapboxCtor = vi.hoisted(() => vi.fn(function (_options: Record<string, unknown>) {
|
||||
@@ -216,4 +217,17 @@ describe('GlMapPreview', () => {
|
||||
expect(() => unmount()).not.toThrow();
|
||||
expect(glMap.remove).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-GLPREVIEW-015: on a Standard style the preview hides the basemap points of interest when asked (#1761)', () => {
|
||||
vi.mocked(isStandardFamily).mockReturnValue(true);
|
||||
const { unmount } = render(<GlMapPreview token="pk.test" style={MAPBOX_DEFAULT_STYLE} lat={48.8} lng={2.3} zoom={16} enable3d={false} hidePois />);
|
||||
act(() => glHandlers.load?.());
|
||||
expect(glMap.setConfigProperty).toHaveBeenCalledWith('basemap', 'showPointOfInterestLabels', false);
|
||||
unmount();
|
||||
|
||||
glMap.setConfigProperty.mockClear();
|
||||
render(<GlMapPreview token="pk.test" style={MAPBOX_DEFAULT_STYLE} lat={48.8} lng={2.3} zoom={16} enable3d={false} />);
|
||||
act(() => glHandlers.load?.());
|
||||
expect(glMap.setConfigProperty).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react'
|
||||
import { isStandardFamily, supportsCustom3d, addCustom3dBuildings, addTerrainAndSky } from '../Map/mapboxSetup'
|
||||
import { MAPBOX_DEFAULT_STYLE, normalizeStyleForProvider, type GlMapProvider } from '../Map/glProviders'
|
||||
import { buildGlMap } from '../Map/glSupport'
|
||||
import { setBasemapConfig } from '../Map/useStandardBasemapConfig'
|
||||
import { fs } from '../shared/DialogShell'
|
||||
|
||||
interface Props {
|
||||
@@ -13,6 +14,8 @@ interface Props {
|
||||
zoom: number
|
||||
enable3d: boolean
|
||||
quality?: boolean
|
||||
/** Leaves out Mapbox Standard's own points of interest, as the trip map does (#1761). */
|
||||
hidePois?: boolean
|
||||
onClick?: (latlng: { lat: number; lng: number }) => void
|
||||
/**
|
||||
* The GL engine, injected instead of imported. Both SDKs used to be pulled in
|
||||
@@ -23,7 +26,7 @@ interface Props {
|
||||
gl: any
|
||||
}
|
||||
|
||||
export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style, lat, lng, zoom, enable3d, quality = false, onClick, gl }: Props) {
|
||||
export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style, lat, lng, zoom, enable3d, quality = false, hidePois = false, onClick, gl }: Props) {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mapRef = useRef<any | null>(null)
|
||||
@@ -67,6 +70,7 @@ export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style
|
||||
if (glStyle === MAPBOX_DEFAULT_STYLE) {
|
||||
try { map.setTerrain(null) } catch { /* noop */ }
|
||||
}
|
||||
if (hidePois && !isMapLibre && isStandardFamily(glStyle)) setBasemapConfig(map, 'showPointOfInterestLabels', false)
|
||||
})
|
||||
|
||||
map.on('click', (e) => {
|
||||
@@ -77,7 +81,7 @@ export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style
|
||||
try { map.remove() } catch { /* noop */ }
|
||||
mapRef.current = null
|
||||
}
|
||||
}, [provider, token, glStyle, enableMapbox3d, quality])
|
||||
}, [provider, token, glStyle, enableMapbox3d, quality, hidePois])
|
||||
|
||||
// Recenter without rebuilding the map when lat/lng/zoom change externally
|
||||
useEffect(() => {
|
||||
|
||||
@@ -118,4 +118,14 @@ describe('useMapSettingsForm', () => {
|
||||
await act(() => result.current.save());
|
||||
expect(toast.error).toHaveBeenCalledWith('common.error');
|
||||
});
|
||||
|
||||
it('FE-COMP-MAPFORM-009: the basemap points of interest switch is read from the settings and saved back (#1761)', async () => {
|
||||
seed({ map_provider: 'mapbox-gl', map_hide_basemap_pois: true });
|
||||
const { result } = renderHook(() => useMapSettingsForm());
|
||||
expect(result.current.hideBasemapPois).toBe(true);
|
||||
|
||||
act(() => result.current.setHideBasemapPois(false));
|
||||
await act(() => result.current.save());
|
||||
expect(updateSettings).toHaveBeenCalledWith(expect.objectContaining({ map_hide_basemap_pois: false }));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,7 +28,8 @@ export interface MapSettingsFormOptions {
|
||||
/**
|
||||
* The user's map settings form behind both settings shells (the desktop tab and the
|
||||
* phone section render their own markup over this): the provider, the raster tiles
|
||||
* and CARTO key, the Mapbox token, the GL style and the 3D and quality toggles,
|
||||
* and CARTO key, the Mapbox token, the GL style, the 3D and quality toggles and the switch
|
||||
* for the basemap's own points of interest,
|
||||
* seeded from the settings store and saved back in one patch.
|
||||
*/
|
||||
export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSettingsFormOptions = {}) {
|
||||
@@ -48,6 +49,7 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
);
|
||||
const [mapbox3d, setMapbox3d] = useState<boolean>(settings.mapbox_3d_enabled !== false);
|
||||
const [mapboxQuality, setMapboxQuality] = useState<boolean>(settings.mapbox_quality_mode === true);
|
||||
const [hideBasemapPois, setHideBasemapPois] = useState<boolean>(settings.map_hide_basemap_pois === true);
|
||||
|
||||
useEffect(() => {
|
||||
const nextProvider = normalizeProvider(settings.map_provider);
|
||||
@@ -58,6 +60,7 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
setMapboxStyle(styleForProvider(nextProvider, slotStyle(nextProvider, settings)));
|
||||
setMapbox3d(settings.mapbox_3d_enabled !== false);
|
||||
setMapboxQuality(settings.mapbox_quality_mode === true);
|
||||
setHideBasemapPois(settings.map_hide_basemap_pois === true);
|
||||
}, [settings]);
|
||||
|
||||
const previewPlaces = useMemo((): Place[] => [previewPlace()], []);
|
||||
@@ -82,6 +85,7 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
...stylePatch,
|
||||
mapbox_3d_enabled: mapbox3d,
|
||||
mapbox_quality_mode: mapboxQuality,
|
||||
map_hide_basemap_pois: hideBasemapPois,
|
||||
});
|
||||
// Only mirror the normalized style into the form once it is actually persisted.
|
||||
if (!mirrorStyleBeforeSave) setMapboxStyle(glStyle);
|
||||
@@ -121,6 +125,8 @@ export function useMapSettingsForm({ mirrorStyleBeforeSave = false }: MapSetting
|
||||
setMapbox3d,
|
||||
mapboxQuality,
|
||||
setMapboxQuality,
|
||||
hideBasemapPois,
|
||||
setHideBasemapPois,
|
||||
previewPlaces,
|
||||
glPreview,
|
||||
webgl2Missing,
|
||||
|
||||
Reference in New Issue
Block a user