|
2 | 2 |
|
3 | 3 | import { useEffect, useState } from "react" |
4 | 4 |
|
5 | | -import { Button, Table, TextInput } from "@mantine/core" |
| 5 | +import { Button, Table, TextInput, Tooltip } from "@mantine/core" |
6 | 6 | import { useElementSize, useViewportSize } from "@mantine/hooks" |
| 7 | +import { IconInfoCircle } from "@tabler/icons-react" |
7 | 8 | import { |
8 | 9 | showErrorNotification, |
9 | 10 | showSuccessNotification, |
10 | 11 | } from "../../helpers/notification" |
| 12 | +import { loadParamDefinitionsForVersion } from "../../helpers/paramDefinitions" |
| 13 | + |
| 14 | +function getBitmaskInfo(rawValue, paramDef) { |
| 15 | + const bitmaskDef = paramDef?.Bitmask |
| 16 | + if (!bitmaskDef || typeof bitmaskDef !== "object") { |
| 17 | + return { |
| 18 | + isBitmask: false, |
| 19 | + displayValue: String(rawValue), |
| 20 | + selected: [], |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + const numericValue = Number(rawValue) |
| 25 | + if (!Number.isFinite(numericValue)) { |
| 26 | + return { |
| 27 | + isBitmask: false, |
| 28 | + displayValue: String(rawValue), |
| 29 | + selected: [], |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const bitmaskValue = Math.trunc(numericValue) >>> 0 |
| 34 | + const selected = Object.entries(bitmaskDef) |
| 35 | + .map(([bitKey, label]) => ({ |
| 36 | + bit: Number.parseInt(bitKey, 10), |
| 37 | + label, |
| 38 | + })) |
| 39 | + .filter((item) => Number.isInteger(item.bit) && item.bit >= 0) |
| 40 | + .sort((a, b) => a.bit - b.bit) |
| 41 | + .filter((item) => item.bit < 32) |
| 42 | + .filter((item) => (bitmaskValue & (1 << item.bit)) !== 0) |
| 43 | + |
| 44 | + return { |
| 45 | + isBitmask: true, |
| 46 | + displayValue: String(bitmaskValue), |
| 47 | + selected, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function renderParamValue(value, paramDef) { |
| 52 | + const bitmaskInfo = getBitmaskInfo(value, paramDef) |
| 53 | + |
| 54 | + if (!bitmaskInfo.isBitmask) { |
| 55 | + return bitmaskInfo.displayValue |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="flex items-center gap-1.5"> |
| 60 | + <span>{bitmaskInfo.displayValue}</span> |
| 61 | + {bitmaskInfo.displayValue !== "0" && bitmaskInfo.selected.length > 0 && ( |
| 62 | + <Tooltip |
| 63 | + multiline |
| 64 | + w={300} |
| 65 | + label={ |
| 66 | + bitmaskInfo.selected.length > 0 && ( |
| 67 | + <div className="text-sm"> |
| 68 | + {bitmaskInfo.selected.map((item) => ( |
| 69 | + <div key={item.bit}>{`${item.bit}: ${item.label}`}</div> |
| 70 | + ))} |
| 71 | + </div> |
| 72 | + ) |
| 73 | + } |
| 74 | + > |
| 75 | + <span className="inline-flex"> |
| 76 | + <IconInfoCircle size={16} color="white" /> |
| 77 | + </span> |
| 78 | + </Tooltip> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + ) |
| 82 | +} |
11 | 83 |
|
12 | 84 | export default function FlaParamsWindow() { |
13 | 85 | const [params, setParams] = useState(null) |
14 | 86 | const [fileName, setFileName] = useState("") |
| 87 | + const [aircraftKey, setAircraftKey] = useState(null) |
| 88 | + const [firmwareVersion, setFirmwareVersion] = useState(null) |
| 89 | + const [paramDefs, setParamDefs] = useState({}) |
15 | 90 | const [paramSearchValue, setParamSearchValue] = useState("") |
16 | 91 | const { height } = useViewportSize() |
17 | 92 | const { ref, height: searchBarHeight } = useElementSize() |
18 | 93 |
|
| 94 | + useEffect(() => { |
| 95 | + let cancelled = false |
| 96 | + |
| 97 | + loadParamDefinitionsForVersion(aircraftKey, firmwareVersion).then( |
| 98 | + (result) => { |
| 99 | + if (!cancelled) { |
| 100 | + setParamDefs(result.paramDefs) |
| 101 | + } |
| 102 | + }, |
| 103 | + ) |
| 104 | + |
| 105 | + return () => { |
| 106 | + cancelled = true |
| 107 | + } |
| 108 | + }, [aircraftKey, firmwareVersion]) |
| 109 | + |
19 | 110 | async function saveParamsToFile() { |
20 | 111 | try { |
21 | 112 | const options = { |
@@ -56,6 +147,12 @@ export default function FlaParamsWindow() { |
56 | 147 | const handler = (_event, data) => { |
57 | 148 | setParams(data.params) |
58 | 149 | setFileName(data.fileName) |
| 150 | + setAircraftKey( |
| 151 | + data.aircraftType === "plane" || data.aircraftType === "copter" |
| 152 | + ? data.aircraftType |
| 153 | + : null, |
| 154 | + ) |
| 155 | + setFirmwareVersion(data.firmwareVersion ?? null) |
59 | 156 | } |
60 | 157 | window.ipcRenderer.on("app:send-fla-params", handler) |
61 | 158 |
|
@@ -105,7 +202,9 @@ export default function FlaParamsWindow() { |
105 | 202 | .map((param) => ( |
106 | 203 | <Table.Tr key={param.name}> |
107 | 204 | <Table.Td>{param.name}</Table.Td> |
108 | | - <Table.Td>{param.value}</Table.Td> |
| 205 | + <Table.Td> |
| 206 | + {renderParamValue(param.value, paramDefs[param.name])} |
| 207 | + </Table.Td> |
109 | 208 | </Table.Tr> |
110 | 209 | ))} |
111 | 210 | </Table.Tbody> |
|
0 commit comments