aboutsummaryrefslogtreecommitdiff
path: root/modern/src/map/SelectedDeviceMap.js
blob: 1294085ce4683f82558e179a9850096fb97d42c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 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 ((selectedDeviceId !== previousDeviceId || mapFollow) && position) {
      map.easeTo({
        center: [position.longitude, position.latitude],
        offset: [0, -dimensions.popupMapOffset / 2],
      });
    }
  });

  return null;
};

export default SelectedDeviceMap;