Skip to content

Commit b8a544f

Browse files
committed
fix(web): prompt links work for encoded thread IDs
1 parent ade4730 commit b8a544f

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎apps/web/src/threadRoutes.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import {
2+
createMemoryHistory,
3+
createRootRoute,
4+
createRoute,
5+
createRouter,
6+
} from "@tanstack/react-router";
17
import { describe, expect, it } from "vite-plus/test";
28
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
39
import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
@@ -25,6 +31,43 @@ describe("threadRoutes", () => {
2531
expect(isThreadRoutePathname("/env-2/thread-1", ref)).toBe(false);
2632
expect(isThreadRoutePathname("/draft/thread-1", ref)).toBe(false);
2733
expect(isThreadRoutePathname("/env-1/thread-1/extra", ref)).toBe(false);
34+
35+
const encodedRef = scopeThreadRef("env space" as never, ThreadId.make("mcp:example/part"));
36+
expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart", encodedRef)).toBe(true);
37+
expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/", encodedRef)).toBe(true);
38+
expect(isThreadRoutePathname("/env%20space/mcp%3Aexample/part", encodedRef)).toBe(false);
39+
expect(isThreadRoutePathname("/env%20space/mcp%253Aexample%2Fpart", encodedRef)).toBe(false);
40+
expect(isThreadRoutePathname("/env%20space/mcp%ZZexample", encodedRef)).toBe(false);
41+
expect(isThreadRoutePathname("/env%20space/mcp%3Aexample%2Fpart/extra", encodedRef)).toBe(
42+
false,
43+
);
44+
expect(
45+
isThreadRoutePathname(
46+
"/env-1/mcp%253Aexample",
47+
scopeThreadRef("env-1" as never, ThreadId.make("mcp%3Aexample")),
48+
),
49+
).toBe(true);
50+
});
51+
52+
it("matches encoded MCP thread IDs from router navigation", async () => {
53+
const root = createRootRoute();
54+
const thread = createRoute({ getParentRoute: () => root, path: "$environmentId/$threadId" });
55+
const newThread = createRoute({ getParentRoute: () => root, path: "new" });
56+
const router = createRouter({
57+
routeTree: root.addChildren([thread, newThread]),
58+
history: createMemoryHistory({ initialEntries: ["/new"] }),
59+
});
60+
await router.load();
61+
const ref = scopeThreadRef("local" as never, ThreadId.make("mcp:example"));
62+
await router.navigate({
63+
to: "/$environmentId/$threadId",
64+
params: { environmentId: ref.environmentId, threadId: ref.threadId },
65+
});
66+
expect(router.state.location.pathname).toBe("/local/mcp%3Aexample");
67+
expect(router.state.matches.at(-1)?.params).toEqual(ref);
68+
expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(true);
69+
await router.navigate({ to: "/new" });
70+
expect(isThreadRoutePathname(router.state.location.pathname, ref)).toBe(false);
2871
});
2972

3073
it("resolves the backing thread while a draft route is being promoted", () => {

‎apps/web/src/threadRoutes.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ export function buildThreadRouteParams(ref: ScopedThreadRef): {
6565
};
6666
}
6767

68-
/** Whether a (decoded) location pathname is `ref`'s own thread route, `/$environmentId/$threadId`. */
68+
/** The router keeps reserved characters encoded in location.pathname. */
6969
export function isThreadRoutePathname(pathname: string, ref: ScopedThreadRef): boolean {
70-
const { environmentId, threadId } = buildThreadRouteParams(ref);
71-
return pathname.replace(/\/$/, "") === `/${environmentId}/${threadId}`;
70+
const parts = pathname.replace(/\/$/, "").split("/");
71+
if (parts.length !== 3 || parts[0] !== "") return false;
72+
try {
73+
return (
74+
decodeURIComponent(parts[1]!) === ref.environmentId &&
75+
decodeURIComponent(parts[2]!) === ref.threadId
76+
);
77+
} catch {
78+
return false;
79+
}
7280
}
7381

7482
export function buildDraftThreadRouteParams(draftId: DraftId): {

0 commit comments

Comments
 (0)