mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +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]);
|
||||
}
|
||||
Reference in New Issue
Block a user