"use client"; import { useState, useEffect, useRef } from "react"; import L from "leaflet"; import "leaflet/dist/leaflet.css"; delete L.Icon.Default.prototype._getIconUrl; L.Icon.Default.mergeOptions({ iconRetinaUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png", iconUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png", shadowUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png", }); export default function PropertyMapWithMarkers({ properties = [], onPropertyClick }) { const mapRef = useRef(null); const mapInstanceRef = useRef(null); const markersRef = useRef([]); const [mapLoaded, setMapLoaded] = useState(false); useEffect(() => { if (!mapRef.current || mapInstanceRef.current) return; const map = L.map(mapRef.current).setView([33.5138, 38.9968], 7); L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { attribution: '© OpenStreetMap', maxZoom: 19, }).addTo(map); mapInstanceRef.current = map; setMapLoaded(true); return () => { if (mapInstanceRef.current) { mapInstanceRef.current.remove(); mapInstanceRef.current = null; } }; }, []); useEffect(() => { if (!mapInstanceRef.current || !mapLoaded) return; markersRef.current.forEach((marker) => marker.remove()); markersRef.current = []; properties.forEach((property) => { if (property.lat && property.lng) { const marker = L.marker([property.lat, property.lng]).addTo(mapInstanceRef.current); const popupContent = `
${property.address || property.location?.address || ""}
النوع: ${property.type}
` : ""} ${property.bedrooms > 0 ? `غرف نوم: ${property.bedrooms}
` : ""} ${property.bathrooms > 0 ? `حمامات: ${property.bathrooms}
` : ""}