import * as plugins from './plugins.js'; import * as appstate from './appstate.js'; const SmartRouter = plugins.deesDomtools.plugins.smartrouter.SmartRouter; const flatViews = ['overview', 'logs'] as const; const subviewMap: Record = { platform: ['settings', 'baseos', 'fleet'] as const, runtime: ['clusters', 'services', 'images', 'deployments', 'tasks'] as const, registry: ['externalregistries', 'testing'] as const, secrets: ['secretgroups', 'secretbundles'] as const, domains: ['domains', 'dns', 'mails'] as const, storage: ['s3', 'dbs', 'backups'] as const, }; const defaultSubview: Record = { platform: 'settings', runtime: 'clusters', registry: 'externalregistries', secrets: 'secretgroups', domains: 'domains', storage: 's3', }; export const validTopLevelViews = [...flatViews, ...Object.keys(subviewMap)] as const; export function isValidView(view: string): boolean { return (validTopLevelViews as readonly string[]).includes(view); } export function isValidSubview(view: string, subview: string): boolean { return subviewMap[view]?.includes(subview) ?? false; } class AppRouter { private router: InstanceType; private initialized = false; private suppressStateUpdate = false; constructor() { this.router = new SmartRouter({ debug: false }); } public init(): void { if (this.initialized) return; this.setupRoutes(); this.setupStateSync(); this.handleInitialRoute(); this.initialized = true; } private setupRoutes(): void { for (const view of flatViews) { this.router.on(`/${view}`, async () => { this.updateViewState(view, null); }); } for (const view of Object.keys(subviewMap)) { this.router.on(`/${view}`, async () => { this.navigateTo(`/${view}/${defaultSubview[view]}`); }); for (const subview of subviewMap[view]) { this.router.on(`/${view}/${subview}`, async () => { this.updateViewState(view, subview); }); } } this.router.on('/', async () => { this.navigateTo('/overview'); }); } private setupStateSync(): void { appstate.uiStatePart.select().subscribe((uiState) => { if (this.suppressStateUpdate) return; const currentPath = window.location.pathname; const expectedPath = uiState.activeSubview ? `/${uiState.activeView}/${uiState.activeSubview}` : `/${uiState.activeView}`; if (currentPath !== expectedPath) { this.suppressStateUpdate = true; this.router.pushUrl(expectedPath); this.suppressStateUpdate = false; } }); } private handleInitialRoute(): void { const path = window.location.pathname; if (!path || path === '/') { this.router.pushUrl('/overview'); return; } const segments = path.split('/').filter(Boolean); const view = segments[0]; const subview = segments[1]; if (!isValidView(view)) { this.router.pushUrl('/overview'); return; } if (subviewMap[view]) { if (subview && isValidSubview(view, subview)) { this.updateViewState(view, subview); } else { this.router.pushUrl(`/${view}/${defaultSubview[view]}`); } } else { this.updateViewState(view, null); } } private updateViewState(view: string, subview: string | null): void { this.suppressStateUpdate = true; const currentState = appstate.uiStatePart.getState()!; if (currentState.activeView !== view || currentState.activeSubview !== subview) { appstate.uiStatePart.setState({ ...currentState, activeView: view, activeSubview: subview, }); } this.suppressStateUpdate = false; } public navigateTo(path: string): void { this.router.pushUrl(path); } public navigateToView(view: string, subview?: string): void { if (!isValidView(view)) { this.navigateTo('/overview'); return; } if (subview && isValidSubview(view, subview)) { this.navigateTo(`/${view}/${subview}`); } else if (subviewMap[view]) { this.navigateTo(`/${view}/${defaultSubview[view]}`); } else { this.navigateTo(`/${view}`); } } public destroy(): void { this.router.destroy(); this.initialized = false; } } export const appRouter = new AppRouter();