From a4e2e7eec198db14ae51cb163abbadd0b11b9b8f Mon Sep 17 00:00:00 2001 From: Addison Tustin Date: Sun, 7 Jul 2024 13:58:59 -0700 Subject: [PATCH] fix: move qrcode URL query-string before fragment --- src/components/Dialog/ImportDialog.tsx | 33 +++++++++++++++++++------- src/components/Dialog/QRDialog.tsx | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/components/Dialog/ImportDialog.tsx b/src/components/Dialog/ImportDialog.tsx index 348549a8..37327096 100644 --- a/src/components/Dialog/ImportDialog.tsx +++ b/src/components/Dialog/ImportDialog.tsx @@ -26,19 +26,34 @@ export const ImportDialog = ({ open, onOpenChange, }: ImportDialogProps): JSX.Element => { - const [qrCodeUrl, setQrCodeUrl] = useState(""); + const [importDialogInput, setImportDialogInput] = useState(""); const [channelSet, setChannelSet] = useState(); const [validUrl, setValidUrl] = useState(false); const { connection } = useDevice(); useEffect(() => { - const base64String = qrCodeUrl.split("e/#")[1]; - const paddedString = base64String - ?.padEnd(base64String.length + ((4 - (base64String.length % 4)) % 4), "=") - .replace(/-/g, "+") - .replace(/_/g, "/"); + // 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 "Invalid Meshtastic URL"; + } + + const encodedChannelConfig = channelsUrl.hash.substring(1); + const paddedString = encodedChannelConfig + .padEnd( + encodedChannelConfig.length + + ((4 - (encodedChannelConfig.length % 4)) % 4), + "=", + ) + .replace(/-/g, "+") + .replace(/_/g, "/"); setChannelSet( Protobuf.AppOnly.ChannelSet.fromBinary(toByteArray(paddedString)), ); @@ -47,7 +62,7 @@ export const ImportDialog = ({ setValidUrl(false); setChannelSet(undefined); } - }, [qrCodeUrl]); + }, [importDialogInput]); const apply = () => { channelSet?.settings.map((ch, index) => { @@ -87,10 +102,10 @@ export const ImportDialog = ({
{ - setQrCodeUrl(e.target.value); + setImportDialogInput(e.target.value); }} /> {validUrl && ( diff --git a/src/components/Dialog/QRDialog.tsx b/src/components/Dialog/QRDialog.tsx index 27a57a1d..9a8853fa 100644 --- a/src/components/Dialog/QRDialog.tsx +++ b/src/components/Dialog/QRDialog.tsx @@ -51,7 +51,7 @@ export const QRDialog = ({ .replace(/\//g, "_"); setQrCodeUrl( - `https://meshtastic.org/e/#${base64}${qrCodeAdd ? "?add=true" : ""}`, + `https://meshtastic.org/e/${qrCodeAdd ? "?add=true" : ""}#${base64}`, ); }, [allChannels, selectedChannels, qrCodeAdd, loraConfig]);