fixed desktop version
This commit is contained in:
@@ -11,13 +11,13 @@ const MARKER_COLORS = {
|
||||
FOUND: "#34c759",
|
||||
};
|
||||
|
||||
function createMarkerLayout(color, imageUrl) {
|
||||
function createMarkerLayout(color, imageUrl, pinId) {
|
||||
const bgStyle = imageUrl
|
||||
? `background-image:url('${escapeCssUrl(imageUrl)}');background-size:cover;background-position:center;`
|
||||
: "";
|
||||
|
||||
return window.ymaps.templateLayoutFactory.createClass(
|
||||
`<div style="
|
||||
`<div class="f-map-pin" data-pin-id="${escapeHtml(pinId ?? "")}" style="
|
||||
width:40px;height:40px;
|
||||
border-radius:50%;
|
||||
border:3px solid ${color};
|
||||
@@ -86,7 +86,7 @@ function createPostPlacemark(m) {
|
||||
const color = MARKER_COLORS[m.type] || MARKER_COLORS.LOST;
|
||||
const typeLabel = m.type === "FOUND" ? "Находка" : "Потеря";
|
||||
const imgSrc = m.imageUrl ? getImageUrl(m.imageUrl) : null;
|
||||
const iconLayout = createMarkerLayout(color, imgSrc);
|
||||
const iconLayout = createMarkerLayout(color, imgSrc, m.id != null ? String(m.id) : "");
|
||||
|
||||
return new window.ymaps.Placemark(
|
||||
[m.latitude, m.longitude],
|
||||
@@ -111,6 +111,15 @@ function createPostPlacemark(m) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{
|
||||
* focusPostId?: any, focusLat?: number, focusLng?: number,
|
||||
* type?: string | null, category?: string | null,
|
||||
* onPostClick?: (id: any) => void, onBack?: () => void,
|
||||
* onMarkersChange?: (markers: any[]) => void, onMarkerHover?: (id: string | null) => void,
|
||||
* highlightId?: string | null, focusTarget?: { lat: number, lng: number, zoom?: number } | null,
|
||||
* }} props
|
||||
*/
|
||||
export default function MapView({
|
||||
focusPostId,
|
||||
focusLat,
|
||||
@@ -119,6 +128,14 @@ export default function MapView({
|
||||
category = null,
|
||||
onPostClick,
|
||||
onBack,
|
||||
// Необязательные пропсы десктопной карты (на мобильном не передаются):
|
||||
// onMarkersChange — список меток в видимой области после каждой загрузки,
|
||||
// onMarkerHover — наведение на метку (id | null), highlightId — подсветка
|
||||
// метки, focusTarget — {lat, lng, zoom?} плавный перелёт к точке.
|
||||
onMarkersChange,
|
||||
onMarkerHover,
|
||||
highlightId = null,
|
||||
focusTarget = null,
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
// tRef нужен, чтобы читать перевод из эффектов, не добавляя t в их deps:
|
||||
@@ -137,6 +154,11 @@ export default function MapView({
|
||||
|
||||
const onPostClickRef = useRef(onPostClick);
|
||||
onPostClickRef.current = onPostClick;
|
||||
const onMarkersChangeRef = useRef(onMarkersChange);
|
||||
onMarkersChangeRef.current = onMarkersChange;
|
||||
const onMarkerHoverRef = useRef(onMarkerHover);
|
||||
onMarkerHoverRef.current = onMarkerHover;
|
||||
const placemarksRef = useRef(new Map());
|
||||
|
||||
// Фильтры читаются через ref: их смена не должна пересоздавать карту,
|
||||
// они просто уезжают вместе с границами в том же самом запросе.
|
||||
@@ -161,6 +183,7 @@ export default function MapView({
|
||||
if (!collection) return placemarksById;
|
||||
|
||||
collection.removeAll();
|
||||
placemarksRef.current = new Map();
|
||||
|
||||
markers.forEach((m) => {
|
||||
if (m.latitude == null || m.longitude == null) return;
|
||||
@@ -171,14 +194,18 @@ export default function MapView({
|
||||
if (m.id != null) {
|
||||
const postId = String(m.id);
|
||||
placemarksById.set(postId, [m.latitude, m.longitude]);
|
||||
placemarksRef.current.set(postId, placemark);
|
||||
placemark.events.add("click", () => {
|
||||
if (onPostClickRef.current) {
|
||||
onPostClickRef.current(postId);
|
||||
}
|
||||
});
|
||||
placemark.events.add("mouseenter", () => onMarkerHoverRef.current?.(postId));
|
||||
placemark.events.add("mouseleave", () => onMarkerHoverRef.current?.(null));
|
||||
}
|
||||
});
|
||||
|
||||
onMarkersChangeRef.current?.(markers.filter((m) => m.latitude != null && m.longitude != null));
|
||||
return placemarksById;
|
||||
};
|
||||
renderMarkersRef.current = renderMarkers;
|
||||
@@ -362,8 +389,11 @@ export default function MapView({
|
||||
|
||||
// Смена фильтров type/category: догружаем маркеры для текущих границ,
|
||||
// карту при этом не пересоздаём.
|
||||
const prevFiltersRef = useRef({ type, category });
|
||||
useEffect(() => {
|
||||
if (type == null && category == null) return;
|
||||
const prev = prevFiltersRef.current;
|
||||
prevFiltersRef.current = { type, category };
|
||||
if (prev.type === type && prev.category === category) return;
|
||||
const map = ymapRef.current;
|
||||
if (!map) return;
|
||||
const bounds = map.getBounds();
|
||||
@@ -380,6 +410,31 @@ export default function MapView({
|
||||
return () => clearTimeout(timer);
|
||||
}, [type, category]);
|
||||
|
||||
// Подсветка метки (наведение на карточку в списке десктопной карты):
|
||||
// класс на DOM-узле лейаута + zIndex, чтобы метка была поверх соседних.
|
||||
useEffect(() => {
|
||||
if (highlightId == null) return undefined;
|
||||
const id = String(highlightId);
|
||||
const placemark = placemarksRef.current.get(id);
|
||||
placemark?.options.set("zIndex", 1000);
|
||||
const node = mapRef.current?.querySelector(`[data-pin-id="${CSS.escape(id)}"]`);
|
||||
node?.classList.add("is-hl");
|
||||
return () => {
|
||||
placemark?.options.unset("zIndex");
|
||||
node?.classList.remove("is-hl");
|
||||
};
|
||||
}, [highlightId]);
|
||||
|
||||
// Программный перелёт к точке (клик по карточке в списке десктопной карты).
|
||||
useEffect(() => {
|
||||
if (!focusTarget) return;
|
||||
const map = ymapRef.current;
|
||||
if (!map) return;
|
||||
const { lat, lng, zoom } = focusTarget;
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return;
|
||||
map.setCenter([lat, lng], Math.max(map.getZoom(), zoom ?? FOCUS_ZOOM - 1), { duration: 300 });
|
||||
}, [focusTarget]);
|
||||
|
||||
// Геолокация: если юзер разрешил и карта уже создана — центр на его месте.
|
||||
// Не трогаем, если карта была открыта с фокусом на конкретном объявлении.
|
||||
const hasFocus = focusPostId != null || (Number.isFinite(focusLat) && Number.isFinite(focusLng));
|
||||
|
||||
Reference in New Issue
Block a user