-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmissionItems.jsx
More file actions
138 lines (118 loc) · 4.28 KB
/
missionItems.jsx
File metadata and controls
138 lines (118 loc) · 4.28 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
128
129
130
131
132
133
134
135
136
137
138
/*
The MissionItems component returns the markers and lines connecting the
markers together on the dashboard map to display. It also filters out any
items which should not be displayed on the map as markers or not have lines
connecting them.
*/
// Helper imports
import { intToCoord } from "../../helpers/dataFormatters"
import { filterMissionItems } from "../../helpers/filterMissions"
// Styling imports
import "maplibre-gl/dist/maplibre-gl.css"
// Component imports
import DrawLineCoordinates from "./drawLineCoordinates"
import MarkerPin from "./markerPin"
// Tailing styling
import { useEffect, useState } from "react"
import resolveConfig from "tailwindcss/resolveConfig"
import tailwindConfig from "../../../tailwind.config"
const tailwindColors = resolveConfig(tailwindConfig).theme.colors
export default function MissionItems({
missionItems,
editable = false,
dragEndCallback = () => {},
}) {
const [filteredMissionItems, setFilteredMissionItems] = useState(
filterMissionItems(missionItems),
)
const [listOfLineCoords, setListOfLineCoords] = useState([])
const [listOfDottedLineCoords, setListOfDottedLineCoords] = useState([])
useEffect(() => {
setFilteredMissionItems(filterMissionItems(missionItems))
}, [missionItems])
useEffect(() => {
const { solid: solidLineCoords, dotted: dottedLineCoords } =
getListOfLineCoordinates(filteredMissionItems)
setListOfLineCoords(solidLineCoords)
setListOfDottedLineCoords(dottedLineCoords)
}, [filteredMissionItems])
function getListOfLineCoordinates(filteredMissionItems) {
if (filteredMissionItems.length === 0) return { solid: [], dotted: [] }
const lineCoordsList = []
const dottedLineCoordsList = []
// Stop processing waypoints after a land command
const landCommandIndex = filteredMissionItems.findIndex((item) =>
[21, 189].includes(item.command),
)
const itemsToProcess =
landCommandIndex === -1
? filteredMissionItems
: filteredMissionItems.slice(0, landCommandIndex + 1)
itemsToProcess.forEach((item) => {
lineCoordsList.push([intToCoord(item.y), intToCoord(item.x)])
})
// Join the last item to first item if aircraft does not land, with a
// dotted line
if (
![21, 189].includes(
itemsToProcess[itemsToProcess.length - 1].command, // Use itemsToProcess here
)
) {
dottedLineCoordsList.push([
intToCoord(itemsToProcess[0].y), // Use itemsToProcess here
intToCoord(itemsToProcess[0].x),
])
dottedLineCoordsList.push([
intToCoord(itemsToProcess[itemsToProcess.length - 1].y), // Use itemsToProcess here
intToCoord(itemsToProcess[itemsToProcess.length - 1].x),
])
}
// Connect jump commands to previously displayed item and jump target item
const jumpCommandItems = missionItems.filter((item) => item.command === 177)
jumpCommandItems.forEach((jumpItem) => {
const nextItem = filteredMissionItems.find((item) => {
return item.seq === jumpItem.param1
})
if (nextItem === undefined) return
const lastFilteredItem = filteredMissionItems
.filter((item) => item.seq < jumpItem.seq)
.at(-1)
lineCoordsList.push([
intToCoord(lastFilteredItem.y),
intToCoord(lastFilteredItem.x),
])
lineCoordsList.push([intToCoord(nextItem.y), intToCoord(nextItem.x)])
})
return { solid: lineCoordsList, dotted: dottedLineCoordsList }
}
return (
<>
{/* Show mission item LABELS */}
{filteredMissionItems.map((item, index) => {
return (
<MarkerPin
key={index}
id={item.id}
lat={intToCoord(item.x)}
lon={intToCoord(item.y)}
colour={tailwindColors.yellow[400]}
text={item.seq}
tooltipText={item.z ? `Alt: ${item.z}` : null}
draggable={editable}
dragEndCallback={dragEndCallback}
/>
)
})}
{/* Show mission item outlines */}
<DrawLineCoordinates
coordinates={listOfLineCoords}
colour={tailwindColors.yellow[400]}
/>
<DrawLineCoordinates
coordinates={listOfDottedLineCoords}
colour={tailwindColors.yellow[400]}
lineProps={{ "line-dasharray": [2, 2] }}
/>
</>
)
}