Skip to content

Commit e2ae02a

Browse files
committed
refactor: keep dynamic route identity page-owned
1 parent 40a5d31 commit e2ae02a

4 files changed

Lines changed: 42 additions & 63 deletions

File tree

‎ui/src/app-routes.ts‎

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
memoryTabFromPath,
1010
pathForAgentPanel,
1111
pathForRoute,
12-
pathForWorkboardBoard,
1312
pluginsHubTabFromPath,
1413
routeIdFromPath,
1514
sessionRouteNamespaceFromPath,
@@ -95,61 +94,7 @@ const APP_ROUTE_TREE = [
9594
pluginPage,
9695
] as const;
9796

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));
97+
const appRoutes = APP_ROUTE_TREE as readonly AppRoute[];
15398

15499
export function createApplicationRouter(): ApplicationRouter {
155100
const router = createRouter<RouteId, ApplicationContext<RouteId>, AppRouteModule>({

‎ui/src/pages/chat/route.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { RouteLocation } from "@openclaw/uirouter";
22
import { definePage } from "@openclaw/uirouter";
33
import { html, nothing } from "lit";
4-
import { routePageSpec } from "../../app-route-paths.ts";
4+
import {
5+
INTERNAL_SESSION_PATH_PARAM,
6+
pathForRoute,
7+
routePageSpec,
8+
} from "../../app-route-paths.ts";
59
import type { ApplicationContext } from "../../app/context.ts";
610
import { t } from "../../i18n/index.ts";
711
import type { BoardFace } from "../../lib/board/settings.ts";
@@ -31,11 +35,32 @@ function renderAmbiguous(data: Extract<ChatRouteData, { kind: "ambiguous" }>) {
3135
`;
3236
}
3337

38+
function sessionLoaderDeps(
39+
face: BoardFace,
40+
context: ApplicationContext,
41+
location: RouteLocation,
42+
): string {
43+
const search = new URLSearchParams(location.search);
44+
const bridgedPath =
45+
location.pathname === pathForRoute(face, context.basePath)
46+
? search.get(INTERNAL_SESSION_PATH_PARAM)
47+
: null;
48+
if (bridgedPath) {
49+
search.delete(INTERNAL_SESSION_PATH_PARAM);
50+
}
51+
const serializedSearch = search.toString();
52+
return `${bridgedPath ?? location.pathname}\u0000${
53+
serializedSearch ? `?${serializedSearch}` : ""
54+
}`;
55+
}
56+
3457
function sessionPage(face: BoardFace) {
3558
return definePage({
3659
...routePageSpec(face),
37-
loaderDeps: (_context: ApplicationContext, location: RouteLocation) =>
38-
`${location.pathname}\u0000${location.search}`,
60+
// The application router temporarily maps dynamic session URLs onto the
61+
// static face route. Both locations describe the same loader match.
62+
loaderDeps: (context: ApplicationContext, location: RouteLocation) =>
63+
sessionLoaderDeps(face, context, location),
3964
loader: async (context: ApplicationContext, { location, signal }) => {
4065
const { loadChatRoute } = await import("./route-loader.ts");
4166
return await loadChatRoute(context, location, face, signal);

‎ui/src/pages/config/route.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ function loadConfigRoute(context: ApplicationContext, location: RouteLocation) {
1616
function configPage(id: ConfigPageId) {
1717
return definePage({
1818
...routePageSpec(id),
19-
loaderDeps: (_context: ApplicationContext, location: RouteLocation) =>
20-
`${location.pathname}\u0000${location.search}\u0000${location.hash}`,
19+
loaderDeps: (_context: ApplicationContext, location: RouteLocation) => {
20+
const route = configRouteData(location);
21+
return `${route.pathname}\u0000${route.search}\u0000${route.hash}`;
22+
},
2123
loader: (context: ApplicationContext, { location }) => loadConfigRoute(context, location),
2224
component: () =>
2325
import("./config-page.ts").then(() => ({

‎ui/src/pages/workboard/route.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { resolveWorkboardRouteLocation, type WorkboardRouteData } from "./route-
77

88
export type { WorkboardRouteData } from "./route-location.ts";
99

10+
function workboardLoaderDeps(context: ApplicationContext, location: RouteLocation): string {
11+
const route = resolveWorkboardRouteLocation(location, context.basePath);
12+
const canonicalLocation = route.canonicalLocation;
13+
return `${canonicalLocation?.pathname ?? location.pathname}\u0000${
14+
canonicalLocation?.search ?? route.search
15+
}`;
16+
}
17+
1018
async function loadWorkboardRoute(
1119
context: ApplicationContext,
1220
location: RouteLocation,
@@ -24,8 +32,7 @@ async function loadWorkboardRoute(
2432

2533
export const page = definePage({
2634
...routePageSpec("workboard"),
27-
loaderDeps: (_context: ApplicationContext, location: RouteLocation) =>
28-
`${location.pathname}\u0000${location.search}`,
35+
loaderDeps: workboardLoaderDeps,
2936
loader: (context: ApplicationContext, { location }) => loadWorkboardRoute(context, location),
3037
component: () =>
3138
import("./workboard-page.ts").then(() => ({

0 commit comments

Comments
 (0)