Skip to content

Commit fb4735d

Browse files
fix(studio): app preview Open and nav route links keep the console basename (#39430)
Fixes #39348 Clause-②: no Metadata-protocol: none ## 维护者速读 - 在 Studio 里预览一个应用,点工具栏的"打开"或导航行旁的路由链接,新标签页以前跳到控制台外面报 404,现在落在控制台里的这个应用上。 - 属于 Studio 搭建体验;替每个在 Studio 里预览应用的人解决打不开的问题。 - 若出错,受影响的是预览面板里的这几个链接打开的地址;改动只在预览组件,可直接撤回。 - 无。 ## What changes - `AppPreview.tsx`: the toolbar **Open** link and each nav row's route link now render through a small `RuntimeLink`, which uses react-router's `Link` when a router sits above the preview. `Link` puts the router basename (`/_console`) in front of the router path, so the href is `/_console/apps/crm/` instead of `/apps/crm/`. `target="_blank"` is kept: `Link` leaves a new-tab click to the browser. This is the same pattern the agent and tool previews already use (`AgentChatLink`). - With no router above (the designer gallery harness, the router-less unit tests) the anchor keeps the bare path, as before. - A nav row whose entry `resolveHref` marks `external` (a `url` entry with `target: _blank`) stays a raw anchor; every other row goes through `Link`, exactly as `NavigationRenderer` draws the same entry in the running shell. An absolute `https://` url without a target also keeps its address (react-router's `Link` renders absolute URLs verbatim; pinned in the test). - Bounded in-place fix, declared: the card names only the Open link; the nav row route links in the same file had the same raw-href defect (same defect class, same mechanical shape, same file under this claim, same gate family), so they are fixed here and pinned in the same test. - Changeset `@object-ui/app-shell: patch` (the precedent other app-shell fixes follow). ## Tests New `AppPreview.openBasename-39348.test.tsx` (5 cases): under `MemoryRouter basename="/_console"` Open's href is `/_console/apps/crm/` with `target=_blank`; a nav row's route link href is `/_console/apps/crm/account` while its text stays `/apps/crm/account`; an absolute url entry keeps `https://docs.example.com`; controls at a root router and with no router give the bare path. Ablation (original `AppPreview.tsx` restored on disk, test file kept): 2 failed / 3 passed, `expected '/apps/crm/' to be '/_console/apps/crm/'` and `expected '/apps/crm/account' to be '/_console/apps/crm/account'`; fix restored afterwards. <details><summary>Local readings at 8bff235</summary> - `vitest run` over `AppPreview*`, `adminPreviews*`, `ResourceEditPage.navDeepLink-11153` (from `ui/`): Test Files 9 passed (9), Tests 95 passed (95). - `tsc --noEmit -p tsconfig.json` (app-shell src): exit 0. eslint on the two files: 0 errors (7 pre-existing `no-explicit-any` warnings on untouched lines). - Test typecheck: the whole-package `tsconfig.test.json` run hit the 4 GB heap cap (NOT MEASURED, CI); the same config narrowed to the new test file: exit 0. </details> ## Acceptance notes - Read first: `AppPreview.tsx` `RuntimeLink` (the router/no-router branch) and the NavRow `item.external` branch. ## Verification Two-minute check: `objectstack dev --ui --seed-admin` in `examples/app-showcase`, Studio, open any app, Preview, click **Open**: the new tab lands on `/_console/apps/APP/`, not a 404. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016WV4d1cnRvyiAqrarmDhZV Co-authored-by: Claude <noreply@anthropic.com>
1 parent d487185 commit fb4735d

3 files changed

Lines changed: 168 additions & 16 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
---
4+
5+
fix(studio): the app preview's Open link and its nav route links open the app under the console's base path
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
Who sees it: anyone previewing an app in Studio. The preview's **Open** link and each navigation row's route link named the bare router path (`/apps/<app>/…`), so on a console served under `/_console` the new tab left the console and the server answered `404 ENDPOINT_NOT_FOUND`. Both now go through the router, which puts the console's base path in front (`/_console/apps/<app>/`); they still open in a new tab. A navigation row's shown route text is unchanged, and a `url` entry keeps its own address.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#39348: the Studio app preview's links open the running app under
5+
* the console's basename.
6+
*
7+
* The console is mounted under `/_console`. The preview's toolbar "Open" link
8+
* and each nav row's route link were raw anchors on the ROUTER path
9+
* (`/apps/<app>/…`), so the new tab left the console and the server answered
10+
* `404 ENDPOINT_NOT_FOUND`. They now go through the router, which applies its
11+
* basename to the href.
12+
*
13+
* The cases:
14+
* - under a non-root basename, Open's href is `<basename>/apps/<app>/` and it
15+
* still opens in a new tab;
16+
* - a nav row's route link carries the basename too, while the route TEXT
17+
* shown stays the router path the shell resolves;
18+
* - an absolute `url` entry keeps its own address, no basename in front;
19+
* - controls: a router at the root, and no router at all (the designer
20+
* gallery harness), both give the bare `/apps/<app>/`.
21+
*/
22+
23+
import { describe, it, expect, afterEach } from 'vitest';
24+
import { render, screen, cleanup } from '@testing-library/react';
25+
import { MemoryRouter } from 'react-router-dom';
26+
import { AppPreview } from './AppPreview';
27+
28+
afterEach(cleanup);
29+
30+
const DRAFT: Record<string, unknown> = {
31+
name: 'crm',
32+
label: 'CRM',
33+
navigation: [
34+
{ id: 'accounts', type: 'object', label: 'Accounts', objectName: 'account' },
35+
{ id: 'docs', type: 'url', label: 'Docs', url: 'https://docs.example.com' },
36+
],
37+
};
38+
39+
function preview() {
40+
return <AppPreview type="app" name="crm" draft={DRAFT} locale="en" />;
41+
}
42+
43+
function renderUnder(basename?: string) {
44+
const root = basename ?? '';
45+
return render(
46+
<MemoryRouter basename={basename} initialEntries={[`${root}/studio`]}>
47+
{preview()}
48+
</MemoryRouter>,
49+
);
50+
}
51+
52+
function openLink(): HTMLAnchorElement {
53+
return screen.getByText('Open').closest('a') as HTMLAnchorElement;
54+
}
55+
56+
function linkByText(text: string): HTMLAnchorElement {
57+
return screen.getByText(text, { selector: 'a' }) as HTMLAnchorElement;
58+
}
59+
60+
describe('AppPreview links under the console basename (objectstack#39348)', () => {
61+
it('Open names <basename>/apps/<app>/ and opens in a new tab', () => {
62+
renderUnder('/_console');
63+
const a = openLink();
64+
expect(a.getAttribute('href')).toBe('/_console/apps/crm/');
65+
expect(a.getAttribute('target')).toBe('_blank');
66+
expect(a.getAttribute('rel')).toBe('noreferrer');
67+
});
68+
69+
it("a nav row's route link carries the basename; its text stays the router path", () => {
70+
renderUnder('/_console');
71+
const a = linkByText('/apps/crm/account');
72+
expect(a.getAttribute('href')).toBe('/_console/apps/crm/account');
73+
expect(a.getAttribute('target')).toBe('_blank');
74+
});
75+
76+
it('an absolute url entry keeps its own address', () => {
77+
renderUnder('/_console');
78+
const a = linkByText('https://docs.example.com');
79+
expect(a.getAttribute('href')).toBe('https://docs.example.com');
80+
});
81+
82+
it('control: a router at the root gives the bare path', () => {
83+
renderUnder();
84+
expect(openLink().getAttribute('href')).toBe('/apps/crm/');
85+
expect(linkByText('/apps/crm/account').getAttribute('href')).toBe('/apps/crm/account');
86+
});
87+
88+
it('control: with no router above (the gallery harness) the path is the address', () => {
89+
render(preview());
90+
expect(openLink().getAttribute('href')).toBe('/apps/crm/');
91+
expect(linkByText('/apps/crm/account').getAttribute('href')).toBe('/apps/crm/account');
92+
});
93+
});

‎ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.tsx‎

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
MousePointerClick,
5858
Puzzle,
5959
} from 'lucide-react';
60+
import { Link, useInRouterContext } from 'react-router-dom';
6061
import { resolveHref, type NavTargetLabelResolver } from '@object-ui/layout';
6162
import type { NavigationItemType } from '@object-ui/types';
6263
import { resolveI18nLabel } from '@objectstack/spec/ui';
@@ -288,6 +289,49 @@ function findFirstLandingAcross(trees: NavItem[][], docs = false): NavItem | und
288289
return undefined;
289290
}
290291

