Skip to content

Commit a12dbac

Browse files
committed
refactor: change SubrouterPathDynamicsSettings from type to interface and enhance links handling
1 parent b5043e8 commit a12dbac

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

app/composables/useSubrouterPathDynamics.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export namespace UseSubrouterPathDynamics {
2525
[path: string]: SubrouterPathDynamicsValue;
2626
}
2727

28-
export type SubrouterPathDynamicsSettings = {
28+
export interface SubrouterPathDynamicsSettings {
2929
baseTitle: string;
3030
basebreadcrumbItems?: BreadcrumbItem[];
3131
/**
@@ -44,27 +44,41 @@ export namespace UseSubrouterPathDynamics {
4444
}
4545
}
4646

47-
class SubrouterPathDynamics {
47+
class SubrouterPathDynamics<Settings extends UseSubrouterPathDynamics.SubrouterPathDynamicsSettings> {
4848

49-
readonly links: Ref<NavigationMenuItem[][]>;
49+
readonly links: Settings["routes"] extends Ref<any> ? Ref<NavigationMenuItem[][]> : NavigationMenuItem[][];
5050

5151
constructor(
52-
private readonly options: UseSubrouterPathDynamics.SubrouterPathDynamicsSettings
52+
private readonly options: Settings
5353
) {
5454

55-
this.links = computed(() => {
55+
if (isRef(this.options.routes)) {
56+
//@ts-ignore
57+
this.links = computed(() => {
58+
const links: NavigationMenuItem[] = [];
59+
for (const [path, dynamics] of Object.entries(this.options.routes.value)) {
60+
if (dynamics.isNavLink) {
61+
links.push({
62+
...dynamics,
63+
to: path
64+
});
65+
}
66+
}
67+
return [links];
68+
});
69+
} else {
5670
const links: NavigationMenuItem[] = [];
57-
for (const [path, dynamics] of Object.entries(isRef(this.options.routes) ? this.options.routes.value : this.options.routes)) {
71+
for (const [path, dynamics] of Object.entries(options.routes)) {
5872
if (dynamics.isNavLink) {
5973
links.push({
6074
...dynamics,
6175
to: path
6276
});
6377
}
6478
}
65-
return [links];
66-
})
67-
79+
//@ts-ignore
80+
this.links = [links];
81+
}
6882
}
6983

7084
async getPathDynamicValues(path: string): Promise<Promise<UseSubrouterPathDynamics.RichPathDynamicValues>> {
@@ -89,6 +103,6 @@ class SubrouterPathDynamics {
89103

90104
}
91105

92-
export function useSubrouterPathDynamics(options: UseSubrouterPathDynamics.SubrouterPathDynamicsSettings) {
93-
return new SubrouterPathDynamics(options);
106+
export function useSubrouterPathDynamics<Settings extends UseSubrouterPathDynamics.SubrouterPathDynamicsSettings>(options: Settings) {
107+
return new SubrouterPathDynamics<Settings>(options);
94108
}

0 commit comments

Comments
 (0)