"use client"; import { useMemo, useRef } from "react"; import { useSetAtom } from "jotai"; import L, { LatLngLiteral } from "leaflet"; import "leaflet-defaulticon-compatibility"; import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css"; import "leaflet/dist/leaflet.css"; import { useFormContext, useWatch } from "react-hook-form"; import { MapContainer, Marker, TileLayer } from "react-leaflet"; import { mapBoundsAtom } from "@/atoms"; import { MapEvents } from "@/components/MapEvents"; const center: LatLngLiteral = { lat: 47.0844, lng: 2.3964 }; const MapCoordinateSelection = ({ page }: { page: "club" | "tournament" }) => { const { control, setValue } = useFormContext(); const latValue = useWatch({ control, name: `${page}.coordinates.0` }); const lngValue = useWatch({ control, name: `${page}.coordinates.1` }); const setMapBounds = useSetAtom(mapBoundsAtom); const markerRef = useRef(null); const eventHandlers = useMemo( () => ({ dragend() { const marker = markerRef.current; if (marker != null) { const position = marker.getLatLng(); setValue( `${page}.coordinates.0`, Math.round((position.lat + Number.EPSILON) * 10000) / 10000, ); setValue( `${page}.coordinates.1`, Math.round((position.lng + Number.EPSILON) * 10000) / 10000, ); } }, }), [setValue, page], ); return ( { if (map) { setMapBounds(map.getBounds()); } }} > ); }; export default MapCoordinateSelection;