292+
/**
293+
* A link that opens a ROUTER path (`/apps/<app>/…`) of the running app in a new
294+
* tab (objectstack#39348).
295+
*
296+
* Rendered through `Link`, so the router's basename is applied: the console
297+
* ships under a mount path (`/_console`), and a bare rooted `href` steps
298+
* outside of it onto the server's 404. `Link` leaves a `target="_blank"` click
299+
* to the browser, so the new tab opens the address the anchor names, as the
300+
* agent and tool previews' links do.
301+
*
302+
* With no router above the preview (the designer gallery harness) there is no
303+
* basename to apply, and the path is the address.
304+
*/
305+
const NAV_LINK_CLASS =
306+
'ml-auto font-mono text-[10px] text-muted-foreground hover:text-foreground inline-flex items-center gap-1';
307+
308+
function RuntimeLink({
309+
path,
310+
className,
311+
title,
312+
stopPropagation,
313+
children,
314+
}: {
315+
path: string;
316+
className?: string;
317+
title?: string;
318+
stopPropagation?: boolean;
319+
children: React.ReactNode;
320+
}) {
321+
const onClick = stopPropagation ? (e: React.MouseEvent) => e.stopPropagation() : undefined;
322+
// Rules-of-hooks safe: whether a router sits above is a fact about the
323+
// mount, and cannot change under it.
324+
return useInRouterContext() ? (
325+
<Link to={path} target="_blank" rel="noreferrer" onClick={onClick} className={className} title={title}>
326+
{children}
327+
</Link>
328+
) : (
329+
<a href={path} target="_blank" rel="noreferrer" onClick={onClick} className={className} title={title}>
330+
{children}
331+
</a>
332+
);
333+
}
334+
291335
export function AppPreview({ name, draft, editing, selection, onSelectionChange, onPatch, locale }: MetadataPreviewProps) {
292336
const appName = String((draft as any).name ?? name ?? '');
293337
// `label` is `I18nLabel` too: resolved in the designer locale, the app's own
@@ -364,15 +408,13 @@ export function AppPreview({ name, draft, editing, selection, onSelectionChange,
364408
hint="app"
365409
toolbar={
366410
baseRuntimeUrl && (
367-
<a
368-
href={baseRuntimeUrl}
369-
target="_blank"
370-
rel="noreferrer"
411+
<RuntimeLink
412+
path={baseRuntimeUrl}
371413
className="text-xs text-muted-foreground hover:text-foreground inline-flex items-center gap-1"
372414
title={tr('engine.appPreview.openTitle', locale)}
373415
>
374416
{tr('engine.appPreview.open', locale)} <ExternalLink className="h-3 w-3" />
375-
</a>
417+
</RuntimeLink>
376418
)
377419
}
378420
>
@@ -488,17 +530,24 @@ function NavRow({
488530
{item.target && (
489531
<span className="font-mono text-[10px] text-muted-foreground truncate">{item.target}</span>
490532
)}
491-
{url && (
492-
<a
493-
href={url}
494-
target="_blank"
495-
rel="noreferrer"
496-
onClick={(e) => e.stopPropagation()}
497-
className="ml-auto font-mono text-[10px] text-muted-foreground hover:text-foreground inline-flex items-center gap-1"
498-
>
499-
{url} <ExternalLink className="h-3 w-3" />
500-
</a>
501-
)}
533+
{url &&
534+
(item.external ? (
535+
<a
536+
href={url}
537+
target="_blank"
538+
rel="noreferrer"
539+
onClick={(e) => e.stopPropagation()}
540+
className={NAV_LINK_CLASS}
541+
>
542+
{url} <ExternalLink className="h-3 w-3" />
543+
</a>
544+
) : (
545+
// A router path the shell resolved (`/apps/<app>/…`): it opens
546+
// under the console's basename, as the toolbar's Open link does.
547+
<RuntimeLink path={url} stopPropagation className={NAV_LINK_CLASS}>
548+
{url} <ExternalLink className="h-3 w-3" />
549+
</RuntimeLink>
550+
))}
502551
</div>
503552
{item.children?.map((c, i) => (
504553
<NavRow

0 commit comments

Comments
 (0)