Skip to content

Commit fff6a5b

Browse files
t3dotggclaude
andauthored
feat(web): make pairing QR codes actually scannable, with endpoint choice (pingdotgg#5360)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent da6e1a9 commit fff6a5b

3 files changed

Lines changed: 315 additions & 189 deletions

File tree

‎apps/web/src/components/settings/ConnectionsSettings.logic.test.ts‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import type { DesktopWslState } from "@t3tools/contracts";
1+
import type { AdvertisedEndpoint, DesktopWslState } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
3-
import { applyWslEnableSelection } from "./ConnectionsSettings.logic";
3+
import {
4+
applyWslEnableSelection,
5+
isQrShareableEndpoint,
6+
selectQrEndpointOption,
7+
} from "./ConnectionsSettings.logic";
48

59
const baseWslState: DesktopWslState = {
610
enabled: false,
@@ -73,3 +77,89 @@ describe("applyWslEnableSelection", () => {
7377
expect(state).toMatchObject({ enabled: true, wslOnly: true });
7478
});
7579
});
80+
81+
function makeEndpoint(overrides: Partial<AdvertisedEndpoint>): AdvertisedEndpoint {
82+
return {
83+
id: "desktop-lan:http://192.168.1.42:4780",
84+
label: "Local network",
85+
provider: { id: "desktop-core", label: "Desktop", kind: "core", isAddon: false },
86+
httpBaseUrl: "http://192.168.1.42:4780",
87+
wsBaseUrl: "ws://192.168.1.42:4780",
88+
reachability: "lan",
89+
compatibility: { hostedHttpsApp: "unknown", desktopApp: "compatible" },
90+
source: "desktop-core",
91+
status: "available",
92+
...overrides,
93+
};
94+
}
95+
96+
describe("isQrShareableEndpoint", () => {
97+
it("excludes loopback endpoints so a scanned phone never dials itself", () => {
98+
expect(
99+
isQrShareableEndpoint(
100+
makeEndpoint({
101+
id: "desktop-loopback:4780",
102+
reachability: "loopback",
103+
httpBaseUrl: "http://127.0.0.1:4780",
104+
}),
105+
),
106+
).toBe(false);
107+
});
108+
109+
it("excludes unavailable endpoints and keeps reachable ones", () => {
110+
expect(isQrShareableEndpoint(makeEndpoint({ status: "unavailable" }))).toBe(false);
111+
expect(isQrShareableEndpoint(makeEndpoint({}))).toBe(true);
112+
expect(
113+
isQrShareableEndpoint(makeEndpoint({ reachability: "private-network", status: "unknown" })),
114+
).toBe(true);
115+
});
116+
});
117+
118+
describe("selectQrEndpointOption", () => {
119+
const options = [
120+
{
121+
id: "desktop-loopback:4780",
122+
preferenceKey: "desktop-core:loopback:http",
123+
qrShareable: false,
124+
},
125+
{
126+
id: "tailscale-ip:http://100.84.12.7:4780",
127+
preferenceKey: "tailscale:ip:http",
128+
qrShareable: true,
129+
},
130+
{
131+
id: "tailscale-ip:http://100.84.12.8:4780",
132+
preferenceKey: "tailscale:ip:http",
133+
qrShareable: true,
134+
},
135+
{
136+
id: "desktop-lan:http://192.168.1.42:4780",
137+
preferenceKey: "desktop-core:lan:http",
138+
qrShareable: true,
139+
},
140+
];
141+
142+
it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
143+
expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
144+
"tailscale-ip:http://100.84.12.8:4780",
145+
);
146+
});
147+
148+
it("falls back to the saved default preference key when nothing is selected", () => {
149+
expect(selectQrEndpointOption(options, null, "desktop-core:lan:http")?.id).toBe(
150+
"desktop-lan:http://192.168.1.42:4780",
151+
);
152+
});
153+
154+
it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
155+
expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
156+
"tailscale-ip:http://100.84.12.7:4780",
157+
);
158+
});
159+
160+
it("returns the first option when nothing is QR-shareable, and null when empty", () => {
161+
const loopbackOnly = options.slice(0, 1);
162+
expect(selectQrEndpointOption(loopbackOnly, null, null)?.id).toBe("desktop-loopback:4780");
163+
expect(selectQrEndpointOption([], "anything", "anything")).toBeNull();
164+
});
165+
});

‎apps/web/src/components/settings/ConnectionsSettings.logic.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
1-
import type { DesktopBridge, DesktopWslState } from "@t3tools/contracts";
1+
import type { AdvertisedEndpoint, DesktopBridge, DesktopWslState } from "@t3tools/contracts";
22

33
type WslEnableBridge = Pick<DesktopBridge, "setWslBackendEnabled" | "setWslDistro" | "setWslOnly">;
44

5+
/**
6+
* A QR code encoding a loopback URL makes the scanning device dial itself, so
7+
* loopback endpoints stay copyable from the endpoint menu but are never
8+
* offered as QR targets.
9+
*/
10+
export function isQrShareableEndpoint(endpoint: AdvertisedEndpoint): boolean {
11+
return endpoint.status !== "unavailable" && endpoint.reachability !== "loopback";
12+
}
13+
14+
export type QrEndpointOption = {
15+
/** Unique per endpoint instance (AdvertisedEndpoint.id); safe as a React key. */
16+
readonly id: string;
17+
/**
18+
* Stable per endpoint *type* (endpointDefaultPreferenceKey). Multiple
19+
* endpoints can share one, so it is only used to match the saved default.
20+
*/
21+
readonly preferenceKey: string;
22+
/** False for endpoints that stay copyable but must never render as a QR. */
23+
readonly qrShareable: boolean;
24+
};
25+
26+
/**
27+
* Resolves which endpoint the share panel shows: the user's explicit pick,
28+
* else the saved default endpoint, else the first QR-shareable option (so the
29+
* panel never opens on a loopback QR), else the first option. A stale
30+
* selectedId (endpoint disappeared) falls back rather than blanking the panel.
31+
*/
32+
export function selectQrEndpointOption<T extends QrEndpointOption>(
33+
options: ReadonlyArray<T>,
34+
selectedId: string | null,
35+
defaultPreferenceKey: string | null,
36+
): T | null {
37+
return (
38+
(selectedId !== null ? options.find((option) => option.id === selectedId) : undefined) ??
39+
(defaultPreferenceKey !== null
40+
? options.find((option) => option.preferenceKey === defaultPreferenceKey)
41+
: undefined) ??
42+
options.find((option) => option.qrShareable) ??
43+
options[0] ??
44+
null
45+
);
46+
}
47+
548
export async function applyWslEnableSelection(input: {
649
readonly bridge: WslEnableBridge;
750
readonly mode: "both" | "wsl-only";

0 commit comments

Comments
 (0)