Files
goodgo-platform/apps/web/components/khu-cong-nghiep/park-map.tsx
Ho Ngoc Hai 66eae72f62
Some checks failed
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 6s
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 40s
Deploy / Build API Image (push) Failing after 17s
Deploy / Build Web Image (push) Failing after 10s
Deploy / Build AI Services Image (push) Failing after 11s
CI / E2E Tests (push) Has been skipped
E2E Tests / Playwright E2E (push) Failing after 10s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 2s
Security Scanning / Trivy Scan — API Image (push) Failing after 47s
Security Scanning / Trivy Scan — Web Image (push) Failing after 27s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 41s
Security Scanning / Trivy Filesystem Scan (push) Failing after 34s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 2s
Deploy / Rollback Staging (push) Has been skipped
Deploy / Rollback Production (push) Has been skipped
fix(maps): marker hover no longer teleports to (0, 0)
Mapbox GL JS writes `transform: translate(Xpx, Ypx)` on the DOM
element passed to `new Marker({ element })`. Any code that does
`el.style.transform = 'scale(...)'` on that same element CLOBBERS
the translate and the marker snaps to the map origin (top-left).

Five map components were doing exactly this in their hover listeners:
- components/neighborhood/neighborhood-poi-map.tsx
- components/du-an/project-map.tsx
- components/khu-cong-nghiep/park-map.tsx
- components/charts/district-heatmap.tsx
- components/valuation/comparables-map.tsx

Fix: wrap the visible marker chrome in an inner <div> and apply the
hover scale to that wrapper. The outer element becomes a thin sizing
shell that Mapbox can keep positioning untouched. Also set
`pointer-events: none` on the inner where the wrapper already has
an interactive role so clicks still bubble to the setPopup-bound
outer element.

Verified on /listings/[id]: POI marker no longer moves on hover,
popup still opens on click with the Phase-C close button.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 17:47:05 +07:00

175 lines
6.2 KiB
TypeScript

