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 { Protobuf, type Types } from "@meshtastic/js"; import { fromByteArray } from "base64-js"; import { ClipboardIcon } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { QRCode } from "react-qrcode-logo"; export interface QRDialogProps { open: boolean; onOpenChange: (open: boolean) => void; loraConfig?: Protobuf.Config.Config_LoRaConfig; channels: Map; } export const QRDialog = ({ open, onOpenChange, loraConfig, channels, }: QRDialogProps): JSX.Element => { const [selectedChannels, setSelectedChannels] = useState([0]); const [qrCodeUrl, setQrCodeUrl] = useState(""); const [qrCodeAdd, setQrCodeAdd] = useState(); const allChannels = useMemo(() => Array.from(channels.values()), [channels]); useEffect(() => { const channelsToEncode = allChannels .filter((ch) => selectedChannels.includes(ch.index)) .map((channel) => channel.settings) .filter((ch): ch is Protobuf.Channel.ChannelSettings => !!ch); const encoded = new Protobuf.AppOnly.ChannelSet( new Protobuf.AppOnly.ChannelSet({ loraConfig, settings: channelsToEncode, }), ); const base64 = fromByteArray(encoded.toBinary()) .replace(/=/g, "") .replace(/\+/g, "-") .replace(/\//g, "_"); setQrCodeUrl( `https://meshtastic.org/e/${qrCodeAdd ? "?add=true" : ""}#${base64}`, ); }, [allChannels, selectedChannels, qrCodeAdd, loraConfig]); return ( Generate QR Code The current LoRa configuration will also be shared.
{allChannels.map((channel) => (
{ if (selectedChannels.includes(channel.index)) { setSelectedChannels( selectedChannels.filter((c) => c !== channel.index), ); } else { setSelectedChannels([ ...selectedChannels, channel.index, ]); } }} />
))}
); };