From 11726e2fa338abb168ceda04b9e5c1b3cdb0ac74 Mon Sep 17 00:00:00 2001 From: Kesar Sidhu Date: Fri, 24 Jul 2026 10:10:33 -0700 Subject: [PATCH] fix(results): stop polyline ref leak and dedupe leg-boundary path points MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Effect 2 no longer creates polylines when the ref is empty — effect 1 owns creation, which prevents orphaned straight-line ghosts after async road polylines overwrite the ref. extractRoadPath now skips the shared first point across both steps and legs so multi-leg fallbacks do not duplicate waypoint coords. Also drop the unused O(n²) findIndex in the pin-sync loop. Co-authored-by: Cursor --- app/ui/src/app/results/components/Map.tsx | 33 +++++++---------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/app/ui/src/app/results/components/Map.tsx b/app/ui/src/app/results/components/Map.tsx index aaefaa5d..20e7893c 100644 --- a/app/ui/src/app/results/components/Map.tsx +++ b/app/ui/src/app/results/components/Map.tsx @@ -129,11 +129,12 @@ function extractRoadPath( } const path: google.maps.LatLng[] = []; - for (const leg of route.legs ?? []) { - for (const [i, step] of (leg.steps ?? []).entries()) { - if (step.path?.length) { - path.push(...(i === 0 ? step.path : step.path.slice(1))); - } + for (const [legIndex, leg] of (route.legs ?? []).entries()) { + for (const [stepIndex, step] of (leg.steps ?? []).entries()) { + if (!step.path?.length) continue; + // Skip the shared boundary point between consecutive steps and legs. + const skipFirst = legIndex > 0 || stepIndex > 0; + path.push(...(skipFirst ? step.path.slice(1) : step.path)); } } return path; @@ -343,29 +344,13 @@ function RoutePolylinesOverlay({ } for (const route of routes) { + const poly = byVehicle[route.vehicleId]; + // Effect 1 owns polyline creation; skip until it has written a polyline. + if (!poly) continue; const committed = buildRoutePath(route, null); if (committed.length < 2) continue; const key = routeCacheKey(committed); const cached = directionsCacheRef.current.get(key); - const routeIndex = routes.findIndex( - (r) => r.vehicleId === route.vehicleId, - ); - const strokeColor = routeColorHex(routeIndex); - - let poly = byVehicle[route.vehicleId]; - if (!poly) { - // Effect 1 may have cleared refs before this runs; show a path immediately. - const path = - cached && cached.path.length >= 2 ? cached.path : committed; - poly = new google.maps.Polyline({ - map, - path, - ...routePolylineOptions(strokeColor), - }); - byVehicle[route.vehicleId] = poly; - continue; - } - if (cached && cached.path.length >= 2) { poly.setPath(cached.path); } else {