'use client';
/* eslint-disable import-x/no-named-as-default-member */
import mapboxgl from 'mapbox-gl';
import * as React from 'react';
import 'mapbox-gl/dist/mapbox-gl.css';
import {
type IndustrialParkListItem,
PARK_STATUS_LABELS,
PARK_STATUS_COLORS,
} from '@/lib/khu-cong-nghiep-api';
import { useMapboxStyle } from '@/lib/mapbox-style';
interface ParkMapProps {
parks: IndustrialParkListItem[];
className?: string;
}
const DEFAULT_CENTER: [number, number] = [106.6297, 10.8231]; // HCMC
const DEFAULT_ZOOM = 6;
export function ParkMap({ parks, className }: ParkMapProps) {
const mapContainerRef = React.useRef<HTMLDivElement>(null);
const mapRef = React.useRef<mapboxgl.Map | null>(null);
const markersRef = React.useRef<mapboxgl.Marker[]>([]);
const mapStyle = useMapboxStyle();
const geoParks = React.useMemo(
() => parks.filter((p) => p.latitude != null && p.longitude != null),
[parks],
);
React.useEffect(() => {
if (!mapContainerRef.current) return;
const token = process.env['NEXT_PUBLIC_MAPBOX_TOKEN'];
if (!token) return;
mapboxgl.accessToken = token;
const map = new mapboxgl.Map({
container: mapContainerRef.current,
style: mapStyle,
center: DEFAULT_CENTER,
zoom: DEFAULT_ZOOM,
attributionControl: false,
});
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
map.addControl(new mapboxgl.AttributionControl({ compact: true }), 'bottom-right');
mapRef.current = map;
return () => {
map.remove();
mapRef.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
React.useEffect(() => {
const map = mapRef.current;
if (!map) return;
map.setStyle(mapStyle);
}, [mapStyle]);
React.useEffect(() => {
const map = mapRef.current;
if (!map) return;
markersRef.current.forEach((m) => m.remove());
markersRef.current = [];
if (geoParks.length === 0) return;
const bounds = new mapboxgl.LngLatBounds();
geoParks.forEach((park) => {
// Mapbox owns `transform: translate(...)` on the marker element.
// Apply hover scale to an inner wrapper so we don't clobber it.
const el = document.createElement('div');
el.className = 'park-map-marker';
const inner = document.createElement('div');
inner.style.cssText = `
background: hsl(var(--card));
color: hsl(var(--card-foreground));
border-radius: 8px;
padding: 4px 8px;
font-size: 11px;
font-weight: 600;
box-shadow: 0 2px 6px rgba(0,0,0,0.3);
white-space: nowrap;
cursor: pointer;
border-left: 3px solid hsl(var(--primary));
transition: transform 0.15s;
transform: scale(1);
max-width: 160px;
overflow: hidden;
text-overflow: ellipsis;
`;
inner.textContent = park.name;
el.appendChild(inner);
el.addEventListener('mouseenter', () => {
inner.style.transform = 'scale(1.05)';
});
el.addEventListener('mouseleave', () => {
inner.style.transform = 'scale(1)';
});
const statusLabel = PARK_STATUS_LABELS[park.status];
const statusColorClass = PARK_STATUS_COLORS[park.status];
const bgColor = statusColorClass.includes('green') ? '#dcfce7' :
statusColorClass.includes('amber') ? '#fef3c7' :
statusColorClass.includes('red') ? '#fee2e2' :
'#dbeafe';
const textColor = statusColorClass.includes('green') ? '#166534' :
statusColorClass.includes('amber') ? '#92400e' :
statusColorClass.includes('red') ? '#991b1b' :
'#1e40af';
const rentText = park.landRentUsdM2Year
? `$${park.landRentUsdM2Year}/m²/năm`
: 'Liên hệ';
const popup = new mapboxgl.Popup({ offset: 15, maxWidth: '260px', closeButton: false })
.setHTML(
`<div style="font-family:system-ui,sans-serif;background:hsl(var(--card));color:hsl(var(--card-foreground));padding:8px;border-radius:6px;">
<p style="font-weight:600;font-size:13px;margin:0 0 4px;">${park.name}</p>
<p style="font-size:12px;color:hsl(var(--muted-foreground));margin:0 0 4px;">${park.province} &middot; ${park.totalAreaHa.toLocaleString()} ha</p>
<p style="font-size:12px;margin:0 0 4px;">
<span style="background:${bgColor};color:${textColor};padding:2px 6px;border-radius:4px;">${statusLabel}</span>
<span style="margin-left:4px;font-weight:600;color:hsl(var(--primary));">${rentText}</span>
</p>
<p style="font-size:12px;color:hsl(var(--muted-foreground));margin:0 0 4px;">Lấp đầy: ${park.occupancyRate}% &middot; ${park.tenantCount} DN</p>
<a href="/khu-cong-nghiep/${park.slug}" style="font-size:12px;color:hsl(var(--primary));text-decoration:none;">Xem chi tiết →</a>
</div>`,
);
const marker = new mapboxgl.Marker({ element: el, anchor: 'left' })
.setLngLat([park.longitude, park.latitude])
.setPopup(popup)
.addTo(map);
markersRef.current.push(marker);
bounds.extend([park.longitude, park.latitude]);
});
if (geoParks.length > 1) {
map.fitBounds(bounds, { padding: 60, maxZoom: 13 });
} else {
map.flyTo({ center: [geoParks[0]!.longitude, geoParks[0]!.latitude], zoom: 14 });
}
}, [geoParks]);
const hasToken = typeof process !== 'undefined' && process.env['NEXT_PUBLIC_MAPBOX_TOKEN'];
return (
<div className={`relative overflow-hidden rounded-lg border ${className || 'h-[400px] md:h-[500px]'}`}>
<div ref={mapContainerRef} className="h-full w-full" />
{!hasToken && (
<div className="absolute inset-0 flex items-center justify-center bg-gradient-to-b from-blue-50 to-green-50">
<p className="text-sm text-muted-foreground">
Thiết lập NEXT_PUBLIC_MAPBOX_TOKEN đ hiển thị bản đ
</p>
</div>
)}
<div className="absolute bottom-3 left-3 rounded bg-white/90 px-2 py-1 text-xs text-muted-foreground shadow">
{geoParks.length} KCN trên bản đ
</div>
</div>
);
}