feat(web): group onebox sidebar navigation
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## Pending
|
## Pending
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- group Onebox sidebar navigation into Apps, Network, and Registry sections (web)
|
||||||
|
- add parent/subview routes for grouped app, network, and registry pages
|
||||||
|
|
||||||
## 2026-05-21 - 1.26.3
|
## 2026-05-21 - 1.26.3
|
||||||
|
|
||||||
|
|||||||
+11
-2
@@ -64,6 +64,7 @@ export interface IAppStoreState {
|
|||||||
|
|
||||||
export interface IUiState {
|
export interface IUiState {
|
||||||
activeView: string;
|
activeView: string;
|
||||||
|
activeSubview: string | null;
|
||||||
autoRefresh: boolean;
|
autoRefresh: boolean;
|
||||||
refreshInterval: number;
|
refreshInterval: number;
|
||||||
pendingAppTemplate?: any;
|
pendingAppTemplate?: any;
|
||||||
@@ -161,6 +162,7 @@ export const uiStatePart = await appState.getStatePart<IUiState>(
|
|||||||
'ui',
|
'ui',
|
||||||
{
|
{
|
||||||
activeView: 'dashboard',
|
activeView: 'dashboard',
|
||||||
|
activeSubview: null,
|
||||||
autoRefresh: true,
|
autoRefresh: true,
|
||||||
refreshInterval: 30000,
|
refreshInterval: 30000,
|
||||||
},
|
},
|
||||||
@@ -1016,10 +1018,17 @@ export const setBackupPasswordAction = settingsStatePart.createAction<{ password
|
|||||||
// UI Actions
|
// UI Actions
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
export const setActiveViewAction = uiStatePart.createAction<{ view: string }>(
|
export const setActiveViewAction = uiStatePart.createAction<{ view: string; subview?: string | null }>(
|
||||||
async (statePartArg, dataArg) => {
|
async (statePartArg, dataArg) => {
|
||||||
const normalizedView = dataArg.view.toLowerCase().replace(/\s+/g, '-');
|
const normalizedView = dataArg.view.toLowerCase().replace(/\s+/g, '-');
|
||||||
return { ...statePartArg.getState(), activeView: normalizedView };
|
const normalizedSubview = dataArg.subview
|
||||||
|
? dataArg.subview.toLowerCase().replace(/\s+/g, '-')
|
||||||
|
: null;
|
||||||
|
return {
|
||||||
|
...statePartArg.getState(),
|
||||||
|
activeView: normalizedView,
|
||||||
|
activeSubview: normalizedSubview,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
+170
-57
@@ -12,14 +12,21 @@ import {
|
|||||||
type TemplateResult,
|
type TemplateResult,
|
||||||
} from '@design.estate/dees-element';
|
} from '@design.estate/dees-element';
|
||||||
|
|
||||||
import type { ObViewDashboard } from './ob-view-dashboard.js';
|
interface IUnresolvedView {
|
||||||
import type { ObViewServices } from './ob-view-services.js';
|
slug?: string;
|
||||||
import type { ObViewDomains } from './ob-view-domains.js';
|
name: string;
|
||||||
import type { ObViewDnsRecords } from './ob-view-dns-records.js';
|
iconName?: string;
|
||||||
import type { ObViewNetwork } from './ob-view-network.js';
|
element?: Promise<any>;
|
||||||
import type { ObViewRegistries } from './ob-view-registries.js';
|
subViews?: IUnresolvedView[];
|
||||||
import type { ObViewTokens } from './ob-view-tokens.js';
|
}
|
||||||
import type { ObViewSettings } from './ob-view-settings.js';
|
|
||||||
|
interface IResolvedView {
|
||||||
|
slug?: string;
|
||||||
|
name: string;
|
||||||
|
iconName?: string;
|
||||||
|
element?: any;
|
||||||
|
subViews?: IResolvedView[];
|
||||||
|
}
|
||||||
|
|
||||||
@customElement('ob-app-shell')
|
@customElement('ob-app-shell')
|
||||||
export class ObAppShell extends DeesElement {
|
export class ObAppShell extends DeesElement {
|
||||||
@@ -29,6 +36,7 @@ export class ObAppShell extends DeesElement {
|
|||||||
@state()
|
@state()
|
||||||
accessor uiState: appstate.IUiState = {
|
accessor uiState: appstate.IUiState = {
|
||||||
activeView: 'dashboard',
|
activeView: 'dashboard',
|
||||||
|
activeSubview: null,
|
||||||
autoRefresh: true,
|
autoRefresh: true,
|
||||||
refreshInterval: 30000,
|
refreshInterval: 30000,
|
||||||
};
|
};
|
||||||
@@ -39,27 +47,93 @@ export class ObAppShell extends DeesElement {
|
|||||||
@state()
|
@state()
|
||||||
accessor loginError: string = '';
|
accessor loginError: string = '';
|
||||||
|
|
||||||
private viewTabs = [
|
private viewTabs: IUnresolvedView[] = [
|
||||||
{ name: 'Dashboard', iconName: 'lucide:layoutDashboard', element: (async () => (await import('./ob-view-dashboard.js')).ObViewDashboard)() },
|
{
|
||||||
{ name: 'App Store', iconName: 'lucide:store', element: (async () => (await import('./ob-view-appstore.js')).ObViewAppStore)() },
|
slug: 'dashboard',
|
||||||
{ name: 'Services', iconName: 'lucide:boxes', element: (async () => (await import('./ob-view-services.js')).ObViewServices)() },
|
name: 'Dashboard',
|
||||||
{ name: 'Domains', iconName: 'lucide:globe', element: (async () => (await import('./ob-view-domains.js')).ObViewDomains)() },
|
iconName: 'lucide:layoutDashboard',
|
||||||
{ name: 'DNS Records', iconName: 'lucide:listTree', element: (async () => (await import('./ob-view-dns-records.js')).ObViewDnsRecords)() },
|
element: (async () => (await import('./ob-view-dashboard.js')).ObViewDashboard)(),
|
||||||
{ name: 'Network', iconName: 'lucide:network', element: (async () => (await import('./ob-view-network.js')).ObViewNetwork)() },
|
},
|
||||||
{ name: 'Registries', iconName: 'lucide:package', element: (async () => (await import('./ob-view-registries.js')).ObViewRegistries)() },
|
{
|
||||||
{ name: 'Tokens', iconName: 'lucide:key', element: (async () => (await import('./ob-view-tokens.js')).ObViewTokens)() },
|
slug: 'apps',
|
||||||
{ name: 'Settings', iconName: 'lucide:settings', element: (async () => (await import('./ob-view-settings.js')).ObViewSettings)() },
|
name: 'Apps',
|
||||||
|
iconName: 'lucide:store',
|
||||||
|
subViews: [
|
||||||
|
{
|
||||||
|
slug: 'app-store',
|
||||||
|
name: 'App Store',
|
||||||
|
iconName: 'lucide:store',
|
||||||
|
element: (async () => (await import('./ob-view-appstore.js')).ObViewAppStore)(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'services',
|
||||||
|
name: 'Services',
|
||||||
|
iconName: 'lucide:boxes',
|
||||||
|
element: (async () => (await import('./ob-view-services.js')).ObViewServices)(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'network',
|
||||||
|
name: 'Network',
|
||||||
|
iconName: 'lucide:network',
|
||||||
|
subViews: [
|
||||||
|
{
|
||||||
|
slug: 'proxy',
|
||||||
|
name: 'Proxy',
|
||||||
|
iconName: 'lucide:route',
|
||||||
|
element: (async () => (await import('./ob-view-network.js')).ObViewNetwork)(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'domains',
|
||||||
|
name: 'Domains',
|
||||||
|
iconName: 'lucide:globe',
|
||||||
|
element: (async () => (await import('./ob-view-domains.js')).ObViewDomains)(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'dns-records',
|
||||||
|
name: 'DNS Records',
|
||||||
|
iconName: 'lucide:listTree',
|
||||||
|
element: (async () => (await import('./ob-view-dns-records.js')).ObViewDnsRecords)(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'registry',
|
||||||
|
name: 'Registry',
|
||||||
|
iconName: 'lucide:package',
|
||||||
|
subViews: [
|
||||||
|
{
|
||||||
|
slug: 'registries',
|
||||||
|
name: 'Registries',
|
||||||
|
iconName: 'lucide:package',
|
||||||
|
element: (async () => (await import('./ob-view-registries.js')).ObViewRegistries)(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'tokens',
|
||||||
|
name: 'Tokens',
|
||||||
|
iconName: 'lucide:key',
|
||||||
|
element: (async () => (await import('./ob-view-tokens.js')).ObViewTokens)(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'settings',
|
||||||
|
name: 'Settings',
|
||||||
|
iconName: 'lucide:settings',
|
||||||
|
element: (async () => (await import('./ob-view-settings.js')).ObViewSettings)(),
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
private resolvedViewTabs: Array<{ name: string; iconName?: string; element: any }> = [];
|
private resolvedViewTabs: IResolvedView[] = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
document.title = 'Onebox';
|
document.title = 'Onebox';
|
||||||
|
|
||||||
const loginSubscription = appstate.loginStatePart
|
const loginSubscription = appstate.loginStatePart
|
||||||
.select((stateArg) => stateArg)
|
.select((stateArg: appstate.ILoginState) => stateArg)
|
||||||
.subscribe((loginState) => {
|
.subscribe((loginState: appstate.ILoginState) => {
|
||||||
this.loginState = loginState;
|
this.loginState = loginState;
|
||||||
if (loginState.isLoggedIn) {
|
if (loginState.isLoggedIn) {
|
||||||
appstate.systemStatePart.dispatchAction(appstate.fetchSystemStatusAction, null);
|
appstate.systemStatePart.dispatchAction(appstate.fetchSystemStatusAction, null);
|
||||||
@@ -68,15 +142,56 @@ export class ObAppShell extends DeesElement {
|
|||||||
this.rxSubscriptions.push(loginSubscription);
|
this.rxSubscriptions.push(loginSubscription);
|
||||||
|
|
||||||
const uiSubscription = appstate.uiStatePart
|
const uiSubscription = appstate.uiStatePart
|
||||||
.select((stateArg) => stateArg)
|
.select((stateArg: appstate.IUiState) => stateArg)
|
||||||
.subscribe((uiState) => {
|
.subscribe((uiState: appstate.IUiState) => {
|
||||||
this.uiState = uiState;
|
this.uiState = uiState;
|
||||||
this.syncAppdashView(uiState.activeView);
|
this.syncAppdashView(uiState.activeView, uiState.activeSubview);
|
||||||
});
|
});
|
||||||
this.rxSubscriptions.push(uiSubscription);
|
this.rxSubscriptions.push(uiSubscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static styles = [
|
private async resolveViewTabs(tabs: IUnresolvedView[]): Promise<IResolvedView[]> {
|
||||||
|
return Promise.all(
|
||||||
|
tabs.map(async (tab) => {
|
||||||
|
const resolvedTab: IResolvedView = {
|
||||||
|
slug: tab.slug,
|
||||||
|
name: tab.name,
|
||||||
|
iconName: tab.iconName,
|
||||||
|
};
|
||||||
|
if (tab.element) {
|
||||||
|
resolvedTab.element = await tab.element;
|
||||||
|
}
|
||||||
|
if (tab.subViews) {
|
||||||
|
resolvedTab.subViews = await this.resolveViewTabs(tab.subViews);
|
||||||
|
}
|
||||||
|
return resolvedTab;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private slugFor(view: IResolvedView): string {
|
||||||
|
return view.slug ?? view.name.toLowerCase().replace(/\s+/g, '-');
|
||||||
|
}
|
||||||
|
|
||||||
|
private findParent(view: IResolvedView): IResolvedView | undefined {
|
||||||
|
return this.resolvedViewTabs.find((viewTab) => viewTab.subViews?.includes(view));
|
||||||
|
}
|
||||||
|
|
||||||
|
private findViewBySlug(viewSlug: string, subviewSlug: string | null): IResolvedView | undefined {
|
||||||
|
const topLevelView = this.resolvedViewTabs.find((view) => this.slugFor(view) === viewSlug);
|
||||||
|
if (!topLevelView) return undefined;
|
||||||
|
if (subviewSlug && topLevelView.subViews) {
|
||||||
|
return topLevelView.subViews.find((subview) => this.slugFor(subview) === subviewSlug) ?? topLevelView;
|
||||||
|
}
|
||||||
|
return topLevelView;
|
||||||
|
}
|
||||||
|
|
||||||
|
private get currentViewTab(): IResolvedView | undefined {
|
||||||
|
if (this.resolvedViewTabs.length === 0) return undefined;
|
||||||
|
return this.findViewBySlug(this.uiState.activeView, this.uiState.activeSubview) ?? this.resolvedViewTabs[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static override styles = [
|
||||||
cssManager.defaultStyles,
|
cssManager.defaultStyles,
|
||||||
css`
|
css`
|
||||||
:host {
|
:host {
|
||||||
@@ -91,16 +206,14 @@ export class ObAppShell extends DeesElement {
|
|||||||
`,
|
`,
|
||||||
];
|
];
|
||||||
|
|
||||||
public render(): TemplateResult {
|
public override render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<div class="maincontainer">
|
<div class="maincontainer">
|
||||||
<dees-simple-login name="Onebox">
|
<dees-simple-login name="Onebox">
|
||||||
<dees-simple-appdash
|
<dees-simple-appdash
|
||||||
name="Onebox"
|
name="Onebox"
|
||||||
.viewTabs=${this.resolvedViewTabs}
|
.viewTabs=${this.resolvedViewTabs}
|
||||||
.selectedView=${this.resolvedViewTabs.find(
|
.selectedView=${this.currentViewTab}
|
||||||
(t) => t.name.toLowerCase().replace(/\s+/g, '-') === this.uiState.activeView
|
|
||||||
) || this.resolvedViewTabs[0]}
|
|
||||||
>
|
>
|
||||||
</dees-simple-appdash>
|
</dees-simple-appdash>
|
||||||
</dees-simple-login>
|
</dees-simple-login>
|
||||||
@@ -108,15 +221,8 @@ export class ObAppShell extends DeesElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async firstUpdated() {
|
public override async firstUpdated() {
|
||||||
// Resolve async view tab imports
|
this.resolvedViewTabs = await this.resolveViewTabs(this.viewTabs);
|
||||||
this.resolvedViewTabs = await Promise.all(
|
|
||||||
this.viewTabs.map(async (tab) => ({
|
|
||||||
name: tab.name,
|
|
||||||
iconName: tab.iconName,
|
|
||||||
element: await tab.element,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
this.requestUpdate();
|
this.requestUpdate();
|
||||||
await this.updateComplete;
|
await this.updateComplete;
|
||||||
|
|
||||||
@@ -130,34 +236,44 @@ export class ObAppShell extends DeesElement {
|
|||||||
const appDash = this.shadowRoot!.querySelector('dees-simple-appdash') as any;
|
const appDash = this.shadowRoot!.querySelector('dees-simple-appdash') as any;
|
||||||
if (appDash) {
|
if (appDash) {
|
||||||
appDash.addEventListener('view-select', (e: CustomEvent) => {
|
appDash.addEventListener('view-select', (e: CustomEvent) => {
|
||||||
const viewName = e.detail.view.name.toLowerCase().replace(/\s+/g, '-');
|
const view = e.detail.view as IResolvedView;
|
||||||
appRouter.navigateToView(viewName);
|
const parent = this.findParent(view);
|
||||||
|
const currentState = appstate.uiStatePart.getState();
|
||||||
|
if (parent) {
|
||||||
|
const parentSlug = this.slugFor(parent);
|
||||||
|
const subviewSlug = this.slugFor(view);
|
||||||
|
if (currentState.activeView === parentSlug && currentState.activeSubview === subviewSlug) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
appRouter.navigateToView(parentSlug, subviewSlug);
|
||||||
|
} else {
|
||||||
|
const slug = this.slugFor(view);
|
||||||
|
if (currentState.activeView === slug && !currentState.activeSubview) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
appRouter.navigateToView(slug);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
appDash.addEventListener('logout', async () => {
|
appDash.addEventListener('logout', async () => {
|
||||||
await appstate.loginStatePart.dispatchAction(appstate.logoutAction, null);
|
await appstate.loginStatePart.dispatchAction(appstate.logoutAction, null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the initial view on the appdash now that tabs are resolved
|
|
||||||
// Read activeView directly from state (not this.uiState which may be stale)
|
|
||||||
if (appDash && this.resolvedViewTabs.length > 0) {
|
if (appDash && this.resolvedViewTabs.length > 0) {
|
||||||
const currentActiveView = appstate.uiStatePart.getState().activeView;
|
const currentUiState = appstate.uiStatePart.getState();
|
||||||
const initialView = this.resolvedViewTabs.find(
|
const initialView =
|
||||||
(t) => t.name.toLowerCase().replace(/\s+/g, '-') === currentActiveView,
|
this.findViewBySlug(currentUiState.activeView, currentUiState.activeSubview) ||
|
||||||
) || this.resolvedViewTabs[0];
|
this.resolvedViewTabs[0];
|
||||||
await appDash.loadView(initialView);
|
await appDash.loadView(initialView);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for stored session (persistent login state)
|
|
||||||
const loginState = appstate.loginStatePart.getState();
|
const loginState = appstate.loginStatePart.getState();
|
||||||
if (loginState.identity?.jwt) {
|
if (loginState.identity?.jwt) {
|
||||||
if (loginState.identity.expiresAt > Date.now()) {
|
if (loginState.identity.expiresAt > Date.now()) {
|
||||||
// Switch to dashboard immediately (no flash of login form)
|
|
||||||
this.loginState = loginState;
|
this.loginState = loginState;
|
||||||
if (simpleLogin) {
|
if (simpleLogin) {
|
||||||
await simpleLogin.switchToSlottedContent();
|
await simpleLogin.switchToSlottedContent();
|
||||||
}
|
}
|
||||||
// Validate token with server in the background
|
|
||||||
try {
|
try {
|
||||||
const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
|
const typedRequest = new plugins.domtools.plugins.typedrequest.TypedRequest<
|
||||||
interfaces.requests.IReq_GetSystemStatus
|
interfaces.requests.IReq_GetSystemStatus
|
||||||
@@ -165,11 +281,9 @@ export class ObAppShell extends DeesElement {
|
|||||||
const response = await typedRequest.fire({ identity: loginState.identity });
|
const response = await typedRequest.fire({ identity: loginState.identity });
|
||||||
appstate.systemStatePart.setState({ status: response.status });
|
appstate.systemStatePart.setState({ status: response.status });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Token rejected by server - switch back to login
|
|
||||||
console.warn('Stored session invalid, returning to login:', err);
|
console.warn('Stored session invalid, returning to login:', err);
|
||||||
await appstate.loginStatePart.dispatchAction(appstate.logoutAction, null);
|
await appstate.loginStatePart.dispatchAction(appstate.logoutAction, null);
|
||||||
if (simpleLogin) {
|
if (simpleLogin) {
|
||||||
// Force page reload to show login properly
|
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,14 +324,13 @@ export class ObAppShell extends DeesElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private syncAppdashView(viewName: string): void {
|
private syncAppdashView(viewName: string, subviewName: string | null): void {
|
||||||
const appDash = this.shadowRoot?.querySelector('dees-simple-appdash') as any;
|
const appDash = this.shadowRoot?.querySelector('dees-simple-appdash') as any;
|
||||||
if (!appDash || this.resolvedViewTabs.length === 0) return;
|
if (!appDash || this.resolvedViewTabs.length === 0) return;
|
||||||
// Match kebab-case view name (e.g., 'app-store') to tab name (e.g., 'App Store')
|
|
||||||
const targetTab = this.resolvedViewTabs.find(
|
const targetTab = this.findViewBySlug(viewName, subviewName);
|
||||||
(t) => t.name.toLowerCase().replace(/\s+/g, '-') === viewName
|
if (!targetTab || appDash.selectedView === targetTab) return;
|
||||||
);
|
|
||||||
if (!targetTab) return;
|
|
||||||
appDash.loadView(targetTab);
|
appDash.loadView(targetTab);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+94
-24
@@ -3,12 +3,40 @@ import * as appstate from './appstate.js';
|
|||||||
|
|
||||||
const SmartRouter = plugins.domtools.plugins.smartrouter.SmartRouter;
|
const SmartRouter = plugins.domtools.plugins.smartrouter.SmartRouter;
|
||||||
|
|
||||||
export const validViews = [
|
const flatViews = ['dashboard', 'settings'] as const;
|
||||||
'dashboard', 'app-store', 'services', 'domains', 'dns-records', 'network',
|
|
||||||
'registries', 'tokens', 'settings',
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export type TValidView = typeof validViews[number];
|
const subviewMap: Record<string, readonly string[]> = {
|
||||||
|
apps: ['app-store', 'services'] as const,
|
||||||
|
network: ['proxy', 'domains', 'dns-records'] as const,
|
||||||
|
registry: ['registries', 'tokens'] as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultSubview: Record<string, string> = {
|
||||||
|
apps: 'app-store',
|
||||||
|
network: 'proxy',
|
||||||
|
registry: 'registries',
|
||||||
|
};
|
||||||
|
|
||||||
|
const legacySubviewTargetMap: Record<string, { view: string; subview: string }> = {
|
||||||
|
'app-store': { view: 'apps', subview: 'app-store' },
|
||||||
|
services: { view: 'apps', subview: 'services' },
|
||||||
|
proxy: { view: 'network', subview: 'proxy' },
|
||||||
|
domains: { view: 'network', subview: 'domains' },
|
||||||
|
'dns-records': { view: 'network', subview: 'dns-records' },
|
||||||
|
registries: { view: 'registry', subview: 'registries' },
|
||||||
|
tokens: { view: 'registry', subview: 'tokens' },
|
||||||
|
};
|
||||||
|
|
||||||
|
export const validTopLevelViews = [...flatViews, ...Object.keys(subviewMap)] as const;
|
||||||
|
export type TValidView = typeof validTopLevelViews[number];
|
||||||
|
|
||||||
|
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 {
|
class AppRouter {
|
||||||
private router: InstanceType<typeof SmartRouter>;
|
private router: InstanceType<typeof SmartRouter>;
|
||||||
@@ -28,24 +56,37 @@ class AppRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setupRoutes(): void {
|
private setupRoutes(): void {
|
||||||
for (const view of validViews) {
|
for (const view of flatViews) {
|
||||||
this.router.on(`/${view}`, async () => {
|
this.router.on(`/${view}`, async () => {
|
||||||
this.updateViewState(view);
|
this.updateViewState(view, null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root redirect
|
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.router.on('/', async () => {
|
||||||
this.navigateTo('/dashboard');
|
this.navigateTo('/dashboard');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private setupStateSync(): void {
|
private setupStateSync(): void {
|
||||||
appstate.uiStatePart.select((s) => s.activeView).subscribe((activeView) => {
|
appstate.uiStatePart.select().subscribe((uiState: appstate.IUiState) => {
|
||||||
if (this.suppressStateUpdate) return;
|
if (this.suppressStateUpdate) return;
|
||||||
|
|
||||||
const currentPath = window.location.pathname;
|
const currentPath = window.location.pathname;
|
||||||
const expectedPath = `/${activeView}`;
|
const expectedPath = uiState.activeSubview
|
||||||
|
? `/${uiState.activeView}/${uiState.activeSubview}`
|
||||||
|
: `/${uiState.activeView}`;
|
||||||
|
|
||||||
if (currentPath !== expectedPath) {
|
if (currentPath !== expectedPath) {
|
||||||
this.suppressStateUpdate = true;
|
this.suppressStateUpdate = true;
|
||||||
@@ -60,25 +101,37 @@ class AppRouter {
|
|||||||
|
|
||||||
if (!path || path === '/') {
|
if (!path || path === '/') {
|
||||||
this.router.pushUrl('/dashboard');
|
this.router.pushUrl('/dashboard');
|
||||||
} else {
|
return;
|
||||||
const segments = path.split('/').filter(Boolean);
|
}
|
||||||
const view = segments[0];
|
|
||||||
|
|
||||||
if (validViews.includes(view as TValidView)) {
|
const segments = path.split('/').filter(Boolean);
|
||||||
this.updateViewState(view as TValidView);
|
const view = segments[0];
|
||||||
|
const subview = segments[1];
|
||||||
|
|
||||||
|
if (!isValidView(view)) {
|
||||||
|
this.router.pushUrl('/dashboard');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subviewMap[view]) {
|
||||||
|
if (subview && isValidSubview(view, subview)) {
|
||||||
|
this.updateViewState(view, subview);
|
||||||
} else {
|
} else {
|
||||||
this.router.pushUrl('/dashboard');
|
this.router.pushUrl(`/${view}/${defaultSubview[view]}`);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.updateViewState(view, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateViewState(view: string): void {
|
private updateViewState(view: string, subview: string | null): void {
|
||||||
this.suppressStateUpdate = true;
|
this.suppressStateUpdate = true;
|
||||||
const currentState = appstate.uiStatePart.getState();
|
const currentState = appstate.uiStatePart.getState();
|
||||||
if (currentState.activeView !== view) {
|
if (currentState.activeView !== view || currentState.activeSubview !== subview) {
|
||||||
appstate.uiStatePart.setState({
|
appstate.uiStatePart.setState({
|
||||||
...currentState,
|
...currentState,
|
||||||
activeView: view,
|
activeView: view,
|
||||||
|
activeSubview: subview,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.suppressStateUpdate = false;
|
this.suppressStateUpdate = false;
|
||||||
@@ -88,17 +141,34 @@ class AppRouter {
|
|||||||
this.router.pushUrl(path);
|
this.router.pushUrl(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public navigateToView(view: string): void {
|
public navigateToView(view: string, subview?: string): void {
|
||||||
const normalized = view.toLowerCase().replace(/\s+/g, '-');
|
const normalizedView = view.toLowerCase().replace(/\s+/g, '-');
|
||||||
if (validViews.includes(normalized as TValidView)) {
|
const normalizedSubview = subview?.toLowerCase().replace(/\s+/g, '-');
|
||||||
this.navigateTo(`/${normalized}`);
|
|
||||||
} else {
|
if (!isValidView(normalizedView)) {
|
||||||
|
const legacyTarget = legacySubviewTargetMap[normalizedView];
|
||||||
|
if (legacyTarget) {
|
||||||
|
this.navigateToView(legacyTarget.view, legacyTarget.subview);
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.navigateTo('/dashboard');
|
this.navigateTo('/dashboard');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalizedSubview && isValidSubview(normalizedView, normalizedSubview)) {
|
||||||
|
this.navigateTo(`/${normalizedView}/${normalizedSubview}`);
|
||||||
|
} else if (subviewMap[normalizedView]) {
|
||||||
|
this.navigateTo(`/${normalizedView}/${defaultSubview[normalizedView]}`);
|
||||||
|
} else {
|
||||||
|
this.navigateTo(`/${normalizedView}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCurrentView(): string {
|
public getCurrentView(): string {
|
||||||
return appstate.uiStatePart.getState().activeView;
|
const uiState = appstate.uiStatePart.getState();
|
||||||
|
return uiState.activeSubview
|
||||||
|
? `${uiState.activeView}/${uiState.activeSubview}`
|
||||||
|
: uiState.activeView;
|
||||||
}
|
}
|
||||||
|
|
||||||
public destroy(): void {
|
public destroy(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user