diff --git a/lib/src/Components/Map/Subcomponents/Controls/LocateControl.tsx b/lib/src/Components/Map/Subcomponents/Controls/LocateControl.tsx index 6877d16c..eb698f2e 100644 --- a/lib/src/Components/Map/Subcomponents/Controls/LocateControl.tsx +++ b/lib/src/Components/Map/Subcomponents/Controls/LocateControl.tsx @@ -42,6 +42,7 @@ export const LocateControl = (): JSX.Element => { const [loading, setLoading] = useState(false) const [showLocationModal, setShowLocationModal] = useState(false) const [foundLocation, setFoundLocation] = useState(null) + const [hasUpdatedPosition, setHasUpdatedPosition] = useState(false) const timeoutRef = useRef(null) useEffect(() => { @@ -64,9 +65,26 @@ export const LocateControl = (): JSX.Element => { setLoading(false) setActive(true) setFoundLocation(e.latlng) - timeoutRef.current = setTimeout(() => { - setShowLocationModal(true) - }, 1000) + + // Only show modal if user is at least 100m away from their current profile position + // and hasn't just updated their position + if (!hasUpdatedPosition) { + if (myProfile.myProfile?.position) { + const currentPos = myProfile.myProfile.position.coordinates + const distance = e.latlng.distanceTo([currentPos[1], currentPos[0]]) + + if (distance >= 100) { + timeoutRef.current = setTimeout(() => { + setShowLocationModal(true) + }, 1000) + } + } else { + // Show modal if user has no current position + timeoutRef.current = setTimeout(() => { + setShowLocationModal(true) + }, 1000) + } + } }, locationerror: () => { setLoading(false) @@ -101,7 +119,9 @@ export const LocateControl = (): JSX.Element => { } const toastId = toast.loading('Updating item position') try { - await myProfile.myProfile.layer?.api?.updateItem!(updatedProfile as Item) + if (myProfile.myProfile.layer?.api?.updateItem) { + await myProfile.myProfile.layer.api.updateItem(updatedProfile as Item) + } success = true } catch (error: unknown) { if (error instanceof Error) { @@ -138,7 +158,15 @@ export const LocateControl = (): JSX.Element => { }) setFoundLocation(null) setActive(false) - lc.stop() + setHasUpdatedPosition(true) + if (timeoutRef.current) { + clearTimeout(timeoutRef.current) + timeoutRef.current = null + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + if (lc) lc.stop() + // Reset flag after a delay to allow future updates + setTimeout(() => setHasUpdatedPosition(false), 5000) } } } @@ -183,7 +211,7 @@ export const LocateControl = (): JSX.Element => {