Commit a3a5b52d authored by boojack's avatar boojack

chore: tweak location picker

parent ab533290
...@@ -35,7 +35,7 @@ export const LocationDialog = ({ ...@@ -35,7 +35,7 @@ export const LocationDialog = ({
return ( return (
<Dialog open={open} onOpenChange={onOpenChange}> <Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-[min(28rem,calc(100vw-2rem))] p-0!"> <DialogContent className="max-w-[min(28rem,calc(100vw-2rem))] p-0!" showCloseButton={false}>
<VisuallyHidden> <VisuallyHidden>
<DialogClose /> <DialogClose />
</VisuallyHidden> </VisuallyHidden>
...@@ -47,7 +47,7 @@ export const LocationDialog = ({ ...@@ -47,7 +47,7 @@ export const LocationDialog = ({
</VisuallyHidden> </VisuallyHidden>
<div className="flex flex-col"> <div className="flex flex-col">
<div className="w-full h-64 overflow-hidden rounded-t-md bg-muted/30"> <div className="w-full h-64 overflow-hidden rounded-t-md bg-muted/30">
<LocationPicker latlng={position} onChange={onPositionChange} /> <LocationPicker className="h-full" latlng={position} onChange={onPositionChange} />
</div> </div>
<div className="w-full flex flex-col p-3 gap-3"> <div className="w-full flex flex-col p-3 gap-3">
<div className="grid grid-cols-2 gap-3"> <div className="grid grid-cols-2 gap-3">
......
...@@ -6,26 +6,25 @@ import { MapContainer, Marker, useMap, useMapEvents } from "react-leaflet"; ...@@ -6,26 +6,25 @@ import { MapContainer, Marker, useMap, useMapEvents } from "react-leaflet";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { defaultMarkerIcon, ThemedTileLayer } from "./map-utils"; import { defaultMarkerIcon, ThemedTileLayer } from "./map-utils";
interface MarkerProps { interface LocationMarkerProps {
position: LatLng | undefined; position: LatLng | undefined;
onChange: (position: LatLng) => void; onChange: (position: LatLng) => void;
readonly?: boolean; readonly?: boolean;
} }
const LocationMarker = (props: MarkerProps) => { const LocationMarker = ({ position: initialPosition, onChange, readonly: readOnly }: LocationMarkerProps) => {
const [position, setPosition] = useState(props.position); const [position, setPosition] = useState(initialPosition);
const initializedRef = useRef(false); const initializedRef = useRef(false);
const map = useMapEvents({ const map = useMapEvents({
click(e) { click(e) {
if (props.readonly) { if (readOnly) {
return; return;
} }
setPosition(e.latlng); setPosition(e.latlng);
map.locate(); map.locate();
// Call the parent onChange function. onChange(e.latlng);
props.onChange(e.latlng);
}, },
locationfound() {}, locationfound() {},
}); });
...@@ -37,15 +36,14 @@ const LocationMarker = (props: MarkerProps) => { ...@@ -37,15 +36,14 @@ const LocationMarker = (props: MarkerProps) => {
} }
}, [map]); }, [map]);
// Keep marker and map in sync with external position updates
useEffect(() => { useEffect(() => {
if (props.position) { if (initialPosition) {
setPosition(props.position); setPosition(initialPosition);
map.setView(props.position); map.setView(initialPosition);
} else { } else {
setPosition(undefined); setPosition(undefined);
} }
}, [props.position, map]); }, [initialPosition, map]);
return position === undefined ? null : <Marker position={position} icon={defaultMarkerIcon}></Marker>; return position === undefined ? null : <Marker position={position} icon={defaultMarkerIcon}></Marker>;
}; };
...@@ -197,22 +195,29 @@ const MapCleanup = () => { ...@@ -197,22 +195,29 @@ const MapCleanup = () => {
return null; return null;
}; };
interface MapProps { interface LocationPickerProps {
readonly?: boolean; readonly?: boolean;
latlng?: LatLng; latlng?: LatLng;
onChange?: (position: LatLng) => void; onChange?: (position: LatLng) => void;
className?: string;
} }
const DEFAULT_CENTER_LAT_LNG = new LatLng(48.8584, 2.2945); const DEFAULT_CENTER_LAT_LNG = new LatLng(48.8584, 2.2945);
const noopOnLocationChange = () => {};
const LeafletMap = (props: MapProps) => { const LocationPicker = ({ readonly: readOnly = false, latlng, onChange = noopOnLocationChange, className }: LocationPickerProps) => {
const position = props.latlng || DEFAULT_CENTER_LAT_LNG; const position = latlng || DEFAULT_CENTER_LAT_LNG;
const statusLabel = props.readonly ? "Pinned location" : props.latlng ? "Selected location" : "Choose a location"; const statusLabel = readOnly ? "Pinned location" : latlng ? "Selected location" : "Choose a location";
return ( return (
<div className="memo-location-map relative isolate w-full overflow-hidden rounded-xl border border-border bg-background shadow-sm"> <div
className={cn(
"memo-location-map relative isolate h-72 w-full overflow-hidden rounded-xl border border-border bg-background shadow-sm",
className,
)}
>
<MapContainer <MapContainer
className="h-72 w-full !bg-muted" className="h-full w-full !bg-muted"
center={position} center={position}
zoom={13} zoom={13}
scrollWheelZoom={false} scrollWheelZoom={false}
...@@ -220,8 +225,8 @@ const LeafletMap = (props: MapProps) => { ...@@ -220,8 +225,8 @@ const LeafletMap = (props: MapProps) => {
attributionControl={false} attributionControl={false}
> >
<ThemedTileLayer /> <ThemedTileLayer />
<LocationMarker position={position} readonly={props.readonly} onChange={props.onChange ? props.onChange : () => {}} /> <LocationMarker position={position} readonly={readOnly} onChange={onChange} />
<MapControls position={props.latlng} /> <MapControls position={latlng} />
<MapCleanup /> <MapCleanup />
</MapContainer> </MapContainer>
...@@ -234,4 +239,4 @@ const LeafletMap = (props: MapProps) => { ...@@ -234,4 +239,4 @@ const LeafletMap = (props: MapProps) => {
); );
}; };
export default LeafletMap; export default LocationPicker;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment