web: further refinements to the sidebar
This commit restores the onHashChange functionality, using an on-demand reverse map (there really aren't that many objects in the nav tree) to make sure all of the parent entities are also listed in the "expanded" listing to make sure the target object is still visible. Along the way, several type lever errors were corrected. Two major pieces of functionality were extracted from the Sidebar function as they're mostly consumers/filters of the information provided, and don't need to be in the Sidebar itself.
This commit is contained in:
46
web/src/elements/sidebar/utils.ts
Normal file
46
web/src/elements/sidebar/utils.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { ROUTE_SEPARATOR } from "@goauthentik/common/constants";
|
||||
|
||||
import { SidebarEntry } from "./types";
|
||||
|
||||
export function entryKey(entry: SidebarEntry) {
|
||||
return `${entry.path || "no-path"}:${entry.label}`;
|
||||
}
|
||||
|
||||
export function makeParentMap(entries: SidebarEntry[]) {
|
||||
const reverseMap = new WeakMap<SidebarEntry, SidebarEntry>();
|
||||
function reverse(entry: SidebarEntry) {
|
||||
(entry.children ?? []).forEach((e) => {
|
||||
reverseMap.set(e, entry);
|
||||
reverse(e);
|
||||
});
|
||||
}
|
||||
entries.forEach(reverse);
|
||||
return reverseMap;
|
||||
}
|
||||
|
||||
function scanner(entry: SidebarEntry, activePath: string): SidebarEntry | undefined {
|
||||
for (const matcher of entry.attributes?.activeWhen ?? []) {
|
||||
const matchtest = new RegExp(matcher);
|
||||
if (matchtest.test(activePath)) {
|
||||
return entry;
|
||||
}
|
||||
const match: SidebarEntry | undefined = (entry.children ?? []).find((e) =>
|
||||
scanner(e, activePath),
|
||||
);
|
||||
if (match) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function findMatchForNavbarUrl(entries: SidebarEntry[]) {
|
||||
const activePath = window.location.hash.slice(1, Infinity).split(ROUTE_SEPARATOR)[0];
|
||||
for (const entry of entries) {
|
||||
const result = scanner(entry, activePath);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user