Skip to content

Commit 9456187

Browse files
committed
fix: preserve thread link punctuation and external fallback
1 parent 17222bc commit 9456187

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎apps/web/src/components/desktop/DesktopWebLinkCoordinator.browser.test.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const state = vi.hoisted(() => ({
1010
projects: [] as EnvironmentProject[],
1111
routeParams: {} as Record<string, string>,
1212
preview: vi.fn(),
13+
openExternal: vi.fn(async (_url: string) => true),
1314
openLink: vi.fn(async () => undefined),
1415
navigate: vi.fn(async (): Promise<void> => undefined),
1516
openScratchProject: vi.fn(async () => null),
@@ -172,6 +173,7 @@ async function mount(...urls: string[]) {
172173
stack.use(
173174
stubGlobal("desktopBridge", {
174175
getClientSettings: async () => null,
176+
openExternal: state.openExternal,
175177
webLinks: {
176178
onOpen: (listener: (url: string) => void) => {
177179
deliver = listener;
@@ -375,3 +377,17 @@ it.each([false, true])(
375377
expect(state.openLink).not.toHaveBeenCalled();
376378
},
377379
);
380+
381+
it.each(["https://app.t3.codes/unknown/thread", "t3code://threads/unknown/thread"])(
382+
"opens an uncatalogued thread in the external browser: %s",
383+
async (url) => {
384+
setup();
385+
await using _coordinator = await mount(url);
386+
await vi.waitFor(() =>
387+
expect(state.openExternal).toHaveBeenCalledWith("https://app.t3.codes/unknown/thread"),
388+
);
389+
expect(state.openScratchProject).not.toHaveBeenCalled();
390+
expect(state.preview).not.toHaveBeenCalled();
391+
expect(state.navigate).not.toHaveBeenCalled();
392+
},
393+
);

‎apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
77
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
88
import { parseThreadDeepLink, threadDeepLink } from "@t3tools/shared/threadDeepLinks";
99
import { promptLinkAppPath } from "@t3tools/shared/promptLinks";
10+
import { readLocalApi } from "../../localApi";
1011
import { readPairingAppLink, resolveRemotePairingTarget } from "@t3tools/shared/remote";
1112
import { useRouter } from "@tanstack/react-router";
1213
import * as Effect from "effect/Effect";
@@ -225,7 +226,7 @@ export function DesktopWebLinkCoordinator() {
225226
return;
226227
}
227228
if (bareThread !== null) {
228-
await openInBrowser(threadDeepLink(bareThread));
229+
await readLocalApi()?.shell.openExternal(threadDeepLink(bareThread));
229230
return;
230231
}
231232
const routeParams = router.state.matches.at(-1)?.params ?? {};

‎packages/shared/src/threadDeepLinks.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import {
99
const ref = { environmentId: EnvironmentId.make("env-1"), threadId: ThreadId.make("thread:1%2F") };
1010

1111
describe("public thread links", () => {
12-
it("round-trips encoded ids without losing literal escapes", () => {
13-
expect(parseThreadDeepLink(threadDeepLink(ref))).toEqual(ref);
14-
expect(parseThreadDeepLink("t3code://threads/env-1/thread%3A1%252F")).toEqual(ref);
15-
});
12+
it.each(["thread:1%2F", "thread.", "thread!", "thread)", "thread'", "thread("])(
13+
"round-trips copied IDs through parsing and paste: %s",
14+
(id) => {
15+
const reference = { environmentId: EnvironmentId.make("env!"), threadId: ThreadId.make(id) };
16+
const link = threadDeepLink(reference);
17+
expect(parseThreadDeepLink(link)).toEqual(reference);
18+
expect(normalizeThreadClipboard(link, null).fragment?.records).toEqual([
19+
threadContextRecord(reference, reference.threadId),
20+
]);
21+
expect(parseThreadDeepLink("t3code://threads/env-1/thread%3A1%252F")).toEqual(ref);
22+
},
23+
);
1624
it.each([
1725
"https://evil.test/env/thread",
1826
"https://app.t3.codes/settings/general",

‎packages/shared/src/threadDeepLinks.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import { DEFAULT_HOSTED_APP_URL } from "./connectAuth.ts";
66
const decodeEnvironmentId = Schema.decodeOption(EnvironmentId);
77
const decodeThreadId = Schema.decodeOption(ThreadId);
88

9+
// Keep ID punctuation distinct from prose delimiters when a plain URL is pasted.
10+
function encodeDeepLinkSegment(value: string): string {
11+
return encodeURIComponent(value).replace(
12+
/[.!'()]/g,
13+
(character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
14+
);
15+
}
16+
917
export function threadDeepLink(ref: ScopedThreadRef): string {
1018
return new URL(
11-
`/${encodeURIComponent(ref.environmentId)}/${encodeURIComponent(ref.threadId)}`,
19+
`/${encodeDeepLinkSegment(ref.environmentId)}/${encodeDeepLinkSegment(ref.threadId)}`,
1220
DEFAULT_HOSTED_APP_URL,
1321
).href;
1422
}

0 commit comments

Comments
 (0)