Browse Source

fix: move qrcode URL query-string before fragment

pull/260/head
Addison Tustin 2 years ago
parent
commit
a4e2e7eec1
No known key found for this signature in database GPG Key ID: 5522F340E15FB447
  1. 33
      src/components/Dialog/ImportDialog.tsx
  2. 2
      src/components/Dialog/QRDialog.tsx

33
src/components/Dialog/ImportDialog.tsx

@ -26,19 +26,34 @@ export const ImportDialog = ({
open, open,
onOpenChange, onOpenChange,
}: ImportDialogProps): JSX.Element => { }: ImportDialogProps): JSX.Element => {
const [qrCodeUrl, setQrCodeUrl] = useState<string>(""); const [importDialogInput, setImportDialogInput] = useState<string>("");
const [channelSet, setChannelSet] = useState<Protobuf.AppOnly.ChannelSet>(); const [channelSet, setChannelSet] = useState<Protobuf.AppOnly.ChannelSet>();
const [validUrl, setValidUrl] = useState<boolean>(false); const [validUrl, setValidUrl] = useState<boolean>(false);
const { connection } = useDevice(); const { connection } = useDevice();
useEffect(() => { useEffect(() => {
const base64String = qrCodeUrl.split("e/#")[1]; // the channel information is contained in the URL's fragment, which will be present after a
const paddedString = base64String // non-URL encoded `#`.
?.padEnd(base64String.length + ((4 - (base64String.length % 4)) % 4), "=")
.replace(/-/g, "+")
.replace(/_/g, "/");
try { 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( setChannelSet(
Protobuf.AppOnly.ChannelSet.fromBinary(toByteArray(paddedString)), Protobuf.AppOnly.ChannelSet.fromBinary(toByteArray(paddedString)),
); );
@ -47,7 +62,7 @@ export const ImportDialog = ({
setValidUrl(false); setValidUrl(false);
setChannelSet(undefined); setChannelSet(undefined);
} }
}, [qrCodeUrl]); }, [importDialogInput]);
const apply = () => { const apply = () => {
channelSet?.settings.map((ch, index) => { channelSet?.settings.map((ch, index) => {
@ -87,10 +102,10 @@ export const ImportDialog = ({
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
<Label>Channel Set/QR Code URL</Label> <Label>Channel Set/QR Code URL</Label>
<Input <Input
value={qrCodeUrl} value={importDialogInput}
suffix={validUrl ? "✅" : "❌"} suffix={validUrl ? "✅" : "❌"}
onChange={(e) => { onChange={(e) => {
setQrCodeUrl(e.target.value); setImportDialogInput(e.target.value);
}} }}
/> />
{validUrl && ( {validUrl && (

2
src/components/Dialog/QRDialog.tsx

@ -51,7 +51,7 @@ export const QRDialog = ({
.replace(/\//g, "_"); .replace(/\//g, "_");
setQrCodeUrl( setQrCodeUrl(
`https://meshtastic.org/e/#${base64}${qrCodeAdd ? "?add=true" : ""}`, `https://meshtastic.org/e/${qrCodeAdd ? "?add=true" : ""}#${base64}`,
); );
}, [allChannels, selectedChannels, qrCodeAdd, loraConfig]); }, [allChannels, selectedChannels, qrCodeAdd, loraConfig]);

Loading…
Cancel
Save