Skip to content

Commit 5285b28

Browse files
committed
fix: load dynamic deep links once
1 parent 9c3503a commit 5285b28

2 files changed

Lines changed: 221 additions & 8 deletions

File tree

‎ui/src/app-route-paths.test.ts‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
pathForMemoryTab,
99
pathForAgentPanel,
1010
pathForPluginsHubTab,
11+
pathForWorkboardBoard,
1112
pluginsHubTabFromPath,
1213
routeIdFromPath,
14+
type RouteId,
1315
type MemoryRouteTab,
1416
type PluginsHubRouteTab,
1517
} from "./app-route-paths.ts";
@@ -27,6 +29,167 @@ const AGENT_PANEL_CASES = [
2729
"memory",
2830
] as const satisfies readonly AgentsPanel[];
2931

32+
const DYNAMIC_STARTUP_CASES = [
33+
{
34+
label: "agent panel",
35+
routeId: "agents",
36+
location: {
37+
pathname: pathForAgentPanel("team.writer", "tools"),
38+
search: "?probe=1",
39+
hash: "#catalog",
40+
},
41+
},
42+
{
43+
label: "chat session",
44+
routeId: "chat",
45+
location: {
46+
pathname: "/chat/main/01JSESSIONA",
47+
search: "?probe=1",
48+
hash: "#message",
49+
},
50+
},
51+
{
52+
label: "dashboard session",
53+
routeId: "dashboard",
54+
location: {
55+
pathname: "/dashboard/main/01JSESSIONA",
56+
search: "?probe=1",
57+
hash: "#dashboard",
58+
},
59+
},
60+
{
61+
label: "workboard board",
62+
routeId: "workboard",
63+
location: {
64+
pathname: pathForWorkboardBoard("ops.v2"),
65+
search: "?agent=main",
66+
hash: "#queue",
67+
},
68+
},
69+
{
70+
label: "Memory tab",
71+
routeId: "memory",
72+
location: {
73+
pathname: pathForMemoryTab("settings"),
74+
search: "?probe=1",
75+
hash: "#memory-backend",
76+
},
77+
},
78+
{
79+
label: "Plugins tab",
80+
routeId: "plugins",
81+
location: {
82+
pathname: pathForPluginsHubTab("discover"),
83+
search: "?query=calendar",
84+
hash: "#featured",
85+
},
86+
},
87+
] as const satisfies readonly {
88+
label: string;
89+
routeId: RouteId;
90+
location: RouteLocation;
91+
}[];
92+
93+
describe("Dynamic route startup bridge", () => {
94+
it.each(DYNAMIC_STARTUP_CASES)(
95+
"loads the $label once while publishing its real location",
96+
async ({ routeId, location: initialLocation }) => {
97+
let location: RouteLocation = { ...initialLocation };
98+
const history: RouterHistory = {
99+
location: () => location,
100+
push: vi.fn((next: RouteLocation) => {
101+
location = next;
102+
}),
103+
replace: vi.fn((next: RouteLocation) => {
104+
location = next;
105+
}),
106+
listen: () => () => undefined,
107+
};
108+
const router = createApplicationRouter();
109+
const route = router.getRoute(routeId);
110+
if (!route) {
111+
throw new Error(`Route missing: ${routeId}`);
112+
}
113+
const loader = vi.fn(async () => ({ routeId }));
114+
const originalLoader = route.loader;
115+
const originalComponent = route.component;
116+
try {
117+
route.loader = loader;
118+
route.component = async () => ({ render: () => null });
119+
120+
await startApplicationRouter(router, history, "", {
121+
basePath: "",
122+
} as unknown as ApplicationContext);
123+
124+
expect(loader).toHaveBeenCalledOnce();
125+
expect(router.getState().location).toEqual(initialLocation);
126+
expect(router.getState().matches[0]?.location).toEqual(initialLocation);
127+
} finally {
128+
router.stop();
129+
route.loader = originalLoader;
130+
route.component = originalComponent;
131+
}
132+
},
133+
);
134+
135+
it("loads a later dynamic history destination exactly once", async () => {
136+
let location: RouteLocation = {
137+
pathname: "/chat/main/01JSESSIONA",
138+
search: "",
139+
hash: "#first",
140+
};
141+
let historyListener: ((next: RouteLocation) => void) | undefined;
142+
const history: RouterHistory = {
143+
location: () => location,
144+
push: vi.fn((next: RouteLocation) => {
145+
location = next;
146+
}),
147+
replace: vi.fn((next: RouteLocation) => {
148+
location = next;
149+
}),
150+
listen: (listener) => {
151+
historyListener = listener;
152+
return () => {
153+
historyListener = undefined;
154+
};
155+
},
156+
};
157+
const router = createApplicationRouter();
158+
const route = router.getRoute("chat");
159+
if (!route) {
160+
throw new Error("Chat route missing");
161+
}
162+
const loader = vi.fn(async () => ({}));
163+
const originalLoader = route.loader;
164+
const originalComponent = route.component;
165+
try {
166+
route.loader = loader;
167+
route.component = async () => ({ render: () => null });
168+
169+
await startApplicationRouter(router, history, "", {
170+
basePath: "",
171+
} as unknown as ApplicationContext);
172+
expect(loader).toHaveBeenCalledOnce();
173+
174+
location = {
175+
pathname: "/chat/main/01JSESSIONB",
176+
search: "?probe=1",
177+
hash: "#second",
178+
};
179+
historyListener?.(location);
180+
181+
await vi.waitFor(() => {
182+
expect(loader).toHaveBeenCalledTimes(2);
183+
expect(router.getState().location).toEqual(location);
184+
});
185+
} finally {
186+
router.stop();
187+
route.loader = originalLoader;
188+
route.component = originalComponent;
189+
}
190+
});
191+
});
192+
30193
describe("Agent panel route paths", () => {
31194
it.each(AGENT_PANEL_CASES)("round-trips the %s panel with an encoded agent id", (panel) => {
32195
const pathname = pathForAgentPanel("team.writer", panel);

‎ui/src/app-routes.ts‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import type { PageDefinition, Router, RouterHistory } from "@openclaw/uirouter";
33
import {
44
agentRouteFromPath,
55
INTERNAL_AGENT_PATH_PARAM,
6-
INTERNAL_SESSION_PATH_PARAM,
76
INTERNAL_MEMORY_PATH_PARAM,
87
INTERNAL_PLUGINS_PATH_PARAM,
8+
INTERNAL_SESSION_PATH_PARAM,
99
memoryTabFromPath,
1010
pathForAgentPanel,
1111
pathForRoute,
12+
pathForWorkboardBoard,
1213
pluginsHubTabFromPath,
1314
routeIdFromPath,
1415
sessionRouteNamespaceFromPath,
@@ -94,7 +95,61 @@ const APP_ROUTE_TREE = [
9495
pluginPage,
9596
] as const;
9697

97-
const appRoutes = APP_ROUTE_TREE as readonly AppRoute[];
98+
const DYNAMIC_PATH_PARAM_BY_ROUTE: Partial<Record<RouteId, string>> = {
99+
agents: INTERNAL_AGENT_PATH_PARAM,
100+
chat: INTERNAL_SESSION_PATH_PARAM,
101+
dashboard: INTERNAL_SESSION_PATH_PARAM,
102+
memory: INTERNAL_MEMORY_PATH_PARAM,
103+
plugins: INTERNAL_PLUGINS_PATH_PARAM,
104+
};
105+
106+
function locationBeforeDynamicRouteBridge(
107+
routeId: RouteId,
108+
location: ReturnType<RouterHistory["location"]>,
109+
basePath: string,
110+
) {
111+
const search = new URLSearchParams(location.search);
112+
const pathParam = DYNAMIC_PATH_PARAM_BY_ROUTE[routeId];
113+
let pathname = pathParam ? search.get(pathParam) : null;
114+
if (pathParam) {
115+
search.delete(pathParam);
116+
} else if (routeId === "workboard") {
117+
const boardId = search.get("board");
118+
if (boardId) {
119+
try {
120+
pathname = pathForWorkboardBoard(boardId, basePath);
121+
search.delete("board");
122+
} catch {
123+
return location;
124+
}
125+
}
126+
}
127+
if (!pathname) {
128+
return location;
129+
}
130+
const serializedSearch = search.toString();
131+
return {
132+
...location,
133+
pathname,
134+
search: serializedSearch ? `?${serializedSearch}` : "",
135+
};
136+
}
137+
138+
function bridgeDynamicRouteLoaderDeps(route: AppRoute): AppRoute {
139+
const loaderDeps = route.loaderDeps;
140+
if (!loaderDeps || (route.id !== "workboard" && !DYNAMIC_PATH_PARAM_BY_ROUTE[route.id])) {
141+
return route;
142+
}
143+
// Router matching uses an exact-path location while dynamic page loaders
144+
// consume the real path. Keep one match identity across that startup bridge.
145+
return {
146+
...route,
147+
loaderDeps: (context, location) =>
148+
loaderDeps(context, locationBeforeDynamicRouteBridge(route.id, location, context.basePath)),
149+
};
150+
}
151+
152+
const appRoutes = APP_ROUTE_TREE.map((route) => bridgeDynamicRouteLoaderDeps(route as AppRoute));
98153

99154
export function createApplicationRouter(): ApplicationRouter {
100155
const router = createRouter<RouteId, ApplicationContext<RouteId>, AppRouteModule>({
@@ -193,12 +248,7 @@ export async function startApplicationRouter(
193248
if (initialDynamicRoute) {
194249
// Replace the synthetic exact-match location with the real browser path
195250
// before the shell renders; the matching loader data is already cached.
196-
await router.navigate(
197-
initialDynamicRoute[0],
198-
context,
199-
{ history: "none", revalidate: true },
200-
location,
201-
);
251+
await router.navigate(initialDynamicRoute[0], context, { history: "none" }, location);
202252
}
203253
}
204254

0 commit comments

Comments
 (0)