aboutsummaryrefslogtreecommitdiff
path: root/modern/src/map/SelectedDeviceMap.js
blob: 38cb6abf6f7287b7384bf78bd4df287f887d4cdc (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';

import { ThemeProvider } from '@material-ui/core/styles';
import maplibregl from 'maplibre-gl';
import { Provider, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { map } from './Map';
import store from '../store';
import StatusView from './StatusView';
import theme from '../theme';

let popup;

const SelectedDeviceMap = () => {
  const history = useHistory();

  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 showStatus = (deviceId, coordinates) => {
    const placeholder = document.createElement('div');
    ReactDOM.render(
      <Provider store={store}>
        <ThemeProvider theme={theme}>
          <StatusView
            deviceId={deviceId}
            onShowDetails={(positionId) => history.push(`/position/${positionId}`)}
            onShowHistory={() => history.push('/replay')}
            onEditClick={(deviceId) => history.push(`/device/${deviceId}`)}
          />
        </ThemeProvider>
      </Provider>,
      placeholder,
    );

    if (popup) {
      popup.remove();
    }
    popup = new maplibregl.Popup({
      offset: 25,
      anchor: 'top',
      closeOnClick: true,
    });

    popup.setDOMContent(placeholder).setLngLat(coordinates).addTo(map);
  };

  useEffect(() => {
    if (mapCenter) {
      map.easeTo({ center: mapCenter.position });
    }
  }, [mapCenter]);

  return null;
};

export default SelectedDeviceMap;