-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConnectedMap.tsx
More file actions
127 lines (110 loc) · 4.09 KB
/
ConnectedMap.tsx
File metadata and controls
127 lines (110 loc) · 4.09 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { useMemo } from 'react';
import { useAtom } from '@reatom/npm-react';
import { useAction } from '@reatom/react-v2';
import { configRepo } from '~core/config';
import { currentMapPositionAtom } from '~core/shared_state/currentMapPosition';
import { currentMapAtom } from '~core/shared_state/currentMap';
import { layersOrderManager } from '~core/logical_layers/utils/layersOrder/layersOrder';
import { layersSettingsAtom } from '~core/logical_layers/atoms/layersSettings';
import { mapLibreParentsIds } from '~core/logical_layers/utils/layersOrder/mapLibreParentsIds';
import { mapPopoverRegistry } from '~core/map/popover/globalMapPopoverRegistry';
import {
MapPopoverProvider,
useMapPopoverService,
} from '~core/map/popover/MapPopoverProvider';
import { useMapPopoverMaplibreIntegration } from '~core/map/hooks/useMapPopoverMaplibreIntegration';
import { registerMapListener } from '~core/shared_state/mapListeners';
import { MapLibreContainer } from './MapLibreContainer';
import type {
Map as MapLibreMap,
MapOptions as MapLibreOptions,
LayerSpecification,
MapMouseEvent,
} from 'maplibre-gl';
export type ApplicationMap = MapLibreMap;
export type ApplicationLayer = LayerSpecification;
export function ConnectedMap({ className }: { className?: string }) {
// do not subsctibe to atom, just read initial value
const [initialPosition] = useAtom(currentMapPositionAtom, [], false);
const setCurrentMap = useAction(currentMapAtom.setMap);
const options = useMemo(
() => ({
getConfig: () => {
const config: Partial<MapLibreOptions> = {
style: configRepo.get().mapBaseStyle,
attributionControl: false,
};
// Apply URL position as initial map position ONLY
if (initialPosition) {
if ('bbox' in initialPosition) {
config.bounds = initialPosition.bbox;
} else {
config.center = [initialPosition.lng, initialPosition.lat];
config.zoom = initialPosition.zoom;
}
}
return config as Omit<MapLibreOptions, 'container'>;
},
onMapCreated: (map: MapLibreMap) => {
// Global map access for debugging
if (!globalThis.KONTUR_MAP) {
globalThis.KONTUR_MAP = map;
}
// Apply default extent if no URL position was used
if (!initialPosition) {
const extent = configRepo.get().extent;
if (extent) {
map.fitBounds(extent, { animate: false });
}
}
setCurrentMap(map);
// Set up light for extrusion layers
map.once('styledata', () => {
map.setLight({ anchor: 'viewport', color: '#FFF', intensity: 1 });
});
// Disable rotation
map.touchZoomRotate.disableRotation();
// Layers manager initialization - wait for map to load
map.once('load', () => {
layersOrderManager.init(map, mapLibreParentsIds, layersSettingsAtom);
});
},
onMapDestroy: (map: MapLibreMap) => {
layersOrderManager.destroy();
if (globalThis.KONTUR_MAP === map) {
globalThis.KONTUR_MAP = undefined;
}
},
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
);
return (
<MapPopoverProvider registry={mapPopoverRegistry}>
<MapLibreContainer options={options} className={className}>
{(map) => <MapIntegration map={map} />}
</MapLibreContainer>
</MapPopoverProvider>
);
}
// should we repsect featureFlags[AppFeature.TOOLTIP] ?
function MapIntegration({ map }: { map: MapLibreMap }) {
const popoverService = useMapPopoverService();
// Create global event handlers using priority system
const eventHandlers = useMemo(
() => ({
onClick: (handler: (event: MapMouseEvent) => boolean) =>
registerMapListener('click', handler, 55),
onMove: (handler: () => boolean) => registerMapListener('move', handler, 80),
}),
[],
);
// MapPopover integration with proper position tracking
useMapPopoverMaplibreIntegration({
map,
popoverService,
trackingThrottleMs: 16,
eventHandlers,
});
return null;
}