import type React from "react"; import { useEffect } from "react"; import { Controller, useForm, useWatch } from "react-hook-form"; import { toast } from "react-hot-toast"; import { FormSection } from "@app/components/form/FormSection.js"; import { Input } from "@app/components/form/Input.js"; import { Select } from "@app/components/form/Select.js"; import { Toggle } from "@app/components/form/Toggle.js"; import { renderOptions } from "@app/core/utils/selectEnumOptions.js"; import { NetworkValidation } from "@app/validation/config/network.js"; import { Form } from "@components/form/Form"; import { useDevice } from "@core/providers/useDevice.js"; import { ErrorMessage } from "@hookform/error-message"; import { classValidatorResolver } from "@hookform/resolvers/class-validator"; import { Protobuf } from "@meshtastic/meshtasticjs"; export const Network = (): JSX.Element => { const { config, connection, setConfig } = useDevice(); const { register, handleSubmit, formState: { errors, isDirty }, control, reset } = useForm({ defaultValues: config.network, resolver: classValidatorResolver(NetworkValidation) }); const wifiEnabled = useWatch({ control, name: "wifiEnabled", defaultValue: false }); const ethEnabled = useWatch({ control, name: "ethEnabled", defaultValue: false }); const ethMode = useWatch({ control, name: "ethMode", defaultValue: Protobuf.Config_NetworkConfig_EthMode.DHCP }); useEffect(() => { reset(config.network); }, [reset, config.network]); const onSubmit = handleSubmit((data) => { if (connection) { void toast.promise( connection .setConfig({ config: { payloadVariant: { oneofKind: "network", network: Protobuf.Config_NetworkConfig.create(data) } } }) .then(() => setConfig({ payloadVariant: { oneofKind: "network", network: data } }) ), { loading: "Saving...", success: "Saved Network Config, Restarting Node", error: "No response received" } ); } }); return (
reset(config.network)} dirty={isDirty} onSubmit={onSubmit} > ( )} /> ( )} /> {ethMode === Protobuf.Config_NetworkConfig_EthMode.STATIC && ( <> )} ); };