import type React from "react"; import { useEffect, useState } from "react"; import { fromByteArray } from "base64-js"; import { Checkbox, ClipboardIcon, Dialog, FormField, IconButton, majorScale, Pane, TextInputField, Tooltip, } from "evergreen-ui"; import { QRCode } from "react-qrcode-logo"; import { Protobuf } from "@meshtastic/meshtasticjs"; export interface QRDialogProps { isOpen: boolean; close: () => void; loraConfig?: Protobuf.Config_LoRaConfig; channels: Protobuf.Channel[]; } export const QRDialog = ({ isOpen, close, loraConfig, channels, }: QRDialogProps): JSX.Element => { const [selectedChannels, setSelectedChannels] = useState([]); const [QRCodeURL, setQRCodeURL] = useState(""); useEffect(() => { const channelsToEncode = channels .filter((channel) => selectedChannels.includes(channel.index)) .map((channel) => channel.settings) .filter((ch): ch is Protobuf.ChannelSettings => !!ch); const encoded = Protobuf.ChannelSet.toBinary( Protobuf.ChannelSet.create({ loraConfig, settings: channelsToEncode, }) ); const base64 = fromByteArray(encoded) .replace(/=/g, "") .replace(/\+/g, "-") .replace(/\//g, "_"); setQRCodeURL(`https://www.meshtastic.org/e/#${base64}`); }, [channels, selectedChannels, loraConfig]); return ( {channels.map((channel) => ( { if (selectedChannels.includes(channel.index)) { setSelectedChannels( selectedChannels.filter((c) => c !== channel.index) ); } else { setSelectedChannels([...selectedChannels, channel.index]); } }} /> ))} ); };