diff options
author | Anton Tananaev <anton@traccar.org> | 2022-05-01 17:23:27 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-05-01 17:23:27 -0700 |
commit | 3e388e438f0e250bd1852c6fa54f9012f33aea6f (patch) | |
tree | 909274be4a1623275420b3d5a19a2846fd9af8a9 /modern/src/map | |
parent | 05a7a75d6db6185bbea02df51a8506a9b1611bb3 (diff) | |
download | trackermap-web-3e388e438f0e250bd1852c6fa54f9012f33aea6f.tar.gz trackermap-web-3e388e438f0e250bd1852c6fa54f9012f33aea6f.tar.bz2 trackermap-web-3e388e438f0e250bd1852c6fa54f9012f33aea6f.zip |
Add map follow preference
Diffstat (limited to 'modern/src/map')
-rw-r--r-- | modern/src/map/SelectedDeviceMap.js | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/modern/src/map/SelectedDeviceMap.js b/modern/src/map/SelectedDeviceMap.js index c46ea40e..d09fdb9f 100644 --- a/modern/src/map/SelectedDeviceMap.js +++ b/modern/src/map/SelectedDeviceMap.js @@ -3,26 +3,25 @@ import { useEffect } from 'react'; import { useSelector } from 'react-redux'; import dimensions from '../theme/dimensions'; import { map } from './Map'; +import { usePrevious } from '../reactHelper'; +import usePersistedState from '../common/usePersistedState'; const SelectedDeviceMap = () => { - const mapCenter = useSelector((state) => { - if (state.devices.selectedId) { - const position = state.positions.items[state.devices.selectedId] || null; - if (position) { - return { deviceId: state.devices.selectedId, position: [position.longitude, position.latitude] }; - } - } - return null; - }); + const selectedDeviceId = useSelector((state) => state.devices.selectedId); + const previousDeviceId = usePrevious(selectedDeviceId); + + const position = useSelector((state) => state.positions.items[selectedDeviceId]); + + const [mapFollow] = usePersistedState('mapFollow', false); useEffect(() => { - if (mapCenter) { + if ((selectedDeviceId != previousDeviceId || mapFollow) && position) { map.easeTo({ - center: mapCenter.position, + center: [position.longitude, position.latitude], offset: [0, -dimensions.popupMapOffset / 2], }); } - }, [mapCenter]); + }); return null; }; |