204 lines
8.0 KiB
TypeScript
204 lines
8.0 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { MapContainer, TileLayer, Marker, Popup, useMap, useMapEvents } from 'react-leaflet';
|
|
import _MarkerClusterGroup from 'react-leaflet-cluster';
|
|
const MarkerClusterGroup = (_MarkerClusterGroup as any).default || _MarkerClusterGroup;
|
|
import L from 'leaflet';
|
|
import 'leaflet/dist/leaflet.css';
|
|
import { useTourStore } from './useTourStore.js';
|
|
import { X, Navigation, Image as ImageIcon, LogOut, Settings, Edit2, Trash2 } from 'lucide-react';
|
|
import { UserManagementModal } from './UserManagementModal.js';
|
|
import { CreateTourModal } from './CreateTourModal.js';
|
|
|
|
// Fix lỗi icon mặc định của Leaflet
|
|
const DefaultIcon = L.icon({
|
|
iconUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png',
|
|
shadowUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png',
|
|
iconSize: [25, 41],
|
|
iconAnchor: [12, 41],
|
|
});
|
|
L.Marker.prototype.options.icon = DefaultIcon;
|
|
|
|
// Component Helper để cập nhật tâm bản đồ khi vị trí người dùng thay đổi
|
|
function RecenterMap({ position }: { position: [number, number] }) {
|
|
const map = useMap();
|
|
useEffect(() => {
|
|
map.setView(position, map.getZoom());
|
|
}, [position, map]);
|
|
return null;
|
|
}
|
|
|
|
// Component Helper để theo dõi sự di chuyển của người dùng trên bản đồ
|
|
function MapTracker() {
|
|
const setMapCenter = useTourStore(state => state.setMapCenter);
|
|
useMapEvents({
|
|
moveend: (e) => {
|
|
const map = e.target;
|
|
const center = map.getCenter();
|
|
const zoom = map.getZoom();
|
|
const coords: [number, number] = [center.lat, center.lng];
|
|
|
|
setMapCenter(coords);
|
|
// Lưu vị trí và mức zoom vào localStorage để sử dụng cho lần sau
|
|
localStorage.setItem('map_view_state', JSON.stringify({ center: coords, zoom }));
|
|
},
|
|
});
|
|
return null;
|
|
}
|
|
|
|
export const ExploreMap = ({ onBack, onLogout, user, onViewTour }: { onBack: () => void, onLogout?: () => void, user?: any, onViewTour: (id: string) => void }) => {
|
|
// Thêm fetchTour vào destructuring từ store
|
|
const { publicTours, fetchPublicTours, fetchTour, setMapCenter } = useTourStore();
|
|
|
|
// Khởi tạo vị trí từ localStorage nếu có, nếu không dùng mặc định (TP.HCM)
|
|
const [initialViewState] = useState(() => {
|
|
const saved = localStorage.getItem('map_view_state');
|
|
if (saved) {
|
|
try { return JSON.parse(saved); } catch (e) { return null; }
|
|
}
|
|
return null;
|
|
});
|
|
|
|
const [userPos, setUserPos] = useState<[number, number]>(initialViewState?.center || [10.7769, 106.7009]);
|
|
const [mapZoom] = useState(initialViewState?.zoom || 13);
|
|
const [isAdminModalOpen, setIsAdminModalOpen] = useState(false);
|
|
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
|
|
|
|
useEffect(() => {
|
|
fetchPublicTours();
|
|
|
|
// Nếu không có vị trí lưu từ trước, mới yêu cầu lấy vị trí hiện tại của thiết bị
|
|
if (!initialViewState) {
|
|
navigator.geolocation.getCurrentPosition(
|
|
(pos) => {
|
|
const posArray: [number, number] = [pos.coords.latitude, pos.coords.longitude];
|
|
setUserPos(posArray);
|
|
setMapCenter(posArray);
|
|
},
|
|
() => console.log("Không thể lấy vị trí người dùng")
|
|
);
|
|
} else {
|
|
// Cập nhật store để đồng bộ với vị trí khởi tạo từ cache
|
|
setMapCenter(initialViewState.center);
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<div className="h-screen w-full relative">
|
|
{/* Nút quay lại */}
|
|
<button
|
|
onClick={onBack}
|
|
className="absolute top-6 left-6 z-[1000] bg-white p-3 rounded-full shadow-xl hover:bg-gray-50 transition-all"
|
|
>
|
|
<X className="w-6 h-6 text-gray-800" />
|
|
</button>
|
|
|
|
{/* Nút đăng xuất - Chỉ hiển thị khi có user login */}
|
|
{onLogout && (
|
|
<button
|
|
onClick={onLogout}
|
|
className="absolute top-6 right-6 z-[1000] bg-white px-4 py-3 rounded-2xl shadow-xl hover:bg-red-50 hover:text-red-600 transition-all flex items-center gap-2 font-bold text-gray-700"
|
|
>
|
|
<LogOut className="w-5 h-5" />
|
|
<span className="hidden sm:inline">Đăng xuất</span>
|
|
</button>
|
|
)}
|
|
|
|
{/* Nút quản lý người dùng cho Admin */}
|
|
{user?.isAdmin && (
|
|
<button
|
|
onClick={() => setIsAdminModalOpen(true)}
|
|
className="absolute top-6 right-40 z-[1000] bg-blue-600 px-4 py-3 rounded-2xl shadow-xl hover:bg-blue-700 text-white transition-all flex items-center gap-2 font-bold"
|
|
>
|
|
<Settings className="w-5 h-5" />
|
|
<span className="hidden sm:inline">Quản lý hệ thống</span>
|
|
</button>
|
|
)}
|
|
|
|
{/* Nút tạo Tour mới */}
|
|
{user && (
|
|
<button
|
|
onClick={() => setIsCreateModalOpen(true)}
|
|
className="absolute top-6 right-80 z-[1000] bg-green-600 px-4 py-3 rounded-2xl shadow-xl hover:bg-green-700 text-white transition-all flex items-center gap-2 font-bold"
|
|
>
|
|
<Navigation className="w-5 h-5" />
|
|
<span className="hidden sm:inline">Tạo Tour mới</span>
|
|
</button>
|
|
)}
|
|
|
|
{/* Header Overlay */}
|
|
<div className="absolute top-6 left-20 z-[1000] bg-white/90 backdrop-blur-md px-6 py-3 rounded-2xl shadow-xl border border-white/20 hidden sm:block">
|
|
<div className="flex items-center gap-2">
|
|
<Navigation className="w-4 h-4 text-blue-600" />
|
|
<span className="font-bold text-gray-800">Đang khám phá khu vực của bạn</span>
|
|
</div>
|
|
</div>
|
|
|
|
<MapContainer
|
|
center={userPos}
|
|
zoom={mapZoom}
|
|
className="h-full w-full"
|
|
preferCanvas={true}
|
|
>
|
|
<TileLayer
|
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
attribution='© OpenStreetMap contributors'
|
|
/>
|
|
|
|
{/* Theo dõi di chuyển bản đồ */}
|
|
<MapTracker />
|
|
|
|
{/* Tự động di chuyển bản đồ đến vị trí người dùng khi tìm thấy tọa độ */}
|
|
<RecenterMap position={userPos} />
|
|
|
|
<MarkerClusterGroup chunkedLoading>
|
|
{publicTours.map((tour) => {
|
|
const startLoc = tour.legs?.[0]?.locations?.[0];
|
|
const tourImage = tour.photos?.[0]?.imageUrl || `https://picsum.photos/seed/${tour.id}/200/200`;
|
|
const markerPos = startLoc
|
|
? [startLoc.latitude, startLoc.longitude] as [number, number]
|
|
: userPos;
|
|
|
|
return (
|
|
<React.Fragment key={tour.id}>
|
|
<Marker
|
|
position={markerPos}
|
|
eventHandlers={{
|
|
click: () => onViewTour(tour.id)
|
|
}}
|
|
icon={L.divIcon({
|
|
className: 'custom-bubble',
|
|
html: `
|
|
<div class="relative group">
|
|
<div class="w-12 h-12 rounded-full border-4 border-white shadow-lg overflow-hidden transition-transform group-hover:scale-110">
|
|
<img src="${tourImage}" class="w-full h-full object-cover" />
|
|
</div>
|
|
<div class="absolute -bottom-1 -right-1 bg-blue-600 rounded-full w-5 h-5 border-2 border-white flex items-center justify-center text-[10px] font-black text-white">
|
|
S
|
|
</div>
|
|
</div>
|
|
`,
|
|
iconSize: [48, 48],
|
|
iconAnchor: [24, 24]
|
|
})}
|
|
/>
|
|
</React.Fragment>
|
|
);
|
|
})}
|
|
</MarkerClusterGroup>
|
|
</MapContainer>
|
|
|
|
{/* Admin Modal */}
|
|
<UserManagementModal isOpen={isAdminModalOpen} onClose={() => setIsAdminModalOpen(false)} />
|
|
|
|
{/* Create Tour Modal */}
|
|
<CreateTourModal
|
|
isOpen={isCreateModalOpen}
|
|
onClose={() => setIsCreateModalOpen(false)}
|
|
onSuccess={(tour) => {
|
|
fetchTour(tour.id);
|
|
onViewTour(tour.id);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}; |