import { useEffect, useState } from "react"; import { fromByteArray } from "base64-js"; import { QRCode } from "react-qrcode-logo"; import { Checkbox } from "@components/UI/Checkbox.js"; import { Input } from "@components/UI/Input.js"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@components/UI/Dialog.js"; import { ClipboardIcon } from "lucide-react"; import { Protobuf, Types } from "@meshtastic/meshtasticjs"; import { Label } from "@components/UI/Label.js"; export interface QRDialogProps { open: boolean; onOpenChange: (open: boolean) => void; loraConfig?: Protobuf.Config_LoRaConfig; channels: Map; } export const QRDialog = ({ open, onOpenChange, loraConfig, channels }: QRDialogProps): JSX.Element => { const [selectedChannels, setSelectedChannels] = useState([0]); const [QRCodeURL, setQRCodeURL] = useState(""); const filteredChannels = Array.from(channels.values()).filter((channel) => selectedChannels.includes(channel.index) ); useEffect(() => { const channelsToEncode = filteredChannels .map((channel) => channel.settings) .filter((ch): ch is Protobuf.ChannelSettings => !!ch); const encoded = new Protobuf.ChannelSet( new Protobuf.ChannelSet({ loraConfig, settings: channelsToEncode }) ); const base64 = fromByteArray(encoded.toBinary()) .replace(/=/g, "") .replace(/\+/g, "-") .replace(/\//g, "_"); setQRCodeURL(`https://meshtastic.org/e/#${base64}`); }, [channels, selectedChannels, loraConfig]); return ( Generate QR Code The current LoRa configuration will also be shared.
{filteredChannels.map((channel) => (
{ if (selectedChannels.includes(channel.index)) { setSelectedChannels( selectedChannels.filter((c) => c !== channel.index) ); } else { setSelectedChannels([ ...selectedChannels, channel.index ]); } }} />
))}
); };