diff --git a/web/src/components/MemoMetadata/Location/LocationDialog.tsx b/web/src/components/MemoMetadata/Location/LocationDialog.tsx index bc8822e3b..9b85ef873 100644 --- a/web/src/components/MemoMetadata/Location/LocationDialog.tsx +++ b/web/src/components/MemoMetadata/Location/LocationDialog.tsx @@ -35,7 +35,7 @@ export const LocationDialog = ({ return ( - + @@ -47,7 +47,7 @@ export const LocationDialog = ({
- +
diff --git a/web/src/components/map/LocationPicker.tsx b/web/src/components/map/LocationPicker.tsx index 7980a4fa8..14513c03c 100644 --- a/web/src/components/map/LocationPicker.tsx +++ b/web/src/components/map/LocationPicker.tsx @@ -6,26 +6,25 @@ import { MapContainer, Marker, useMap, useMapEvents } from "react-leaflet"; import { cn } from "@/lib/utils"; import { defaultMarkerIcon, ThemedTileLayer } from "./map-utils"; -interface MarkerProps { +interface LocationMarkerProps { position: LatLng | undefined; onChange: (position: LatLng) => void; readonly?: boolean; } -const LocationMarker = (props: MarkerProps) => { - const [position, setPosition] = useState(props.position); +const LocationMarker = ({ position: initialPosition, onChange, readonly: readOnly }: LocationMarkerProps) => { + const [position, setPosition] = useState(initialPosition); const initializedRef = useRef(false); const map = useMapEvents({ click(e) { - if (props.readonly) { + if (readOnly) { return; } setPosition(e.latlng); map.locate(); - // Call the parent onChange function. - props.onChange(e.latlng); + onChange(e.latlng); }, locationfound() {}, }); @@ -37,15 +36,14 @@ const LocationMarker = (props: MarkerProps) => { } }, [map]); - // Keep marker and map in sync with external position updates useEffect(() => { - if (props.position) { - setPosition(props.position); - map.setView(props.position); + if (initialPosition) { + setPosition(initialPosition); + map.setView(initialPosition); } else { setPosition(undefined); } - }, [props.position, map]); + }, [initialPosition, map]); return position === undefined ? null : ; }; @@ -197,22 +195,29 @@ const MapCleanup = () => { return null; }; -interface MapProps { +interface LocationPickerProps { readonly?: boolean; latlng?: LatLng; onChange?: (position: LatLng) => void; + className?: string; } const DEFAULT_CENTER_LAT_LNG = new LatLng(48.8584, 2.2945); +const noopOnLocationChange = () => {}; -const LeafletMap = (props: MapProps) => { - const position = props.latlng || DEFAULT_CENTER_LAT_LNG; - const statusLabel = props.readonly ? "Pinned location" : props.latlng ? "Selected location" : "Choose a location"; +const LocationPicker = ({ readonly: readOnly = false, latlng, onChange = noopOnLocationChange, className }: LocationPickerProps) => { + const position = latlng || DEFAULT_CENTER_LAT_LNG; + const statusLabel = readOnly ? "Pinned location" : latlng ? "Selected location" : "Choose a location"; return ( -
+
{ attributionControl={false} > - {}} /> - + + @@ -234,4 +239,4 @@ const LeafletMap = (props: MapProps) => { ); }; -export default LeafletMap; +export default LocationPicker;