feat: add routable browser URLs for stacks and shell views (#1586)

* feat: add routable browser URLs for stacks and shell views

Sync in-memory navigation to the address bar via a History API hook so
deep links, refresh, Back/Forward, and bookmarks work across nodes,
views, stack editor tabs, and mobile surfaces. Gate role/tier URL
normalization on permissions and license readiness, preserve URLs on
metadata fetch failure, and surface retryable stack-list errors without
rewriting pending stack paths.

* fix: preserve deep-link views on cold load and refresh

Stop the node-switch effect from resetting to dashboard on initial mount.

Defer URL writer settlement until hydrated activeView matches the route.

Adds E2E coverage for shell cold loads, stack refresh, and compose env tab.

* fix: keep mobile dashboard on list surface so sidebar renders

On mobile, the URL sync hook was routing /nodes/<slug>/dashboard to the
content surface, hiding the stack list sidebar. This prevented the
data-stacks-loaded sentinel from appearing, causing sidebar truncation
E2E tests to time out after reload on a mobile viewport.

Mobile dashboard now stays on the list surface; other non-editor views
still render on the content surface.

* fix: complete mobile URL routing follow-ups for stack deep links

Restore mobile /dashboard vs /stacks, list surface always writes /stacks.

Hydrate pendingDetailStack, freeze compose failures with routeDetailError,

and add unit plus E2E coverage.

* fix: hydrate shell views from URL and sync in-app navigation

Bootstrap activeView and tab state from the pathname on cold load.

Settle route phase when state already matches, normalize unknown segments,

and open Monaco editor tabs from stack deep links via applyEditorRouteState.

* fix: prevent mobile stack deep links from hanging on cold load

The resolvePendingStack effect did not re-fire when the pending stack ref
was populated during URL hydration, because the urlHydratingStack state
set in the same callback was not listed in the effect's dependency array.
Adding it causes the effect to retry once hydration has committed.

A resolvingRef mutex prevents concurrent invocations. When the target file
is already loaded, route state is applied directly without calling
loadFileForRoute, which avoids unmounting the editor (and hiding the
recovery chip) if a background refresh triggers route resolution during
a deploy operation.

* test: adapt stack, deploy, and sidebar e2e specs to routable stack URLs

* ci: raise E2E Playwright job timeout to 20 minutes
This commit is contained in:
Anso
2026-07-08 13:07:16 -04:00
committed by GitHub
parent 0c37d18586
commit 5fe0843eb2
35 changed files with 2263 additions and 199 deletions
+189
View File
@@ -0,0 +1,189 @@
import type { FleetTab, SecurityTab } from '@/lib/events';
import type { SectionId } from '@/components/settings/types';
import type { ActiveView, EditorTab, ParsedRoute, RouteState } from './routeTypes';
const VIEW_SEGMENTS = {
dashboard: 'dashboard',
editor: 'stacks',
'host-console': 'host-console',
resources: 'resources',
templates: 'templates',
'global-observability': 'logs',
fleet: 'fleet',
security: 'security',
'audit-log': 'audit',
'scheduled-ops': 'schedules',
'auto-updates': 'updates',
settings: 'settings',
} as const satisfies Record<ActiveView, string>;
const SEGMENT_TO_VIEW: Record<string, ActiveView> = Object.fromEntries(
Object.entries(VIEW_SEGMENTS).map(([view, seg]) => [seg, view as ActiveView]),
) as Record<string, ActiveView>;
const EDITOR_TABS = new Set<EditorTab>(['compose', 'env', 'files']);
const SECURITY_TABS = new Set<SecurityTab>([
'overview', 'images', 'compose', 'secrets', 'policies', 'suppressions', 'history', 'scanner',
]);
const FLEET_TABS = new Set<FleetTab>([
'overview', 'snapshots', 'configuration', 'dependencies', 'container-labels',
'deployments', 'routing', 'federation', 'actions', 'secrets',
]);
const MAX_QUERY_LEN = 512;
function normalizePathname(pathname: string): string {
const trimmed = pathname.replace(/\/+$/, '') || '/';
return trimmed.toLowerCase();
}
function parseBoundedPositiveInt(raw: string | null): number | null {
if (!raw || raw.length > 12) return null;
if (!/^\d+$/.test(raw)) return null;
const n = Number(raw);
if (!Number.isSafeInteger(n) || n <= 0) return null;
return n;
}
function safeDecodeQueryValue(raw: string): string | null {
if (raw.length > MAX_QUERY_LEN) return null;
try {
return decodeURIComponent(raw);
} catch {
return null;
}
}
export function parsePath(pathname: string, search: string): ParsedRoute {
const empty: ParsedRoute = {
nodeSlug: null,
view: null,
stackName: null,
editorTab: null,
envFile: null,
securityTab: null,
fleetTab: null,
settingsSection: null,
filterNodeId: null,
isStackList: false,
};
const path = normalizePathname(pathname);
if (path === '/') return empty;
const parts = path.split('/').filter(Boolean);
if (parts[0] !== 'nodes' || parts.length < 3) return empty;
const nodeSlug = parts[1];
const segment = parts[2];
let params: URLSearchParams;
try {
params = new URLSearchParams(search.startsWith('?') ? search.slice(1) : search);
} catch {
return { ...empty, nodeSlug };
}
const filterNodeId = parseBoundedPositiveInt(params.get('node'));
const envFile = safeDecodeQueryValue(params.get('env') ?? '');
if (segment === 'stacks') {
if (parts.length === 3) {
return { ...empty, nodeSlug, view: 'dashboard', isStackList: true };
}
const stackName = parts[3];
const tabRaw = parts[4]?.toLowerCase();
const editorTab = tabRaw && EDITOR_TABS.has(tabRaw as EditorTab)
? (tabRaw as EditorTab)
: 'compose';
return {
...empty,
nodeSlug,
view: 'editor',
stackName,
editorTab,
envFile: envFile || null,
filterNodeId,
};
}
const view = SEGMENT_TO_VIEW[segment];
if (!view) return { ...empty, nodeSlug };
const result: ParsedRoute = { ...empty, nodeSlug, view, filterNodeId };
if (view === 'security' && parts[3]) {
const tab = parts[3].toLowerCase();
if (SECURITY_TABS.has(tab as SecurityTab)) result.securityTab = tab as SecurityTab;
}
if (view === 'fleet' && parts[3]) {
const tab = parts[3].toLowerCase();
if (FLEET_TABS.has(tab as FleetTab)) result.fleetTab = tab as FleetTab;
}
if (view === 'settings' && parts[3]) {
result.settingsSection = parts[3] as SectionId;
}
if (view === 'host-console' && parts[3]) {
result.stackName = parts[3];
}
return result;
}
export function buildPath(state: RouteState): string {
const base = `/nodes/${encodeURIComponent(state.nodeSlug)}`;
const url = new URL('http://local');
if (state.activeView === 'editor' && state.stackName) {
const tab = state.editorTab || 'compose';
url.pathname = `${base}/stacks/${encodeURIComponent(state.stackName)}/${tab}`;
if (tab === 'env' && state.envFile) {
url.searchParams.set('env', state.envFile);
}
if (state.filterNodeId != null) {
url.searchParams.set('node', String(state.filterNodeId));
}
return url.pathname + url.search;
}
if (state.isMobile && state.mobileSurface === 'list') {
url.pathname = `${base}/stacks`;
return url.pathname;
}
if (state.activeView === 'dashboard') {
url.pathname = `${base}/dashboard`;
return url.pathname;
}
const segment = VIEW_SEGMENTS[state.activeView];
url.pathname = `${base}/${segment}`;
if (state.activeView === 'security' && state.securityTab !== 'overview') {
url.pathname += `/${state.securityTab}`;
}
if (state.activeView === 'fleet' && state.fleetActiveTab !== 'overview') {
if (!state.isMobile) {
url.pathname += `/${state.fleetActiveTab}`;
}
}
if (state.activeView === 'settings') {
const section = state.isMobile
? state.settingsSection
: (state.settingsSection ?? 'appearance');
if (section) {
url.pathname += `/${section}`;
}
}
if (state.activeView === 'host-console' && state.stackName) {
url.pathname += `/${encodeURIComponent(state.stackName)}`;
}
if (state.activeView === 'scheduled-ops' && state.filterNodeId != null) {
url.searchParams.set('node', String(state.filterNodeId));
}
return url.pathname + url.search;
}
export { VIEW_SEGMENTS, SEGMENT_TO_VIEW };