import { create, toBinary } from "@bufbuild/protobuf"; 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 { Protobuf, type Types } from "@meshtastic/core"; import { fromByteArray } from "base64-js"; import { useEffect, useMemo, useState } from "react"; import { QRCode } from "react-qrcode-logo"; import { useTranslation } from "react-i18next"; export interface QRDialogProps { open: boolean; onOpenChange: (open: boolean) => void; loraConfig?: Protobuf.Config.Config_LoRaConfig; channels: Map; } export const QRDialog = ({ open, onOpenChange, loraConfig, channels, }: QRDialogProps) => { const { t } = useTranslation("dialog"); 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 = create( Protobuf.AppOnly.ChannelSetSchema, create(Protobuf.AppOnly.ChannelSetSchema, { loraConfig, settings: channelsToEncode, }), ); const base64 = fromByteArray( toBinary(Protobuf.AppOnly.ChannelSetSchema, encoded), ) .replace(/=/g, "") .replace(/\+/g, "-") .replace(/\//g, "_"); setQrCodeUrl( `https://meshtastic.org/e/${qrCodeAdd ? "?add=true" : ""}#${base64}`, ); }, [allChannels, selectedChannels, qrCodeAdd, loraConfig]); return ( {t("qr.title")} {t("qr.description")}
{allChannels.map((channel) => (
{ if (selectedChannels.includes(channel.index)) { setSelectedChannels( selectedChannels.filter((c) => c !== channel.index ), ); } else { setSelectedChannels([ ...selectedChannels, channel.index, ]); } }} />
))}
); };