2026-02-02 00:36:19 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
import * as appstate from './appstate.js';
|
|
|
|
|
|
|
|
|
|
const SmartRouter = plugins.domtools.plugins.smartrouter.SmartRouter;
|
|
|
|
|
|
2026-02-16 11:25:16 +00:00
|
|
|
export const validViews = ['overview', 'network', 'emails', 'logs', 'configuration', 'security', 'certificates', 'remoteingress'] as const;
|
2026-02-02 00:36:19 +00:00
|
|
|
|
|
|
|
|
export type TValidView = typeof validViews[number];
|
|
|
|
|
|
|
|
|
|
class AppRouter {
|
|
|
|
|
private router: InstanceType<typeof SmartRouter>;
|
|
|
|
|
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 validViews) {
|
2026-02-22 00:45:01 +00:00
|
|
|
this.router.on(`/${view}`, async () => {
|
|
|
|
|
this.updateViewState(view);
|
|
|
|
|
});
|
2026-02-02 00:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Root redirect
|
|
|
|
|
this.router.on('/', async () => {
|
|
|
|
|
this.navigateTo('/overview');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setupStateSync(): void {
|
|
|
|
|
appstate.uiStatePart.state.subscribe((uiState) => {
|
|
|
|
|
if (this.suppressStateUpdate) return;
|
|
|
|
|
|
|
|
|
|
const currentPath = window.location.pathname;
|
2026-02-22 00:45:01 +00:00
|
|
|
const expectedPath = `/${uiState.activeView}`;
|
2026-02-02 00:36:19 +00:00
|
|
|
|
2026-02-22 00:45:01 +00:00
|
|
|
if (currentPath !== expectedPath) {
|
2026-02-02 00:36:19 +00:00
|
|
|
this.suppressStateUpdate = true;
|
2026-02-22 00:45:01 +00:00
|
|
|
this.router.pushUrl(expectedPath);
|
2026-02-02 00:36:19 +00:00
|
|
|
this.suppressStateUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleInitialRoute(): void {
|
|
|
|
|
const path = window.location.pathname;
|
|
|
|
|
|
|
|
|
|
if (!path || path === '/') {
|
|
|
|
|
this.router.pushUrl('/overview');
|
|
|
|
|
} else {
|
|
|
|
|
const segments = path.split('/').filter(Boolean);
|
|
|
|
|
const view = segments[0];
|
|
|
|
|
|
|
|
|
|
if (validViews.includes(view as TValidView)) {
|
|
|
|
|
this.updateViewState(view as TValidView);
|
|
|
|
|
} else {
|
|
|
|
|
this.router.pushUrl('/overview');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateViewState(view: string): void {
|
|
|
|
|
this.suppressStateUpdate = true;
|
|
|
|
|
const currentState = appstate.uiStatePart.getState();
|
|
|
|
|
if (currentState.activeView !== view) {
|
|
|
|
|
appstate.uiStatePart.setState({
|
|
|
|
|
...currentState,
|
|
|
|
|
activeView: view,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
this.suppressStateUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public navigateTo(path: string): void {
|
|
|
|
|
this.router.pushUrl(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public navigateToView(view: string): void {
|
|
|
|
|
if (validViews.includes(view as TValidView)) {
|
|
|
|
|
this.navigateTo(`/${view}`);
|
|
|
|
|
} else {
|
|
|
|
|
this.navigateTo('/overview');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getCurrentView(): string {
|
|
|
|
|
return appstate.uiStatePart.getState().activeView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
this.router.destroy();
|
|
|
|
|
this.initialized = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const appRouter = new AppRouter();
|