From fcf1a76bf65ba5a39d69b56e27dd4a0c65aee74e Mon Sep 17 00:00:00 2001 From: Ahmedhossamdev Date: Wed, 13 Nov 2024 21:01:39 +0200 Subject: [PATCH 1/4] refactor: marker clearing logic in trip planner mode with enhanced state management --- src/components/map/MapView.svelte | 62 ++++++++++--------------------- 1 file changed, 19 insertions(+), 43 deletions(-) diff --git a/src/components/map/MapView.svelte b/src/components/map/MapView.svelte index b330414..819b2cd 100644 --- a/src/components/map/MapView.svelte +++ b/src/components/map/MapView.svelte @@ -18,10 +18,9 @@ export let selectedRoute = null; export let showRoute = false; export let showRouteMap = false; - export let stop = null; export let mapProvider = null; - let isTripPlanMoodActive = false; + let isTripPlanModeActive = false; let selectedStopID = null; let mapInstance = null; @@ -99,8 +98,8 @@ const center = mapInstance.getCenter(); const zoomLevel = mapInstance.map.getZoom(); - // Prevent fetching stops when a route is selected, we only fetch stops when we are see other stops - if (selectedRoute || showRoute) { + // Prevent fetching stops in the background when a route is selected or trip plan mode is active, we only fetch stops when we are see other stops + if (selectedRoute || showRoute || isTripPlanModeActive) { return; } await loadStopsAndAddMarkers(center.lat, center.lng, false, zoomLevel); @@ -129,15 +128,6 @@ }); allStops = [...new Map([...allStops, ...newStops].map((stop) => [stop.id, stop])).values()]; - - if (selectedRoute && !showRoute) { - allStops = []; - } else if (showRoute && selectedRoute) { - const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id)); - stopsToShow.forEach((s) => addMarker(s)); - } else { - newStops.forEach((s) => addMarker(s)); - } } function clearAllMarkers() { @@ -147,42 +137,28 @@ markers = []; } - $: if (selectedRoute && !showRoute) { - clearAllMarkers(); - allStops = []; - } - - $: if (stop && mapInstance) { - // TODO: make sure that these markers are deduped. i.e. we shouldn't - // show the same stop twice on the map - if (stop.id !== selectedStopID) { - addMarker(stop); + function updateMarkers() { + if (showRoute) { + const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id)); + stopsToShow.forEach((s) => addMarker(s)); + } else if (!selectedRoute && !isTripPlanModeActive) { + allStops.forEach((s) => addMarker(s)); } } - $: if (selectedRoute && showRoute) { - clearAllMarkers(); - const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id)); - stopsToShow.forEach((s) => addMarker(s)); - } - - // If no route is selected and not showing a route, add all markers - // This ensures markers are re-added after a route is deselected - $: if (!selectedRoute && !showRoute) { - allStops.forEach((s) => addMarker(s)); - } - - // TODO: prevent fetch stops-for-location if the trip planner mode is on - we should do this after merge. $: { - if (isTripPlanMoodActive) { + if (selectedRoute) { clearAllMarkers(); - } else { - if (!selectedRoute || !showRoute) { - allStops.forEach((s) => addMarker(s)); - } + updateMarkers(); + } else if (!isTripPlanModeActive) { + allStops.forEach((s) => addMarker(s)); } } + $: if (isTripPlanModeActive) { + clearAllMarkers(); + } + function addMarker(s) { if (!mapInstance) { console.error('Map not initialized yet'); @@ -239,10 +215,10 @@ if (browser) { const darkMode = document.documentElement.classList.contains('dark'); window.addEventListener('planTripTabClicked', () => { - isTripPlanMoodActive = true; + isTripPlanModeActive = true; }); window.addEventListener('tabSwitched', () => { - isTripPlanMoodActive = false; + isTripPlanModeActive = false; }); const event = new CustomEvent('themeChange', { detail: { darkMode } }); window.dispatchEvent(event); From 995a8d474239ec22f9b1449df2ca74d6aaaeaa3e Mon Sep 17 00:00:00 2001 From: Ahmedhossamdev Date: Wed, 13 Nov 2024 21:01:49 +0200 Subject: [PATCH 2/4] fix: remove unused stop prop --- src/routes/+page.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 8ad76f7..dd6d63c 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -226,7 +226,6 @@ on:stopSelected={stopSelected} {showRoute} {showRouteMap} - {stop} bind:mapProvider /> {/if} From 931920c4b9516ddceacd9bbd7ad792a6e39b07cb Mon Sep 17 00:00:00 2001 From: Ahmedhossamdev Date: Wed, 13 Nov 2024 21:11:08 +0200 Subject: [PATCH 3/4] fix: remove unused selectedStopID variable --- src/components/map/MapView.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/map/MapView.svelte b/src/components/map/MapView.svelte index 819b2cd..bc83703 100644 --- a/src/components/map/MapView.svelte +++ b/src/components/map/MapView.svelte @@ -22,7 +22,6 @@ let isTripPlanModeActive = false; - let selectedStopID = null; let mapInstance = null; let mapElement; let markers = []; From 17427450d04fd4a12d7f57f8c215de26e1bf25c3 Mon Sep 17 00:00:00 2001 From: Ahmedhossamdev Date: Wed, 13 Nov 2024 21:15:06 +0200 Subject: [PATCH 4/4] fix: remove assignment of unused selectedStopID --- src/components/map/MapView.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/map/MapView.svelte b/src/components/map/MapView.svelte index bc83703..cce510d 100644 --- a/src/components/map/MapView.svelte +++ b/src/components/map/MapView.svelte @@ -188,7 +188,6 @@ icon: icon, stop: s, onClick: () => { - selectedStopID = s.id; dispatch('stopSelected', { stop: s }); } });