import { create, fromBinary } from "@bufbuild/protobuf"; import { Button } from "@components/UI/Button.tsx"; import { Checkbox } from "../UI/Checkbox/index.tsx"; import { Dialog, DialogClose, 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"; import { useTranslation } from "react-i18next"; export interface ImportDialogProps { open: boolean; onOpenChange: (open: boolean) => void; loraConfig?: Protobuf.Config.Config_LoRaConfig; } export const ImportDialog = ({ open, onOpenChange, }: ImportDialogProps) => { const { t } = useTranslation("dialog"); 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 t("import.error.invalidUrl"); } 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: Protobuf.Channel.ChannelSettings, 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 ( {t("import.title")} {t("import.description")}
{ setImportDialogInput(e.target.value); }} /> {validUrl && (
{ /* */ }
{ /* */ } {t("import.channels")}
{channelSet?.settings.map((channel) => (
))}
)}
); };