import { useRef } from 'react' import { Marker, Popup } from 'react-leaflet' import { useTranslation } from 'react-i18next' import { pointIconGreen, pointIconBlue, pointIconRed } from './mapIcons' import { PURPOSES } from './types' import type { FlightPoint, MovingPointInfo } from './types' import type L from 'leaflet' interface Props { point: FlightPoint points: FlightPoint[] index: number mapElement: HTMLElement | null onDrag: (index: number, position: { lat: number; lng: number }) => void onDragEnd: (index: number, position: { lat: number; lng: number }) => void onAltitudeChange: (index: number, altitude: number) => void onMetaChange: (index: number, meta: string[]) => void onRemove: (id: string) => void onMoving: (info: MovingPointInfo | null) => void } export default function MapPoint({ point, points, index, mapElement, onDrag, onDragEnd, onAltitudeChange, onMetaChange, onRemove, onMoving, }: Props) { const { t } = useTranslation() const markerRef = useRef(null) const icon = index === 0 ? pointIconGreen : index === points.length - 1 ? pointIconRed : pointIconBlue const handleMove = (e: L.LeafletEvent) => { const marker = markerRef.current if (!marker || !mapElement) return const markerEl = (marker as unknown as { _icon: HTMLElement })._icon if (!markerEl) return const mapRect = mapElement.getBoundingClientRect() const mRect = markerEl.getBoundingClientRect() const dx = mRect.left - mapRect.left + mRect.width > mapRect.width / 2 ? -150 : 200 const dy = mRect.top + mRect.height > mapRect.height / 2 ? -150 : 150 onMoving({ x: mRect.left - mapRect.left + dx, y: mRect.top - mapRect.top + dy, latlng: (e.target as L.Marker).getLatLng() }) } const toggleMeta = (value: string) => { const newMeta = point.meta.includes(value) ? point.meta.filter(m => m !== value) : [...point.meta, value] onMetaChange(index, newMeta) } return ( onDrag(index, (e.target as L.Marker).getLatLng()), dragend: (e) => { onDragEnd(index, (e.target as L.Marker).getLatLng()); onMoving(null) }, move: handleMove, }} >
{t('flights.planner.point')} {index + 1}
onAltitudeChange(index, Number(e.target.value))} className="w-full accent-az-orange" /> {point.altitude}m
{PURPOSES.map(p => ( ))}
) }