2026-05-24 19:51:29 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet';
|
2026-05-24 22:47:47 +00:00
|
|
|
import { OrderItem, Supplier } from '@/lib/types';
|
2026-05-24 20:31:35 +00:00
|
|
|
import { formatCurrency, calculateDistance, parseCoordinates } from '@/lib/utils';
|
|
|
|
|
import { Navigation, AlertTriangle } from 'lucide-react';
|
2026-05-24 19:51:29 +00:00
|
|
|
import L from 'leaflet';
|
|
|
|
|
import 'leaflet/dist/leaflet.css';
|
|
|
|
|
|
|
|
|
|
const defaultIcon = L.icon({
|
|
|
|
|
iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png',
|
|
|
|
|
iconRetinaUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png',
|
|
|
|
|
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
|
|
|
|
|
iconSize: [25, 41],
|
|
|
|
|
iconAnchor: [12, 41],
|
|
|
|
|
popupAnchor: [1, -34],
|
|
|
|
|
shadowSize: [41, 41],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const currentLocationIcon = L.divIcon({
|
|
|
|
|
className: 'custom-div-icon',
|
2026-05-24 22:47:47 +00:00
|
|
|
html: `<div style="width:20px;height:20px;background:#ea580c;border:3px solid white;border-radius:50%;box-shadow:0 2px 6px rgba(0,0,0,0.3);"></div>`,
|
2026-05-24 19:51:29 +00:00
|
|
|
iconSize: [20, 20],
|
|
|
|
|
iconAnchor: [10, 10],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
interface MapViewProps {
|
2026-05-24 22:47:47 +00:00
|
|
|
items: OrderItem[];
|
2026-05-24 19:51:29 +00:00
|
|
|
onNavigate: (lat: number, lng: number, name: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
function MapBounds({ items }: { items: OrderItem[] }) {
|
2026-05-24 19:51:29 +00:00
|
|
|
const map = useMap();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-24 22:47:47 +00:00
|
|
|
const validPoints = items
|
|
|
|
|
.map((i) => {
|
|
|
|
|
const coords = parseCoordinates(i.expand?.supplier?.coordinates);
|
2026-05-24 20:31:35 +00:00
|
|
|
return coords ? [coords.latitude, coords.longitude] as [number, number] : null;
|
|
|
|
|
})
|
|
|
|
|
.filter((point): point is [number, number] => point !== null);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
if (validPoints.length > 0) {
|
|
|
|
|
const bounds = L.latLngBounds(validPoints);
|
|
|
|
|
map.fitBounds(bounds, { padding: [50, 50], maxZoom: 15 });
|
|
|
|
|
}
|
2026-05-24 22:47:47 +00:00
|
|
|
}, [map, items]);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
export default function CollectionMap({ items, onNavigate }: MapViewProps) {
|
2026-05-24 19:51:29 +00:00
|
|
|
const [userLocation, setUserLocation] = useState<[number, number] | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if ('geolocation' in navigator) {
|
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
2026-05-24 22:47:47 +00:00
|
|
|
(pos) => setUserLocation([pos.coords.latitude, pos.coords.longitude]),
|
|
|
|
|
() => {},
|
2026-05-24 20:31:35 +00:00
|
|
|
{ enableHighAccuracy: false, timeout: 30000 }
|
2026-05-24 19:51:29 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const suppliers = useMemo(() => {
|
2026-05-24 22:47:47 +00:00
|
|
|
const map = new Map<string, { supplier: Supplier; items: OrderItem[]; coords: { latitude: number; longitude: number } }>();
|
2026-05-24 19:51:29 +00:00
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
items.forEach((item) => {
|
|
|
|
|
if (!item.expand?.supplier) return;
|
|
|
|
|
const supplier = item.expand.supplier;
|
2026-05-24 20:31:35 +00:00
|
|
|
const coords = parseCoordinates(supplier.coordinates);
|
|
|
|
|
if (!coords) return;
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
const key = supplier.id;
|
|
|
|
|
if (!map.has(key)) {
|
2026-05-24 22:47:47 +00:00
|
|
|
map.set(key, { supplier, items: [], coords });
|
2026-05-24 19:51:29 +00:00
|
|
|
}
|
2026-05-24 22:47:47 +00:00
|
|
|
map.get(key)!.items.push(item);
|
2026-05-24 19:51:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Array.from(map.values());
|
2026-05-24 22:47:47 +00:00
|
|
|
}, [items]);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
const defaultCenter: [number, number] = [-33.9249, 18.4241];
|
2026-05-24 19:51:29 +00:00
|
|
|
const center = userLocation || defaultCenter;
|
|
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
const getDistance = (coords: { latitude: number; longitude: number }) => {
|
2026-05-24 20:31:35 +00:00
|
|
|
if (!userLocation) return null;
|
2026-05-24 22:47:47 +00:00
|
|
|
return calculateDistance(userLocation[0], userLocation[1], coords.latitude, coords.longitude);
|
2026-05-24 19:51:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="h-[calc(100vh-4rem)] w-full relative">
|
2026-05-24 22:47:47 +00:00
|
|
|
<MapContainer center={center} zoom={12} className="h-full w-full" zoomControl={false}>
|
|
|
|
|
<TileLayer attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
|
|
|
|
|
<MapBounds items={items} />
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
{userLocation && (
|
|
|
|
|
<Marker position={userLocation} icon={currentLocationIcon}>
|
2026-05-24 22:47:47 +00:00
|
|
|
<Popup><div className="text-sm font-medium">Your Location</div></Popup>
|
2026-05-24 19:51:29 +00:00
|
|
|
</Marker>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
{suppliers.map(({ supplier, items: supplierItems, coords }) => {
|
|
|
|
|
const distance = getDistance(coords);
|
|
|
|
|
const hasProblem = supplierItems.some((i) => i.item_status === 'problem' || i.problem_flag);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
return (
|
2026-05-24 22:47:47 +00:00
|
|
|
<Marker key={supplier.id} position={[coords.latitude, coords.longitude]} icon={defaultIcon}>
|
2026-05-24 19:51:29 +00:00
|
|
|
<Popup>
|
|
|
|
|
<div className="min-w-[200px] max-w-[250px]">
|
|
|
|
|
<h3 className="font-bold text-sm mb-1">{supplier.suppliername}</h3>
|
2026-05-24 20:31:35 +00:00
|
|
|
<p className="text-xs text-gray-500 mb-2">{supplier.supplieraddress}</p>
|
2026-05-24 22:47:47 +00:00
|
|
|
{distance && <p className="text-xs text-orange-600 mb-2 font-medium">{distance.toFixed(1)} km away</p>}
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
<div className="space-y-1.5 mb-3">
|
2026-05-24 22:47:47 +00:00
|
|
|
{supplierItems.map((i) => (
|
|
|
|
|
<div key={i.id} className="text-xs p-1.5 bg-gray-50 rounded">
|
2026-05-24 19:51:29 +00:00
|
|
|
<div className="flex items-center gap-1">
|
2026-05-24 22:47:47 +00:00
|
|
|
{(i.item_status === 'problem' || i.problem_flag) && <AlertTriangle size={12} className="text-red-500" />}
|
|
|
|
|
<span className="font-medium truncate">{i.description}</span>
|
2026-05-24 19:51:29 +00:00
|
|
|
</div>
|
2026-05-24 22:47:47 +00:00
|
|
|
<span className="text-gray-400">Qty: {i.order_qty || i.quantity || 0}</span>
|
2026-05-24 19:51:29 +00:00
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<button
|
2026-05-24 22:47:47 +00:00
|
|
|
onClick={() => onNavigate(coords.latitude, coords.longitude, supplier.suppliername)}
|
2026-05-24 20:31:35 +00:00
|
|
|
className="w-full flex items-center justify-center gap-1.5 py-2 px-3 bg-orange-500 text-white rounded-lg text-sm font-medium active:bg-orange-600 transition-colors"
|
2026-05-24 19:51:29 +00:00
|
|
|
>
|
2026-05-24 22:47:47 +00:00
|
|
|
<Navigation size={14} /> Navigate
|
2026-05-24 19:51:29 +00:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</Popup>
|
|
|
|
|
</Marker>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</MapContainer>
|
|
|
|
|
|
|
|
|
|
<div className="absolute top-4 right-4 bg-white/90 backdrop-blur-sm rounded-xl p-3 shadow-lg z-[400]">
|
|
|
|
|
<p className="text-xs font-medium text-gray-700 mb-2">Legend</p>
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<div className="flex items-center gap-2">
|
2026-05-24 20:31:35 +00:00
|
|
|
<div className="w-3 h-3 rounded-full bg-orange-500 border-2 border-white shadow-sm" />
|
2026-05-24 19:51:29 +00:00
|
|
|
<span className="text-[10px] text-gray-600">Your Location</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<div className="w-4 h-4 relative">
|
2026-05-24 20:31:35 +00:00
|
|
|
<div className="absolute inset-0 bg-orange-500 rounded-sm rotate-45 transform origin-bottom" />
|
2026-05-24 19:51:29 +00:00
|
|
|
</div>
|
|
|
|
|
<span className="text-[10px] text-gray-600">Collection Stop</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|