diff --git a/.changeset/39348-app-preview-open-basename.md b/.changeset/39348-app-preview-open-basename.md new file mode 100644 index 00000000000..8f63187de3a --- /dev/null +++ b/.changeset/39348-app-preview-open-basename.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(studio): the app preview's Open link and its nav route links open the app under the console's base path + +Clause-②: no +Metadata-protocol: none + +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//…`), 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//`); they still open in a new tab. A navigation row's shown route text is unchanged, and a `url` entry keeps its own address. diff --git a/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.openBasename-39348.test.tsx b/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.openBasename-39348.test.tsx new file mode 100644 index 00000000000..472d5afe397 --- /dev/null +++ b/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.openBasename-39348.test.tsx @@ -0,0 +1,93 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * objectstack#39348: the Studio app preview's links open the running app under + * the console's basename. + * + * The console is mounted under `/_console`. The preview's toolbar "Open" link + * and each nav row's route link were raw anchors on the ROUTER path + * (`/apps//…`), so the new tab left the console and the server answered + * `404 ENDPOINT_NOT_FOUND`. They now go through the router, which applies its + * basename to the href. + * + * The cases: + * - under a non-root basename, Open's href is `/apps//` and it + * still opens in a new tab; + * - a nav row's route link carries the basename too, while the route TEXT + * shown stays the router path the shell resolves; + * - an absolute `url` entry keeps its own address, no basename in front; + * - controls: a router at the root, and no router at all (the designer + * gallery harness), both give the bare `/apps//`. + */ + +import { describe, it, expect, afterEach } from 'vitest'; +import { render, screen, cleanup } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; +import { AppPreview } from './AppPreview'; + +afterEach(cleanup); + +const DRAFT: Record = { + name: 'crm', + label: 'CRM', + navigation: [ + { id: 'accounts', type: 'object', label: 'Accounts', objectName: 'account' }, + { id: 'docs', type: 'url', label: 'Docs', url: 'https://docs.example.com' }, + ], +}; + +function preview() { + return ; +} + +function renderUnder(basename?: string) { + const root = basename ?? ''; + return render( + + {preview()} + , + ); +} + +function openLink(): HTMLAnchorElement { + return screen.getByText('Open').closest('a') as HTMLAnchorElement; +} + +function linkByText(text: string): HTMLAnchorElement { + return screen.getByText(text, { selector: 'a' }) as HTMLAnchorElement; +} + +describe('AppPreview links under the console basename (objectstack#39348)', () => { + it('Open names /apps// and opens in a new tab', () => { + renderUnder('/_console'); + const a = openLink(); + expect(a.getAttribute('href')).toBe('/_console/apps/crm/'); + expect(a.getAttribute('target')).toBe('_blank'); + expect(a.getAttribute('rel')).toBe('noreferrer'); + }); + + it("a nav row's route link carries the basename; its text stays the router path", () => { + renderUnder('/_console'); + const a = linkByText('/apps/crm/account'); + expect(a.getAttribute('href')).toBe('/_console/apps/crm/account'); + expect(a.getAttribute('target')).toBe('_blank'); + }); + + it('an absolute url entry keeps its own address', () => { + renderUnder('/_console'); + const a = linkByText('https://docs.example.com'); + expect(a.getAttribute('href')).toBe('https://docs.example.com'); + }); + + it('control: a router at the root gives the bare path', () => { + renderUnder(); + expect(openLink().getAttribute('href')).toBe('/apps/crm/'); + expect(linkByText('/apps/crm/account').getAttribute('href')).toBe('/apps/crm/account'); + }); + + it('control: with no router above (the gallery harness) the path is the address', () => { + render(preview()); + expect(openLink().getAttribute('href')).toBe('/apps/crm/'); + expect(linkByText('/apps/crm/account').getAttribute('href')).toBe('/apps/crm/account'); + }); +}); diff --git a/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.tsx b/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.tsx index 43d51814833..a39e9464df9 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/previews/AppPreview.tsx @@ -57,6 +57,7 @@ import { MousePointerClick, Puzzle, } from 'lucide-react'; +import { Link, useInRouterContext } from 'react-router-dom'; import { resolveHref, type NavTargetLabelResolver } from '@object-ui/layout'; import type { NavigationItemType } from '@object-ui/types'; import { resolveI18nLabel } from '@objectstack/spec/ui'; @@ -288,6 +289,49 @@ function findFirstLandingAcross(trees: NavItem[][], docs = false): NavItem | und return undefined; } +/** + * A link that opens a ROUTER path (`/apps//…`) of the running app in a new + * tab (objectstack#39348). + * + * Rendered through `Link`, so the router's basename is applied: the console + * ships under a mount path (`/_console`), and a bare rooted `href` steps + * outside of it onto the server's 404. `Link` leaves a `target="_blank"` click + * to the browser, so the new tab opens the address the anchor names, as the + * agent and tool previews' links do. + * + * With no router above the preview (the designer gallery harness) there is no + * basename to apply, and the path is the address. + */ +const NAV_LINK_CLASS = + 'ml-auto font-mono text-[10px] text-muted-foreground hover:text-foreground inline-flex items-center gap-1'; + +function RuntimeLink({ + path, + className, + title, + stopPropagation, + children, +}: { + path: string; + className?: string; + title?: string; + stopPropagation?: boolean; + children: React.ReactNode; +}) { + const onClick = stopPropagation ? (e: React.MouseEvent) => e.stopPropagation() : undefined; + // Rules-of-hooks safe: whether a router sits above is a fact about the + // mount, and cannot change under it. + return useInRouterContext() ? ( + + {children} + + ) : ( + + {children} + + ); +} + export function AppPreview({ name, draft, editing, selection, onSelectionChange, onPatch, locale }: MetadataPreviewProps) { const appName = String((draft as any).name ?? name ?? ''); // `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, hint="app" toolbar={ baseRuntimeUrl && ( - {tr('engine.appPreview.open', locale)} - + ) } > @@ -488,17 +530,24 @@ function NavRow({ {item.target && ( {item.target} )} - {url && ( - e.stopPropagation()} - className="ml-auto font-mono text-[10px] text-muted-foreground hover:text-foreground inline-flex items-center gap-1" - > - {url} - - )} + {url && + (item.external ? ( + e.stopPropagation()} + className={NAV_LINK_CLASS} + > + {url} + + ) : ( + // A router path the shell resolved (`/apps//…`): it opens + // under the console's basename, as the toolbar's Open link does. + + {url} + + ))} {item.children?.map((c, i) => (