Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/39348-app-preview-open-basename.md
Original file line number Diff line number Diff line change
@@ -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/<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.
Original file line number Diff line number Diff line change
@@ -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/<app>/…`), 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 `<basename>/apps/<app>/` 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/<app>/`.
*/

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<string, unknown> = {
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 <AppPreview type="app" name="crm" draft={DRAFT} locale="en" />;
}

function renderUnder(basename?: string) {
const root = basename ?? '';
return render(
<MemoryRouter basename={basename} initialEntries={[`${root}/studio`]}>
{preview()}
</MemoryRouter>,
);
}

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 <basename>/apps/<app>/ 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');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -288,6 +289,49 @@ function findFirstLandingAcross(trees: NavItem[][], docs = false): NavItem | und
return undefined;
}

/**
* A link that opens a ROUTER path (`/apps/<app>/…`) 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() ? (
<Link to={path} target="_blank" rel="noreferrer" onClick={onClick} className={className} title={title}>
{children}
</Link>
) : (
<a href={path} target="_blank" rel="noreferrer" onClick={onClick} className={className} title={title}>
{children}
</a>
);
}

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
Expand Down Expand Up @@ -364,15 +408,13 @@ export function AppPreview({ name, draft, editing, selection, onSelectionChange,
hint="app"
toolbar={
baseRuntimeUrl && (
<a
href={baseRuntimeUrl}
target="_blank"
rel="noreferrer"
<RuntimeLink
path={baseRuntimeUrl}
className="text-xs text-muted-foreground hover:text-foreground inline-flex items-center gap-1"
title={tr('engine.appPreview.openTitle', locale)}
>
{tr('engine.appPreview.open', locale)} <ExternalLink className="h-3 w-3" />
</a>
</RuntimeLink>
)
}
>
Expand Down Expand Up @@ -488,17 +530,24 @@ function NavRow({
{item.target && (
<span className="font-mono text-[10px] text-muted-foreground truncate">{item.target}</span>
)}
{url && (
<a
href={url}
target="_blank"
rel="noreferrer"
onClick={(e) => e.stopPropagation()}
className="ml-auto font-mono text-[10px] text-muted-foreground hover:text-foreground inline-flex items-center gap-1"
>
{url} <ExternalLink className="h-3 w-3" />
</a>
)}
{url &&
(item.external ? (
<a
href={url}
target="_blank"
rel="noreferrer"
onClick={(e) => e.stopPropagation()}
className={NAV_LINK_CLASS}
>
{url} <ExternalLink className="h-3 w-3" />
</a>
) : (
// A router path the shell resolved (`/apps/<app>/…`): it opens
// under the console's basename, as the toolbar's Open link does.
<RuntimeLink path={url} stopPropagation className={NAV_LINK_CLASS}>
{url} <ExternalLink className="h-3 w-3" />
</RuntimeLink>
))}
</div>
{item.children?.map((c, i) => (
<NavRow
Expand Down
Loading