From 86b116dd97118187bc69d73c0c0626be2a22bd8b Mon Sep 17 00:00:00 2001 From: BunyodL Date: Thu, 22 May 2025 10:53:16 +0500 Subject: [PATCH] update: center the marker on station select --- src/features/map/ui/yandex-map.tsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/features/map/ui/yandex-map.tsx b/src/features/map/ui/yandex-map.tsx index e7fbbfb..f375b6e 100644 --- a/src/features/map/ui/yandex-map.tsx +++ b/src/features/map/ui/yandex-map.tsx @@ -5,6 +5,7 @@ import { YMaps, } from '@pbe/react-yandex-maps'; import { isEmpty } from 'lodash'; +import { useEffect, useRef } from 'react'; import { Point } from '../model'; @@ -21,6 +22,29 @@ export const YandexMap = ({ selectedStation, handleMapStationClick, }: YandexMapProps) => { + const mapRef = useRef(null); + + useEffect(() => { + if (!mapRef.current) return; + + if (selectedStation !== null) { + const selectedPoint = points.find( + (point) => point.id === selectedStation, + ); + if (selectedPoint) { + mapRef.current + .setCenter(selectedPoint.coordinates, mapRef.current.getZoom(), { + duration: 1000, + }) + .then(() => { + mapRef.current.setZoom(13, { duration: 300 }); + }); + } + } else { + mapRef.current.setZoom(11, { duration: 300 }); + } + }, [selectedStation, points]); + return ( { + mapRef.current = ref; + }} > {points.map((point) => { const isSelectedStation = selectedStation === point.id;