Fix manual positions

This commit is contained in:
Sacha Weatherstone
2022-10-29 22:36:05 +10:00
parent 8538ba4f6a
commit 5c68c552e2
3 changed files with 46 additions and 25 deletions
@@ -14,7 +14,10 @@ import { classValidatorResolver } from "@hookform/resolvers/class-validator";
import { Protobuf } from "@meshtastic/meshtasticjs";
export const Position = (): JSX.Element => {
const { config, connection } = useDevice();
const { config, connection, nodes, hardware } = useDevice();
const myNode = nodes.find((n) => n.data.num === hardware.myNodeNum);
const {
register,
handleSubmit,
@@ -22,7 +25,12 @@ export const Position = (): JSX.Element => {
reset,
control,
} = useForm<PositionValidation>({
defaultValues: config.position,
defaultValues: {
fixedAlt: myNode?.data.position?.altitude,
fixedLat: (myNode?.data.position?.latitudeI ?? 0) / 1e7,
fixedLng: (myNode?.data.position?.longitudeI ?? 0) / 1e7,
...config.position,
},
resolver: classValidatorResolver(PositionValidation),
});
@@ -33,12 +41,22 @@ export const Position = (): JSX.Element => {
});
useEffect(() => {
reset(config.position);
}, [reset, config.position]);
reset({
fixedAlt: myNode?.data.position?.altitude,
fixedLat: (myNode?.data.position?.latitudeI ?? 0) / 1e7,
fixedLng: (myNode?.data.position?.longitudeI ?? 0) / 1e7,
...config.position,
});
}, [reset, config.position, myNode?.data.position]);
const onSubmit = handleSubmit((data) => {
const { fixedAlt, fixedLat, fixedLng, ...rest } = data;
const configHasChanged = !Protobuf.Config_PositionConfig.equals(
config.position,
Protobuf.Config_PositionConfig.create(rest)
);
if (connection) {
void toast.promise(
connection.sendPacket(
@@ -66,25 +84,27 @@ export const Position = (): JSX.Element => {
error: "No response received",
}
);
void toast.promise(
connection.setConfig(
{
payloadVariant: {
oneofKind: "position",
position: rest,
if (configHasChanged) {
void toast.promise(
connection.setConfig(
{
payloadVariant: {
oneofKind: "position",
position: rest,
},
},
},
async () => {
reset({ ...data });
await Promise.resolve();
async () => {
reset({ ...data });
await Promise.resolve();
}
),
{
loading: "Saving...",
success: "Saved Position Config, Restarting Node",
error: "No response received",
}
),
{
loading: "Saving...",
success: "Saved Position Config, Restarting Node",
error: "No response received",
}
);
);
}
}
});
+1
View File
@@ -47,6 +47,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
disabled ? "cursor-not-allowed bg-orange-50 text-orange-200" : ""
}`}
disabled={disabled}
step="any"
{...rest}
/>
{suffix && (