Skip to content

Commit 3c76c90

Browse files
committed
fix(web): show the correct Tailscale URL when switching to port 443
1 parent 48a2923 commit 3c76c90

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { DesktopWslState } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
33
import {
44
applyWslEnableSelection,
5+
resolveTailscaleServeBaseUrl,
56
selectQrEndpointOption,
67
togglePairingScopeSelection,
78
} from "./ConnectionsSettings.logic";
@@ -41,6 +42,26 @@ describe("togglePairingScopeSelection", () => {
4142
});
4243
});
4344

45+
describe("resolveTailscaleServeBaseUrl", () => {
46+
it.each([
47+
["https://host.tail.ts.net:441/", 443, "https://host.tail.ts.net"],
48+
["https://host.tail.ts.net:441/", 444, "https://host.tail.ts.net:444"],
49+
] as const)("previews %s on port %i", (httpBaseUrl, port, expected) => {
50+
expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, true)).toBe(expected);
51+
});
52+
53+
it("returns null without a pending endpoint", () => {
54+
expect(resolveTailscaleServeBaseUrl(null, 443, true)).toBeNull();
55+
});
56+
57+
it.each([
58+
["https://host.tail.ts.net:441/", 0, false],
59+
["not a URL", 443, true],
60+
] as const)("preserves %s when port %i cannot be previewed", (httpBaseUrl, port, isPortValid) => {
61+
expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, isPortValid)).toBe(httpBaseUrl);
62+
});
63+
});
64+
4465
const baseWslState: DesktopWslState = {
4566
enabled: false,
4667
distro: null,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ export function missingTailscaleEndpointDescription(
6767
: "Start Tailscale to set up HTTPS access through MagicDNS.";
6868
}
6969

70+
export function resolveTailscaleServeBaseUrl(
71+
endpoint: Pick<AdvertisedEndpoint, "httpBaseUrl"> | null,
72+
port: number,
73+
isPortValid: boolean,
74+
): string | null {
75+
if (!endpoint) return null;
76+
if (!isPortValid) return endpoint.httpBaseUrl;
77+
try {
78+
const url = new URL(endpoint.httpBaseUrl);
79+
url.port = String(port);
80+
return url.toString().replace(/\/$/u, "");
81+
} catch {
82+
return endpoint.httpBaseUrl;
83+
}
84+
}
85+
7086
export function isWslSettingsRowVisible(input: {
7187
readonly state: DesktopWslState | null;
7288
readonly error: string | null;

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
isWslSettingsRowVisible,
3131
missingTailscaleEndpointDescription,
3232
resolveLocalVersionStatusLabel,
33+
resolveTailscaleServeBaseUrl,
3334
} from "./ConnectionsSettings.logic";
3435
import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
3536
import { LocalEnvironmentSetting } from "./LocalEnvironmentSetting";
@@ -340,20 +341,11 @@ export function ConnectionsSettings({ view }: { readonly view: "environments" |
340341
parsedTailscaleServePort >= 1 &&
341342
parsedTailscaleServePort <= 65_535;
342343

343-
const pendingTailscaleServeBaseUrl = (() => {
344-
if (!pendingTailscaleServeEndpoint) return null;
345-
if (!isTailscaleServePortValid) return pendingTailscaleServeEndpoint.httpBaseUrl;
346-
if (parsedTailscaleServePort === DEFAULT_TAILSCALE_SERVE_PORT) {
347-
return pendingTailscaleServeEndpoint.httpBaseUrl;
348-
}
349-
try {
350-
const url = new URL(pendingTailscaleServeEndpoint.httpBaseUrl);
351-
url.port = String(parsedTailscaleServePort);
352-
return url.toString().replace(/\/$/u, "");
353-
} catch {
354-
return pendingTailscaleServeEndpoint.httpBaseUrl;
355-
}
356-
})();
344+
const pendingTailscaleServeBaseUrl = resolveTailscaleServeBaseUrl(
345+
pendingTailscaleServeEndpoint,
346+
parsedTailscaleServePort,
347+
isTailscaleServePortValid,
348+
);
357349

358350
const handleDesktopServerExposureChange = async (checked: boolean) => {
359351
if (

0 commit comments

Comments
 (0)