import { create, fromBinary } from "@bufbuild/protobuf"; import { Button } from "@components/UI/Button.tsx"; import { Checkbox } from "@components/UI/Checkbox.tsx"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@components/UI/Dialog.tsx"; import { Input } from "@components/UI/Input.tsx"; import { Label } from "@components/UI/Label.tsx"; import { Switch } from "@components/UI/Switch.tsx"; import { useDevice } from "@core/stores/deviceStore.ts"; import { Protobuf } from "@meshtastic/core"; import { toByteArray } from "base64-js"; import { useEffect, useState } from "react"; export interface ImportDialogProps { open: boolean; onOpenChange: (open: boolean) => void; loraConfig?: Protobuf.Config.Config_LoRaConfig; } export const ImportDialog = ({ open, onOpenChange, }: ImportDialogProps) => { const [importDialogInput, setImportDialogInput] = useState(""); const [channelSet, setChannelSet] = useState(); const [validUrl, setValidUrl] = useState(false); const { connection } = useDevice(); useEffect(() => { // the channel information is contained in the URL's fragment, which will be present after a // non-URL encoded `#`. try { const channelsUrl = new URL(importDialogInput); if ( (channelsUrl.hostname !== "meshtastic.org" && channelsUrl.pathname !== "/e/") || !channelsUrl.hash ) { throw "Invalid Meshtastic URL"; } const encodedChannelConfig = channelsUrl.hash.substring(1); const paddedString = encodedChannelConfig .padEnd( encodedChannelConfig.length + ((4 - (encodedChannelConfig.length % 4)) % 4), "=", ) .replace(/-/g, "+") .replace(/_/g, "/"); setChannelSet( fromBinary( Protobuf.AppOnly.ChannelSetSchema, toByteArray(paddedString), ), ); setValidUrl(true); } catch (_error) { setValidUrl(false); setChannelSet(undefined); } }, [importDialogInput]); const apply = () => { channelSet?.settings.map((ch: unknown, index: number) => { connection?.setChannel( create(Protobuf.Channel.ChannelSchema, { index, role: index === 0 ? Protobuf.Channel.Channel_Role.PRIMARY : Protobuf.Channel.Channel_Role.SECONDARY, settings: ch, }), ); }); if (channelSet?.loraConfig) { connection?.setConfig( create(Protobuf.Config.ConfigSchema, { payloadVariant: { case: "lora", value: channelSet.loraConfig, }, }), ); } }; return ( Import Channel Set The current LoRa configuration will be overridden.
{ setImportDialogInput(e.target.value); }} /> {validUrl && (
{ /* */ }
{ /* */ } Channels:
{channelSet?.settings.map((channel) => (
))}
)}
); };