diff --git a/components/territory/territory-workspace.tsx b/components/territory/territory-workspace.tsx index 6d2ff49..25a12ba 100644 --- a/components/territory/territory-workspace.tsx +++ b/components/territory/territory-workspace.tsx @@ -570,14 +570,42 @@ export function TerritoryWorkspace({ orgSlug, initialDashboard, territoryConfig const mobileViewAutoSwitchedRef = useRef(initialPreferListView); const pins = data.pins; - const selectedPin = useMemo(() => pins.find((pin) => pin.id === selectedId) ?? null, [pins, selectedId]); - const routeStops = useMemo( - () => - routePlanningEnabled - ? routeStopIds.map((id) => pins.find((pin) => pin.id === id)).filter((pin): pin is TerritoryAccountPin => Boolean(pin)) - : [], - [pins, routePlanningEnabled, routeStopIds], - ); + + // ⚡ Bolt: Performance optimization + // Pre-compute an O(1) Map dictionary to prevent O(N^2) lookups + // during route planning and selection interactions. + const pinsById = useMemo(() => { + const map = new Map(); + for (const pin of pins) { + map.set(pin.id, pin); + } + return map; + }, [pins]); + + const selectedPin = useMemo(() => { + if (!selectedId) { + return null; + } + const pin = pinsById.get(selectedId); + if (pin) { + return pin; + } + return null; + }, [pinsById, selectedId]); + + const routeStops = useMemo(() => { + if (!routePlanningEnabled) { + return []; + } + const stops: TerritoryAccountPin[] = []; + for (const id of routeStopIds) { + const pin = pinsById.get(id); + if (pin) { + stops.push(pin); + } + } + return stops; + }, [pinsById, routePlanningEnabled, routeStopIds]); const mappablePinCount = useMemo(() => pins.filter(hasUsableCoordinates).length, [pins]); const activeFilterCount = Object.values(filters).filter(Boolean).length; const useLiteMarkers = isNarrowViewport || mappablePinCount > 1200; diff --git a/package-lock.json b/package-lock.json index 6115437..cfe04eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4513,7 +4513,6 @@ "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, "hasInstallScript": true, "license": "MIT", "optional": true,