From 9b4279e563451d68c790240370ed5d210cd43b95 Mon Sep 17 00:00:00 2001 From: miles-grant-ibigroup Date: Fri, 9 Feb 2024 14:52:56 -0500 Subject: [PATCH 1/2] fix: Match ferris wheel vector behavior to raster behavior --- packages/vehicle-rental-overlay/src/index.tsx | 101 ++++++++---------- 1 file changed, 42 insertions(+), 59 deletions(-) diff --git a/packages/vehicle-rental-overlay/src/index.tsx b/packages/vehicle-rental-overlay/src/index.tsx index 26a467d5d..8aafa7f6b 100644 --- a/packages/vehicle-rental-overlay/src/index.tsx +++ b/packages/vehicle-rental-overlay/src/index.tsx @@ -23,25 +23,6 @@ const getColorForStation = (v: Station) => { return "gray"; }; -const checkIfPositionInViewport = ( - bounds: mapboxgl.LngLatBounds, - lat: number, - lng: number -): boolean => { - const PADDING = 0.001; - // @ts-expect-error types appear to be wrong? version issue? - // eslint-disable-next-line no-underscore-dangle - const [sw, ne] = [bounds._sw, bounds._ne]; - if (!sw || !ne) return false; - - return ( - lat >= sw.lat - PADDING && - lat <= ne.lat + PADDING && - lng >= sw.lng - PADDING && - lng <= ne.lng + PADDING - ); -}; - type Props = { /** * A list of companies that are applicable to just this instance of the @@ -114,8 +95,7 @@ const VehicleRentalOverlay = ({ visible }: Props): JSX.Element => { const { current: map } = useMap(); - const zoom = map?.getZoom(); - const bounds = map?.getBounds(); + const [zoom, setZoom] = useState(0); const layerId = `rental-vehicles-${id}`; const [clickedVehicle, setClickedVehicle] = useState(null); @@ -143,6 +123,13 @@ const VehicleRentalOverlay = ({ setClickedVehicle(event.features?.[0].properties); }); }); + map.on("zoom", () => { + // Avoid too many re-renders by only updating state if we are a whole number value different + const newZoom = map.getZoom(); + if (Math.floor(zoom) !== Math.floor(newZoom)) { + setZoom(newZoom); + } + }); }, [map]); // Don't render if no map or no stops are defined. @@ -192,44 +179,40 @@ const VehicleRentalOverlay = ({ )} {zoom >= DETAILED_MARKER_CUTOFF && - stations - .filter(station => - checkIfPositionInViewport(bounds, station.y, station.x) - ) - .map(station => ( - { - setClickedVehicle(null); - setLocation(location); - }} - getEntityName={ - // @ts-expect-error no stop support. Avoid a breaking change - getStationName && ((s, cc) => getStationName(cc, s)) - } - entity={station} - /> - } - position={[station.y, station.x]} - > - {station.bikesAvailable !== undefined && - !station.isFloatingBike && - !station.isFloatingVehicle && - station.spacesAvailable !== undefined ? ( - - ) : ( - - )} - - ))} + stations.map(station => ( + { + setClickedVehicle(null); + setLocation(location); + }} + getEntityName={ + // @ts-expect-error no stop support. Avoid a breaking change + getStationName && ((s, cc) => getStationName(cc, s)) + } + entity={station} + /> + } + position={[station.y, station.x]} + > + {station.bikesAvailable !== undefined && + !station.isFloatingBike && + !station.isFloatingVehicle && + station.spacesAvailable !== undefined ? ( + + ) : ( + + )} + + ))} {clickedVehicle && ( Date: Thu, 15 Feb 2024 15:42:40 -0500 Subject: [PATCH 2/2] refactor: address pr feedback --- packages/vehicle-rental-overlay/src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vehicle-rental-overlay/src/index.tsx b/packages/vehicle-rental-overlay/src/index.tsx index 8aafa7f6b..8d816098c 100644 --- a/packages/vehicle-rental-overlay/src/index.tsx +++ b/packages/vehicle-rental-overlay/src/index.tsx @@ -95,7 +95,7 @@ const VehicleRentalOverlay = ({ visible }: Props): JSX.Element => { const { current: map } = useMap(); - const [zoom, setZoom] = useState(0); + const [zoom, setZoom] = useState(map?.getZoom()); const layerId = `rental-vehicles-${id}`; const [clickedVehicle, setClickedVehicle] = useState(null);