From 09ff2b9b059eaf2e03a5355ac4df7ee76ffad6c8 Mon Sep 17 00:00:00 2001 From: Clinton Lunn Date: Tue, 26 Nov 2024 21:20:20 -0500 Subject: [PATCH] refactor: clear areaId if nothing under click --- src/app/(maps)/components/FullScreenMap.tsx | 6 +----- src/components/maps/GlobalMap.tsx | 2 +- src/js/hooks/useUrlParams.tsx | 14 +++++--------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/app/(maps)/components/FullScreenMap.tsx b/src/app/(maps)/components/FullScreenMap.tsx index d1f3eb32d..c2b37dba6 100644 --- a/src/app/(maps)/components/FullScreenMap.tsx +++ b/src/app/(maps)/components/FullScreenMap.tsx @@ -94,11 +94,7 @@ export const FullScreenMap: React.FC = () => { const handleMapClick = useCallback( (e: MapLayerMouseEvent) => { - const areaId = e.features?.[0]?.properties?.id - if (areaId === '') { - return - } - + const areaId = e.features?.[0]?.properties?.id ?? null const { camera } = urlParams.fromUrl() const url = urlParams.toUrl({ camera: camera ?? null, areaId }) router.replace(url, { scroll: false }) diff --git a/src/components/maps/GlobalMap.tsx b/src/components/maps/GlobalMap.tsx index 908e695df..b8e19442c 100644 --- a/src/components/maps/GlobalMap.tsx +++ b/src/components/maps/GlobalMap.tsx @@ -108,11 +108,11 @@ export const GlobalMap: React.FC = ({ const onClick = (event: MapLayerMouseEvent): void => { if (mapInstance === null) return const feature = event?.features?.[0] + handleOnClick?.(event) if (feature === undefined) { setClickInfo(null) } else { const { layer, geometry, properties } = feature - handleOnClick?.(event) setClickInfo(prev => { setActiveFeatureVisual(prev, { selected: false, hover: false }) const activeFeature = tileToFeature(layer.id, event.point, geometry, properties as TileProps, mapInstance) diff --git a/src/js/hooks/useUrlParams.tsx b/src/js/hooks/useUrlParams.tsx index 15850b601..a18437801 100644 --- a/src/js/hooks/useUrlParams.tsx +++ b/src/js/hooks/useUrlParams.tsx @@ -20,18 +20,14 @@ const useUrlParams = (): UseUrlParamsReturn => { } const baseUrl = `${pathname}?` - const cameraParam = (camera != null) ? `camera=${cameraInfoToQuery(camera)}` : '' + const cameraParam = camera != null ? `camera=${cameraInfoToQuery(camera)}` : '' const otherParams = params.toString() - if (cameraParam != null && otherParams != null) { - return `${baseUrl}${cameraParam}&${otherParams}` - } else if (cameraParam != null) { - return `${baseUrl}${cameraParam}` - } else if (otherParams != null) { - return `${baseUrl}${otherParams}` - } + const query = [cameraParam, otherParams] + .filter(param => param !== '') // Remove empty params + .join('&') // Join non-empty params with `&` - return pathname + return query !== '' ? `${baseUrl}${query}` : pathname // Return base URL if query is empty } const fromUrl = (): UrlProps => {