-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPointEditionLayers.tsx
137 lines (125 loc) · 4.65 KB
/
PointEditionLayers.tsx
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
128
129
130
131
132
133
134
135
136
137
import { useContext, useMemo, useState } from 'react';
import { featureCollection } from '@turf/helpers';
import { isEqual } from 'lodash';
import type { Map } from 'maplibre-gl';
import { Popup } from 'react-map-gl/dist/esm/exports-maplibre';
import { useSelector } from 'react-redux';
import EntitySumUp from 'applications/editor/components/EntitySumUp';
import EditorContext from 'applications/editor/context';
import { NEW_ENTITY_ID, cleanSymbolType, flattenEntity } from 'applications/editor/data/utils';
import { POINT_LAYER_ID } from 'applications/editor/tools/pointEdition/consts';
import type { PointEditionState } from 'applications/editor/tools/pointEdition/types';
import type { ExtendedEditorContextType } from 'applications/editor/types';
import type { EditorEntity } from 'applications/editor/typesEditorEntity';
import colors from 'common/Map/Consts/colors';
import GeoJSONs, {
EditorSource,
SourcesDefinitionsIndex,
} from 'common/Map/Layers/InfraObjectLayers/GeoJSONs';
import { useInfraID } from 'common/osrdContext';
import { getMap } from 'reducers/map/selectors';
import { NULL_GEOMETRY } from 'types';
interface BasePointEditionLayersProps {
// eslint-disable-next-line react/no-unused-prop-types
map: Map;
mergeEntityWithNearestPoint?: (
entity: EditorEntity,
nearestPoint: NonNullable<PointEditionState<EditorEntity>['nearestPoint']>
) => EditorEntity;
interactiveLayerIDRegex?: RegExp;
}
export const BasePointEditionLayers = ({
mergeEntityWithNearestPoint,
interactiveLayerIDRegex,
}: BasePointEditionLayersProps) => {
const infraID = useInfraID();
const {
renderingFingerprint,
state: { nearestPoint, mousePosition, entity, objType },
editorState: { editorLayers },
} = useContext(EditorContext) as ExtendedEditorContextType<PointEditionState<EditorEntity>>;
const { mapStyle, layersSettings, issuesSettings } = useSelector(getMap);
const [showPopup, setShowPopup] = useState(true);
const renderedEntity = useMemo(() => {
let res: EditorEntity | null = null;
if (entity.geometry && !isEqual(entity.geometry, NULL_GEOMETRY)) {
res = entity;
} else if (nearestPoint) {
if (mergeEntityWithNearestPoint) {
res = mergeEntityWithNearestPoint(entity, nearestPoint);
} else {
res = {
...entity,
geometry: nearestPoint.feature.geometry,
properties: entity.properties,
};
}
} else if (mousePosition) {
res = { ...entity, geometry: { type: 'Point', coordinates: mousePosition } };
}
return res;
}, [entity, mergeEntityWithNearestPoint, mousePosition, nearestPoint]);
const flatRenderedEntity = useMemo(
() => (renderedEntity ? flattenEntity(renderedEntity) : null),
[renderedEntity]
);
const type = cleanSymbolType((entity.properties || {}).extensions?.sncf?.installation_type || '');
const layers = useMemo(
() =>
SourcesDefinitionsIndex[objType](
{
prefix: '',
colors: colors[mapStyle],
isEmphasized: true,
showIGNBDORTHO: false,
layersSettings,
issuesSettings,
},
`editor/${objType}/`
).map((layer) =>
// Quick hack to keep a proper interactive layer:
layer?.id?.match(interactiveLayerIDRegex || /-main$/)
? { ...layer, id: POINT_LAYER_ID }
: layer
),
[interactiveLayerIDRegex, mapStyle, objType, type, layersSettings, issuesSettings]
);
return (
<>
{/* Editor data layer */}
<GeoJSONs
colors={colors[mapStyle]}
hidden={entity.properties.id !== NEW_ENTITY_ID ? [entity.properties.id] : undefined}
layers={editorLayers}
fingerprint={renderingFingerprint}
layersSettings={layersSettings}
issuesSettings={issuesSettings}
infraID={infraID}
/>
{/* Edited entity */}
<EditorSource layers={layers} data={flatRenderedEntity || featureCollection([])} />
{showPopup && renderedEntity && renderedEntity.geometry.type === 'Point' && (
<Popup
className="popup py-2"
anchor="bottom"
longitude={renderedEntity.geometry.coordinates[0]}
latitude={renderedEntity.geometry.coordinates[1]}
onClose={() => setShowPopup(false)}
>
<EntitySumUp entity={renderedEntity} status="edited" />
</Popup>
)}
</>
);
};
export const SignalEditionLayers = ({ map }: { map: Map }) => (
<BasePointEditionLayers
map={map}
interactiveLayerIDRegex={/signal-point$/}
mergeEntityWithNearestPoint={(entity, nearestPoint) => ({
...entity,
geometry: nearestPoint.feature.geometry,
properties: entity.properties,
})}
/>
);