From d79a6965c02d20951051c34e7e70eba14220ab9b Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Mon, 13 Feb 2023 22:45:42 +1000 Subject: [PATCH] Minor fixes, start channel editor migration --- src/components/Dialog/QRDialog.tsx | 4 +- src/components/PageComponents/Channel.tsx | 425 +++++++++++++--------- src/pages/Config/index.tsx | 8 +- src/pages/Map.tsx | 2 +- src/validation/channelSettings.ts | 15 +- 5 files changed, 275 insertions(+), 179 deletions(-) diff --git a/src/components/Dialog/QRDialog.tsx b/src/components/Dialog/QRDialog.tsx index 97cc6890..f26d2012 100644 --- a/src/components/Dialog/QRDialog.tsx +++ b/src/components/Dialog/QRDialog.tsx @@ -63,7 +63,7 @@ export const QRDialog = ({
{channels.map((channel) => ( - <> +
))}
diff --git a/src/components/PageComponents/Channel.tsx b/src/components/PageComponents/Channel.tsx index 089134a9..2f9199f9 100644 --- a/src/components/PageComponents/Channel.tsx +++ b/src/components/PageComponents/Channel.tsx @@ -1,14 +1,9 @@ import { useEffect, useState } from "react"; import { fromByteArray, toByteArray } from "base64-js"; -import { Controller, useForm } from "react-hook-form"; -import { ChannelSettingsValidation } from "@app/validation/channelSettings.js"; -import { Input } from "@components/UI/Input.js"; -import { Switch } from "@components/UI/Switch.js"; +import type { ChannelSettingsValidation } from "@app/validation/channelSettings.js"; import { useDevice } from "@core/stores/deviceStore.js"; -import { RefreshCwIcon, EyeIcon, EyeOffIcon } from "lucide-react"; -import { classValidatorResolver } from "@hookform/resolvers/class-validator"; import { Protobuf } from "@meshtastic/meshtasticjs"; -import { Label } from "@radix-ui/react-label"; +import { DynamicForm } from "../Form/DynamicForm.js"; export interface SettingsPanelProps { channel: Protobuf.Channel; @@ -19,168 +14,268 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => { const [keySize, setKeySize] = useState<128 | 256>(256); const [pskHidden, setPskHidden] = useState(true); - const { - register, - handleSubmit, - formState: { errors, isDirty }, - reset, - control, - setValue - } = useForm({ - defaultValues: { - enabled: [ - Protobuf.Channel_Role.SECONDARY, - Protobuf.Channel_Role.PRIMARY - ].find((role) => role === channel?.role) - ? true - : false, - ...channel?.settings, - psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)) - }, - resolver: classValidatorResolver(ChannelSettingsValidation) - }); + // const { + // register, + // handleSubmit, + // formState: { errors, isDirty }, + // reset, + // control, + // setValue + // } = useForm({ + // defaultValues: { + // enabled: [ + // Protobuf.Channel_Role.SECONDARY, + // Protobuf.Channel_Role.PRIMARY + // ].find((role) => role === channel?.role) + // ? true + // : false, + // ...channel?.settings, + // psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)) + // }, + // resolver: classValidatorResolver(ChannelSettingsValidation) + // }); - useEffect(() => { - reset({ - enabled: [ - Protobuf.Channel_Role.SECONDARY, - Protobuf.Channel_Role.PRIMARY - ].find((role) => role === channel?.role) - ? true - : false, - ...channel?.settings, - psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)) - }); - }, [channel, reset]); + // useEffect(() => { + // reset({ + // enabled: [ + // Protobuf.Channel_Role.SECONDARY, + // Protobuf.Channel_Role.PRIMARY + // ].find((role) => role === channel?.role) + // ? true + // : false, + // ...channel?.settings, + // psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)) + // }); + // }, [channel, reset]); - const onSubmit = handleSubmit((data) => { - connection - ?.setChannel( - new Protobuf.Channel({ - role: - channel?.role === Protobuf.Channel_Role.PRIMARY - ? Protobuf.Channel_Role.PRIMARY - : data.enabled - ? Protobuf.Channel_Role.SECONDARY - : Protobuf.Channel_Role.DISABLED, - index: channel?.index, - settings: { - ...data, - psk: toByteArray(data.psk ?? "") - } - }) - ) - .then(() => - addChannel({ - config: new Protobuf.Channel({ - index: channel.index, - role: channel.role, - settings: { - ...data, - psk: toByteArray(data.psk ?? "") - } - }), - lastInterraction: new Date(), - messages: [] - }) - ); - }); + // const onSubmit = handleSubmit((data) => { + // connection + // ?.setChannel( + // new Protobuf.Channel({ + // role: + // channel?.role === Protobuf.Channel_Role.PRIMARY + // ? Protobuf.Channel_Role.PRIMARY + // : data.enabled + // ? Protobuf.Channel_Role.SECONDARY + // : Protobuf.Channel_Role.DISABLED, + // index: channel?.index, + // settings: { + // ...data, + // psk: toByteArray(data.psk ?? "") + // } + // }) + // ) + // .then(() => + // addChannel({ + // config: new Protobuf.Channel({ + // index: channel.index, + // role: channel.role, + // settings: { + // ...data, + // psk: toByteArray(data.psk ?? "") + // } + // }), + // lastInterraction: new Date(), + // messages: [] + // }) + // ); + // }); + + const onSubmit = (data: ChannelSettingsValidation) => { + console.log(data); + }; return ( -
-
- {channel?.index !== 0 && ( - <> - ( - <> - - - - )} - /> - - - - )} - {/* */} - - { - setPskHidden(!pskHidden); + //
+ // + // {channel?.index !== 0 && ( + // <> + // ( + // <> + // + // + // + // )} + // /> + // + // + // + // )} + // {/* */} + // + // { + // setPskHidden(!pskHidden); + // } + // }} + // // error={errors.psk?.message} + // {...register("psk")} + // /> + // ( + // <> + // + // + // + // )} + // /> + // ( + // <> + // + // + // + // )} + // /> + // + + onSubmit={onSubmit} + defaultValues={{ + enabled: [ + Protobuf.Channel_Role.SECONDARY, + Protobuf.Channel_Role.PRIMARY + ].find((role) => role === channel?.role) + ? true + : false, + ...channel?.settings, + psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)) + }} + fieldGroups={[ + { + label: "Bluetooth Settings", + description: "Settings for the Bluetooth module", + fields: [ + { + type: "toggle", + name: "enabled", + label: "Enabled", + description: "Description" + }, + { + type: "password", + name: "psk", + label: "pre-Shared Key", + description: "Description", + + disabledBy: [ + { + fieldName: "enabled" + } + ], + properties: { + // act + } + }, + { + type: "number", + name: "channelNum", + label: "Channel Number", + description: "Description", + disabledBy: [ + { + fieldName: "enabled" + } + ] + }, + { + type: "text", + name: "name", + label: "Name", + description: "Description", + disabledBy: [ + { + fieldName: "enabled" + } + ] + }, + { + type: "number", + name: "id", + label: "ID", + description: "Description", + disabledBy: [ + { + fieldName: "enabled" + } + ] + }, + { + type: "toggle", + name: "uplinkEnabled", + label: "Uplink Enabled", + description: "Description", + disabledBy: [ + { + fieldName: "enabled" + } + ] + }, + { + type: "toggle", + name: "downlinkEnabled", + label: "Downlink Enabled", + description: "Description", + disabledBy: [ + { + fieldName: "enabled" + } + ] } - }} - // error={errors.psk?.message} - {...register("psk")} - /> - ( - <> - - - - )} - /> - ( - <> - - - - )} - /> - -
+ ] + } + ]} + /> ); }; diff --git a/src/pages/Config/index.tsx b/src/pages/Config/index.tsx index 43912822..ba8ebac6 100644 --- a/src/pages/Config/index.tsx +++ b/src/pages/Config/index.tsx @@ -55,13 +55,7 @@ export const ConfigPage = (): JSX.Element => { } ]} > -
- {activeConfigSection === "device" ? ( - - ) : ( - - )} -
+ {activeConfigSection === "device" ? : } ); diff --git a/src/pages/Map.tsx b/src/pages/Map.tsx index a0c0894d..a7dfad2d 100644 --- a/src/pages/Map.tsx +++ b/src/pages/Map.tsx @@ -13,7 +13,6 @@ import { } from "lucide-react"; import { bbox, lineString } from "@turf/turf"; import { SidebarSection } from "@components/UI/Sidebar/SidebarSection.js"; -import { Button } from "@components/UI/Button.js"; import { SidebarButton } from "@components/UI/Sidebar/sidebarButton.js"; import { Protobuf } from "@meshtastic/meshtasticjs"; @@ -61,6 +60,7 @@ export const MapPage = (): JSX.Element => {