From 59ea8cf42dddefe0160c0ef1a0a4795777481f36 Mon Sep 17 00:00:00 2001 From: Kevin Fabre Date: Thu, 21 Sep 2023 12:15:26 +0200 Subject: [PATCH] fix(POI Maps): improve popup removals Popup can be removed if its source disappear --- .../src/components/MapPoi/Map.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/visualizations/src/components/MapPoi/Map.ts b/packages/visualizations/src/components/MapPoi/Map.ts index 76a3e844..935cd2d9 100644 --- a/packages/visualizations/src/components/MapPoi/Map.ts +++ b/packages/visualizations/src/components/MapPoi/Map.ts @@ -26,6 +26,9 @@ export default class MapPOI { /** Array of layer IDs that are not from the base style of the map */ private layerIds: string[] = []; + /** Array of source IDs used in the map */ + private sourceIds: string[] = []; + /** A navigation control for the map. */ private navigationControl = new maplibregl.NavigationControl({ showCompass: false }); @@ -82,9 +85,11 @@ export default class MapPOI { /** Event handler for popup close event. */ private onPopupClose() { this.popupFeatures.forEach(({ source, sourceLayer, id }) => { - this.queue((map) => { - map.setFeatureState({ source, sourceLayer, id }, { 'popup-feature': false }); - }); + if (this.sourceIds.includes(source)) { + this.queue((map) => { + map.setFeatureState({ source, sourceLayer, id }, { 'popup-feature': false }); + }); + } }); this.popupFeatures = []; } @@ -111,8 +116,12 @@ export default class MapPOI { // Get the popup configuration for a layer const popupConfiguration = this.popupsConfiguration[layerId]; - // If no popup configuration for a layer, we remove the popup - if (!popupConfiguration) { + /* + * We remove the popup if: + * - no popup configuration for a layer + * - popup's source is no longer used in the map + */ + if (!popupConfiguration || !this.sourceIds.includes(source)) { this.popup.remove(); this.popupFeatures = []; return; @@ -125,7 +134,6 @@ export default class MapPOI { } this.popup.setHTML(getLoadingContent()); - getContent(featureId, properties).then((content) => { this.popup.setHTML(content); }); @@ -188,6 +196,7 @@ export default class MapPOI { }); } this.layerIds = layers.map(({ id }) => id); + this.sourceIds = Object.keys(sources); }); }