Skip to content

Commit affba40

Browse files
fix(app-shell): metadata history Back to item opens the item under every route nesting (#29316)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} --> _Requested by **Steedos** · [project thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbKeKvZtRKpgpNfnuuBoDndk)_ Fixes #28626 Clause-②: no Metadata-protocol: none ## 维护者速读 - **改了什么**:元数据条目历史页的「Back to item」不再按匹配到的路由回退,改为从当前 URL 去掉最后一段(`history`)并保留查询串,因此回到 `…/metadata/TYPE/NAME`。 - **为什么改**:原写法 `../NAME` 相对路由解析,会丢掉整条路由:有应用时落到以条目名命名的类型列表,无应用时落到应用根下的路径,并且丢掉 `?package=` / `?scope=environment`。目前在线上只有 Setup 的数据源历史页还会走到这条链接。 - **风险与回滚**:只改一个按钮的跳转目标;Studio「All metadata」区域的链接分支没动。回滚即还原 `ResourceHistoryPage.tsx` 一处。 - **先看**:`ui/packages/app-shell/src/views/item-tools/ResourceHistoryPage.tsx` 的 `goBackToItem`;新测试 `ResourceHistoryPage.backToItem-28626.test.tsx`。 - **两分钟验证**:`pnpm --dir ui exec vitest run packages/app-shell/src/views/item-tools/ResourceHistoryPage.backToItem-28626.test.tsx`,预期 6/6 通过。 - **需要你做的**:无,席位落地。 ## What changes `MetadataResourceHistoryPage` built its non-area **Back to item** link as `navigate('../NAME')`. React Router resolves that `..` against the matched route, not the URL, so it drops the whole matched route: - with an active app (`/apps/:appName/*` → `metadata` → `:type/:name/history`) it landed on `/apps/setup/metadata/NAME`, the `:type` route, which Setup then sends to Studio's list of a type named after the item; - with no app (`/apps/:appName/*` → `metadata/:type/:name/history`) it landed on `/apps/setup/NAME`; - under the exported `MetadataResourceRouter` (`component/:ns/:name/*`, type in `?type=`) it landed on `/apps/APP/NAME`; - the query (`?package=`, `?scope=environment`, `?type=`) was dropped every time. The non-area branch now cuts the URL's own last segment as it is spelled and keeps the query: `navigate({ pathname: pathname.replace(LAST_SEGMENT, ''), search })`, where the pattern is a slash, one or more non-slash characters and an optional trailing slash at the end of the path. A relative `navigate({ pathname: '..', search }, { relative: 'path' })` was tried first and dropped: the router resolves it against the decoded match pathname, so an encoded name came back decoded (`sales%20db` → `sales db`). The encoded-name test row pins that. The Studio "All metadata" area branch (built from `areaHost.basePath`) is unchanged and is the test's control row. **Reach today.** Since #28818 every Setup metadata route except the datasource's redirects to Studio's area, so in the Console only the datasource history page (with and without an active app) still reaches this link. The exported router is not mounted by the Console (`component/metadata/resource/*` is caught by `LegacyMetadataRedirect`), but a host can mount it, and the fix covers it too. ## Tests New `ui/packages/app-shell/src/views/item-tools/ResourceHistoryPage.backToItem-28626.test.tsx`: the real page and React Router, the metadata client stubbed. Six rows: both `AppContent` nestings, the query kept under both, an encoded name kept encoded, the exported router's splat with `?type=`, and the Studio area as control. - Fix head `da4f9be7`: 6 passed. - Red on the current line: the same file against `origin/main`'s page (`8a8a03d3`, restored from base, then restored to `HEAD` and proven by hash and an empty `git diff HEAD`): 5 failed, 1 passed (the Studio control). The relative-`..` variant (`6108d08c`) was 5 passed, 1 failed (encoded name). ## Acceptance notes - Only the datasource history page reaches this link live in the Console today, as measured above. - Datasource names are `^[a-z_][a-z0-9_]*$` in the spec, so the decoded spelling never bites the live datasource page; the encoded-name row guards the exported router with other names. ## Verification All on head `da4f9be7`. <details><summary>Tests and type-check</summary> - `pnpm --dir ui exec vitest run packages/app-shell/src/views/item-tools/ResourceHistoryPage.backToItem-28626.test.tsx`: Tests 6 passed (6). - Reverse leg, `origin/main`'s page restored into the tree: Tests 5 failed, 1 passed (6); restored to `HEAD`, hash matched, `git diff HEAD` empty. - Every ui test file that names the page (`git grep -l` on `ResourceHistoryPage` / `MetadataResourceHistoryPage`, 21 files across app-shell and the console): Test Files 21 passed (21), Tests 306 passed (306). CI's Test UI runs the rest. - `pnpm exec turbo run type-check --filter=@object-ui/app-shell`: Tasks 65 successful, 65 total. - `pnpm --dir ui exec vitest run scripts/`: Test Files 169 passed, 2 skipped (171). </details> <details><summary>Derived gates</summary> - `node scripts/pm/dispatch-gates.mjs --ran`: 34 derived families, 34 run, 0 NOT-MEASURED; every one exit 0, `check-sdui-manifest` included (no registry change). - `PR_BODY=... node scripts/check-partof-closing-keyword.mjs`: exit 0. - `pnpm --dir ui check:new-line-citations`: 0 new citations, exit 0. - Full dogfood battery: n/a (no `!` in the title, no narrowing arm). </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jSipqJ9BXYCu6y2jgs)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ba1f164 commit affba40

3 files changed

Lines changed: 183 additions & 5 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
---
4+
5+
Clause-②: no
6+
Metadata-protocol: none
7+
8+
A metadata item's history page now opens the item again from **Back to item**. Outside Studio's "All metadata" area (Setup's datasource history, with or without an active app, and the exported `MetadataResourceRouter`) the link used to resolve against the matched route, so it landed on a type list named after the item, or on a path at the app root, and dropped the query. It now opens `…/metadata/TYPE/NAME` with the query the page was opened with (`?package=`, `?scope=environment`). The Studio area's link is unchanged.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#28626 — the routed history page's "Back to item" opens the item
5+
* it is the history of, `…/metadata/TYPE/NAME`, with the query the item page
6+
* reads (`?package=`, `?scope=environment`), under every route nesting that
7+
* still mounts the page.
8+
*
9+
* Setup keeps one metadata type on its own pages, the datasource (ADR-0146
10+
* D1, `SETUP_KEPT_METADATA_TYPE`), and `AppContent` mounts its history page
11+
* under two nestings: `/apps/:appName/*` → `metadata` → `:type/:name/history`
12+
* with an active app, and `/apps/:appName/*` → `metadata/:type/:name/history`
13+
* with none. The exported `MetadataResourceRouter` mounts it under a splat,
14+
* `component/:ns/:name/*`, with the type in `?type=`. Under Studio's "All
15+
* metadata" area the link is built from the area's root and is the control.
16+
*
17+
* Real: the page and React Router. Stubbed: the metadata client and types.
18+
*/
19+
20+
import '@testing-library/jest-dom/vitest';
21+
import * as React from 'react';
22+
import { afterEach, describe, expect, it, vi } from 'vitest';
23+
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
24+
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
25+
import { MetadataAreaHostContext, type MetadataAreaHost } from '../metadata-admin/metadataAreaHost';
26+
27+
const client = { history: vi.fn(async () => ({ events: [] })) };
28+
29+
vi.mock('../metadata-admin/useMetadata', async (importOriginal) => ({
30+
...(await importOriginal<Record<string, unknown>>()),
31+
useMetadataClient: () => client,
32+
useMetadataTypes: () => ({
33+
loading: false,
34+
error: null,
35+
entries: [{ type: 'datasource', label: 'Datasource', domain: 'system' }],
36+
}),
37+
}));
38+
vi.mock('../metadata-admin/i18n', async (importOriginal) => ({
39+
...(await importOriginal<Record<string, unknown>>()),
40+
useMetadataLocale: () => 'en-US',
41+
}));
42+
43+
import { MetadataResourceHistoryPage } from './ResourceHistoryPage';
44+
45+
afterEach(() => {
46+
cleanup();
47+
vi.clearAllMocks();
48+
});
49+
50+
function Here() {
51+
const { pathname, search } = useLocation();
52+
return <output data-testid="here">{`${pathname}${search}`}</output>;
53+
}
54+
55+
const item = <div data-testid="item-page" />;
56+
const history = <MetadataResourceHistoryPage />;
57+
58+
/** `AppContent`'s branch with an active app: the history route nests in `metadata`. */
59+
function WithApp() {
60+
return (
61+
<Routes>
62+
<Route path="metadata">
63+
<Route path=":type" element={<div data-testid="type-list" />} />
64+
<Route path=":type/:name" element={item} />
65+
<Route path=":type/:name/history" element={history} />
66+
</Route>
67+
</Routes>
68+
);
69+
}
70+
71+
/** `AppContent`'s branch with no app: one flat route per page. */
72+
function NoApp() {
73+
return (
74+
<Routes>
75+
<Route path="metadata/:type" element={<div data-testid="type-list" />} />
76+
<Route path="metadata/:type/:name" element={item} />
77+
<Route path="metadata/:type/:name/history" element={history} />
78+
</Routes>
79+
);
80+
}
81+
82+
function renderUnderApps(branch: React.ReactNode, url: string) {
83+
return render(
84+
<MemoryRouter initialEntries={[url]}>
85+
<Here />
86+
<Routes>
87+
<Route path="/apps/:appName/*" element={branch} />
88+
<Route path="/studio/*" element={<div data-testid="studio" />} />
89+
</Routes>
90+
</MemoryRouter>,
91+
);
92+
}
93+
94+
const backToItem = () => fireEvent.click(screen.getByRole('button', { name: /Back to item/ }));
95+
96+
describe('"Back to item" on the routed history page (objectstack#28626)', () => {
97+
it('with an active app it opens the item, not a type list named after it', async () => {
98+
renderUnderApps(<WithApp />, '/apps/setup/metadata/datasource/warehouse/history');
99+
backToItem();
100+
expect(await screen.findByTestId('item-page')).toBeInTheDocument();
101+
expect(screen.getByTestId('here')).toHaveTextContent(/^\/apps\/setup\/metadata\/datasource\/warehouse$/);
102+
});
103+
104+
it('with no app it opens the item, not a root path named after it', async () => {
105+
renderUnderApps(<NoApp />, '/apps/setup/metadata/datasource/warehouse/history');
106+
backToItem();
107+
expect(await screen.findByTestId('item-page')).toBeInTheDocument();
108+
expect(screen.getByTestId('here')).toHaveTextContent(/^\/apps\/setup\/metadata\/datasource\/warehouse$/);
109+
});
110+
111+
it('keeps the query the item page reads, under both nestings', async () => {
112+
for (const branch of [<WithApp key="with" />, <NoApp key="none" />]) {
113+
renderUnderApps(branch, '/apps/crm/metadata/datasource/warehouse/history?package=com.example.crm&scope=environment');
114+
backToItem();
115+
expect(await screen.findByTestId('item-page')).toBeInTheDocument();
116+
expect(screen.getByTestId('here')).toHaveTextContent(
117+
/^\/apps\/crm\/metadata\/datasource\/warehouse\?package=com\.example\.crm&scope=environment$/,
118+
);
119+
cleanup();
120+
}
121+
});
122+
123+
it('keeps an encoded item name encoded', async () => {
124+
renderUnderApps(<WithApp />, '/apps/setup/metadata/datasource/sales%20db/history');
125+
backToItem();
126+
expect(await screen.findByTestId('item-page')).toBeInTheDocument();
127+
expect(screen.getByTestId('here')).toHaveTextContent(/^\/apps\/setup\/metadata\/datasource\/sales%20db$/);
128+
});
129+
130+
it('under the exported resource router (a splat, type in the query) it opens the item with its ?type=', async () => {
131+
renderUnderApps(
132+
<Routes>
133+
<Route path="component/:ns/:name/*" element={<MetadataResourceHistoryPage type="datasource" name="warehouse" />} />
134+
</Routes>,
135+
'/apps/crm/component/metadata/resource/warehouse/history?type=datasource',
136+
);
137+
backToItem();
138+
expect(screen.getByTestId('here')).toHaveTextContent(
139+
/^\/apps\/crm\/component\/metadata\/resource\/warehouse\?type=datasource$/,
140+
);
141+
});
142+
143+
it('under Studio\'s "All metadata" area it still opens the area\'s item page (control)', async () => {
144+
const host: MetadataAreaHost = { packageId: 'com.example.crm', readOnly: false, basePath: '/studio/com.example.crm/metadata' };
145+
render(
146+
<MetadataAreaHostContext.Provider value={host}>
147+
<MemoryRouter initialEntries={['/studio/com.example.crm/metadata/flow/lead_route/history?package=com.example.crm']}>
148+
<Here />
149+
<Routes>
150+
<Route path="/studio/:pkg/metadata">
151+
<Route path=":type/:name" element={item} />
152+
<Route path=":type/:name/history" element={history} />
153+
</Route>
154+
</Routes>
155+
</MemoryRouter>
156+
</MetadataAreaHostContext.Provider>,
157+
);
158+
backToItem();
159+
expect(await screen.findByTestId('item-page')).toBeInTheDocument();
160+
expect(screen.getByTestId('here')).toHaveTextContent(
161+
/^\/studio\/com\.example\.crm\/metadata\/flow\/lead_route\?package=com\.example\.crm$/,
162+
);
163+
});
164+
});

‎ui/packages/app-shell/src/views/item-tools/ResourceHistoryPage.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ export function MetadataResourceHistoryPage({
9494
// read-only package offers no rollback, and "Back to item" names the area's
9595
// item page with the query it came with (its `?package=` and scope).
9696
const areaHost = useMetadataAreaHost();
97-
const { search } = useLocation();
98-
const backToItem = areaHost
99-
? `${areaHost.basePath}/${encodeURIComponent(type)}/${encodeURIComponent(name)}${search}`
100-
: `../${encodeURIComponent(name)}`;
97+
const { pathname, search } = useLocation();
98+
// Anywhere else the item page is this URL minus its last (`history`)
99+
// segment, with the same query, under whatever route nesting mounted the
100+
// page. Cut from the URL as it is spelled, never resolved against the
101+
// matched route: that drops the whole route, and a relative `..` hands back
102+
// the item name decoded.
103+
const goBackToItem = () =>
104+
areaHost
105+
? navigate(`${areaHost.basePath}/${encodeURIComponent(type)}/${encodeURIComponent(name)}${search}`)
106+
: navigate({ pathname: pathname.replace(/\/[^/]+\/?$/, ''), search });
101107

102108
return (
103109
<PageShell
@@ -110,7 +116,7 @@ export function MetadataResourceHistoryPage({
110116
<Button
111117
variant="ghost"
112118
size="sm"
113-
onClick={() => navigate(backToItem)}
119+
onClick={goBackToItem}
114120
>
115121
<ArrowLeft className="h-4 w-4 mr-1" />
116122
Back to item

0 commit comments

Comments
 (0)