import { useEffect, useState } from "react"; import { toByteArray } from "base64-js"; import { Checkbox } from "@components/form/Checkbox.js"; import { Input } from "@components/form/Input.js"; import { Dialog } from "@components/generic/Dialog.js"; import { Protobuf } from "@meshtastic/meshtasticjs"; import { Select } from "@components/form/Select.js"; import { renderOptions } from "@core/utils/selectEnumOptions.js"; import { Toggle } from "@components/form/Toggle.js"; import { Button } from "@components/form/Button.js"; import { useDevice } from "@core/providers/useDevice.js"; export interface ImportDialogProps { isOpen: boolean; close: () => void; loraConfig?: Protobuf.Config_LoRaConfig; channels: Protobuf.Channel[]; } export const ImportDialog = ({ isOpen, close }: ImportDialogProps): JSX.Element => { const [QRCodeURL, setQRCodeURL] = useState(""); const [channelSet, setChannelSet] = useState(); const [validURL, setValidURL] = useState(false); const { connection } = useDevice(); useEffect(() => { const base64String = QRCodeURL.split("e/#")[1] ?.replace(/-/g, "+") .replace(/_/g, "/") .padEnd(QRCodeURL.length + ((4 - (QRCodeURL.length % 4)) % 4), "="); try { setChannelSet(Protobuf.ChannelSet.fromBinary(toByteArray(base64String))); setValidURL(true); } catch (error) { setValidURL(false); setChannelSet(undefined); } }, [QRCodeURL]); const apply = () => { channelSet?.settings.map((ch, index) => { connection?.setChannel( new Protobuf.Channel({ index, role: index === 0 ? Protobuf.Channel_Role.PRIMARY : Protobuf.Channel_Role.SECONDARY, settings: ch }) ); }); if (channelSet?.loraConfig) { connection?.setConfig( new Protobuf.Config({ payloadVariant: { case: "lora", value: channelSet.loraConfig } }) ); } }; return (
{ setQRCodeURL(e.target.value); }} /> {validURL && (
Channels:
{channelSet?.settings.map((channel, index) => ( ))}
)}
); };