1 |
48690561
|
Vaclav Honzik
|
import { Fragment, useEffect, useState } from 'react'
|
2 |
a7ae217f
|
Vaclav Honzik
|
import { useDispatch, useSelector } from 'react-redux'
|
3 |
812b9f90
|
Vaclav Honzik
|
import { RootState } from '../../redux/store'
|
4 |
48690561
|
Vaclav Honzik
|
import { MapPoint, isMapPointDisplayable } from '../trackingToolUtils'
|
5 |
a7ae217f
|
Vaclav Honzik
|
import TextPath from 'react-leaflet-textpath'
|
6 |
14588cb6
|
Vaclav Honzik
|
import { updateMapPoint } from '../trackingToolSlice'
|
7 |
a7ae217f
|
Vaclav Honzik
|
import MapMarker from './MapMarker'
|
8 |
|
|
import { LatLngTuple } from 'leaflet'
|
9 |
|
|
import { Popup, Tooltip } from 'react-leaflet'
|
10 |
|
|
import { Checkbox, FormControlLabel, Stack, Typography } from '@mui/material'
|
11 |
812b9f90
|
Vaclav Honzik
|
import { formatHtmlStringToReactDom } from '../../../utils/formatting/HtmlUtils'
|
12 |
|
|
import { DialogCatalogItemDetail as CatalogItemDetailDialog } from '../../Catalog/CatalogItemDetail'
|
13 |
b70813cb
|
Vaclav Honzik
|
|
14 |
de12c6be
|
Vaclav Honzik
|
type EdgeElement = any
|
15 |
48690561
|
Vaclav Honzik
|
const MapPath = () => {
|
16 |
dd270a41
|
Vaclav Honzik
|
const dispatch = useDispatch()
|
17 |
14588cb6
|
Vaclav Honzik
|
|
18 |
|
|
// Displayed path contains all map points
|
19 |
48690561
|
Vaclav Honzik
|
const path = useSelector(
|
20 |
|
|
(state: RootState) => state.trackingTool.displayedPath
|
21 |
8c57f958
|
Vaclav Honzik
|
)
|
22 |
48690561
|
Vaclav Honzik
|
// Color of the path
|
23 |
|
|
const pathColor = '#346eeb'
|
24 |
8c57f958
|
Vaclav Honzik
|
|
25 |
de12c6be
|
Vaclav Honzik
|
// List of all active map points
|
26 |
|
|
const [displayableMapPoints, setDisplayableMapPoints] = useState<
|
27 |
|
|
MapPoint[]
|
28 |
|
|
>([])
|
29 |
8c57f958
|
Vaclav Honzik
|
useEffect(() => {
|
30 |
48690561
|
Vaclav Honzik
|
if (!path) {
|
31 |
|
|
setDisplayableMapPoints([])
|
32 |
|
|
return
|
33 |
|
|
}
|
34 |
|
|
|
35 |
de12c6be
|
Vaclav Honzik
|
// Set all displayable vertices
|
36 |
a7ae217f
|
Vaclav Honzik
|
setDisplayableMapPoints(
|
37 |
|
|
path.filter((mapPoint) => isMapPointDisplayable(mapPoint))
|
38 |
|
|
)
|
39 |
de12c6be
|
Vaclav Honzik
|
}, [path])
|
40 |
b70813cb
|
Vaclav Honzik
|
|
41 |
de12c6be
|
Vaclav Honzik
|
// List of all edges in the path
|
42 |
|
|
const [edges, setEdges] = useState<EdgeElement[]>([])
|
43 |
dd270a41
|
Vaclav Honzik
|
useEffect(() => {
|
44 |
de12c6be
|
Vaclav Honzik
|
// Get all active map points
|
45 |
|
|
const activeMapPoints = displayableMapPoints.filter(
|
46 |
f41a4cd3
|
Vaclav Honzik
|
(item) => item.addToPath && !item.hidden
|
47 |
dd270a41
|
Vaclav Honzik
|
)
|
48 |
b70813cb
|
Vaclav Honzik
|
if (activeMapPoints.length < 2) {
|
49 |
7b864a5c
|
Vaclav Honzik
|
setEdges([])
|
50 |
de12c6be
|
Vaclav Honzik
|
return
|
51 |
b70813cb
|
Vaclav Honzik
|
}
|
52 |
14588cb6
|
Vaclav Honzik
|
console.log('rerender')
|
53 |
b70813cb
|
Vaclav Honzik
|
|
54 |
14588cb6
|
Vaclav Honzik
|
// Keep track of already built edges so we do not render excessive amount of them
|
55 |
|
|
// We can use a simple hack where we concatenate id of both and check if it is already in the set
|
56 |
|
|
const existingPaths = new Set<string>()
|
57 |
de12c6be
|
Vaclav Honzik
|
const edges = []
|
58 |
b70813cb
|
Vaclav Honzik
|
for (let i = 0; i < activeMapPoints.length - 1; i += 1) {
|
59 |
14588cb6
|
Vaclav Honzik
|
const edgeId = `${activeMapPoints[i].id}${
|
60 |
|
|
activeMapPoints[i + 1].id
|
61 |
|
|
}`
|
62 |
|
|
if (existingPaths.has(edgeId)) {
|
63 |
|
|
continue
|
64 |
|
|
}
|
65 |
|
|
existingPaths.add(edgeId)
|
66 |
de12c6be
|
Vaclav Honzik
|
const [start, end] = [
|
67 |
|
|
activeMapPoints[i].catalogItem,
|
68 |
|
|
activeMapPoints[i + 1].catalogItem,
|
69 |
|
|
]
|
70 |
b70813cb
|
Vaclav Honzik
|
edges.push(
|
71 |
|
|
<TextPath
|
72 |
14588cb6
|
Vaclav Honzik
|
key={edgeId}
|
73 |
b70813cb
|
Vaclav Honzik
|
positions={[
|
74 |
de12c6be
|
Vaclav Honzik
|
[start.latitude, start.longitude],
|
75 |
|
|
[end.latitude, end.longitude],
|
76 |
b70813cb
|
Vaclav Honzik
|
]}
|
77 |
|
|
text="►"
|
78 |
|
|
attributes={{
|
79 |
14588cb6
|
Vaclav Honzik
|
'font-size': 17,
|
80 |
48690561
|
Vaclav Honzik
|
fill: pathColor,
|
81 |
b70813cb
|
Vaclav Honzik
|
}}
|
82 |
|
|
repeat
|
83 |
|
|
center
|
84 |
04fdedc6
|
Vaclav Honzik
|
weight={0}
|
85 |
b70813cb
|
Vaclav Honzik
|
/>
|
86 |
|
|
)
|
87 |
|
|
}
|
88 |
de12c6be
|
Vaclav Honzik
|
setEdges(edges)
|
89 |
48690561
|
Vaclav Honzik
|
}, [dispatch, displayableMapPoints])
|
90 |
b70813cb
|
Vaclav Honzik
|
|
91 |
de12c6be
|
Vaclav Honzik
|
// List of vertices to display
|
92 |
|
|
const [vertices, setVertices] = useState<JSX.Element[]>([])
|
93 |
|
|
useEffect(() => {
|
94 |
|
|
// Iterate over all displayable map points and map them to MapMarker
|
95 |
14588cb6
|
Vaclav Honzik
|
const uniqueMapPoints = new Set<string>()
|
96 |
de12c6be
|
Vaclav Honzik
|
setVertices(
|
97 |
14588cb6
|
Vaclav Honzik
|
displayableMapPoints
|
98 |
|
|
.filter((mapPoint) => {
|
99 |
|
|
if (uniqueMapPoints.has(mapPoint.id)) {
|
100 |
|
|
return false
|
101 |
|
|
}
|
102 |
|
|
uniqueMapPoints.add(mapPoint.id)
|
103 |
|
|
return true
|
104 |
|
|
})
|
105 |
|
|
.map((item) => (
|
106 |
|
|
<MapMarker
|
107 |
|
|
key={item.id}
|
108 |
|
|
position={[
|
109 |
|
|
item.catalogItem.latitude as number,
|
110 |
|
|
item.catalogItem.longitude as number,
|
111 |
|
|
]}
|
112 |
|
|
mapPoint={item}
|
113 |
|
|
updatePositionCallbackFn={(position: LatLngTuple) => {
|
114 |
|
|
dispatch(
|
115 |
|
|
updateMapPoint({
|
116 |
|
|
...item,
|
117 |
|
|
catalogItem: {
|
118 |
|
|
...item.catalogItem,
|
119 |
|
|
latitude: position[0],
|
120 |
|
|
longitude: position[1],
|
121 |
|
|
},
|
122 |
|
|
})
|
123 |
|
|
)
|
124 |
|
|
}}
|
125 |
|
|
>
|
126 |
|
|
<Fragment>
|
127 |
|
|
<Tooltip>
|
128 |
|
|
{/* <Typography> */}
|
129 |
|
|
{item.catalogItem.name ?? ''}
|
130 |
|
|
{/* </Typography> */}
|
131 |
|
|
</Tooltip>
|
132 |
|
|
<Popup>
|
133 |
|
|
<Fragment>
|
134 |
|
|
<Stack direction="column" sx={{ m: 0 }}>
|
135 |
|
|
<Typography
|
136 |
|
|
variant="h6"
|
137 |
|
|
fontWeight="bold"
|
138 |
|
|
fontSize={16}
|
139 |
|
|
>
|
140 |
|
|
{formatHtmlStringToReactDom(
|
141 |
|
|
item.catalogItem.name as string
|
142 |
|
|
)}
|
143 |
|
|
</Typography>
|
144 |
|
|
<FormControlLabel
|
145 |
|
|
control={
|
146 |
|
|
<Checkbox
|
147 |
|
|
checked={item.addToPath}
|
148 |
|
|
onChange={() => {
|
149 |
|
|
dispatch(
|
150 |
|
|
updateMapPoint({
|
151 |
|
|
...item,
|
152 |
|
|
addToPath:
|
153 |
|
|
!item.addToPath,
|
154 |
|
|
})
|
155 |
|
|
)
|
156 |
|
|
}}
|
157 |
|
|
/>
|
158 |
|
|
}
|
159 |
|
|
labelPlacement="end"
|
160 |
|
|
label="Active"
|
161 |
|
|
/>
|
162 |
|
|
<CatalogItemDetailDialog
|
163 |
|
|
itemId={item.catalogItem.id ?? ''}
|
164 |
|
|
/>
|
165 |
|
|
</Stack>
|
166 |
|
|
</Fragment>
|
167 |
|
|
</Popup>
|
168 |
|
|
</Fragment>
|
169 |
|
|
</MapMarker>
|
170 |
|
|
))
|
171 |
de12c6be
|
Vaclav Honzik
|
)
|
172 |
48690561
|
Vaclav Honzik
|
}, [dispatch, displayableMapPoints])
|
173 |
b70813cb
|
Vaclav Honzik
|
|
174 |
|
|
return (
|
175 |
|
|
<Fragment>
|
176 |
de12c6be
|
Vaclav Honzik
|
{vertices}
|
177 |
|
|
{edges}
|
178 |
b70813cb
|
Vaclav Honzik
|
</Fragment>
|
179 |
|
|
)
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
export default MapPath
|