- sidebarNavigation.ts: static SIDEBAR_SECTIONS registry (i18n labelKey) + getTogglableFeatureNodes() with per-flag dedup - projectSections.json: shared feature-id manifest; vitest guard pins nav ids to it, pytest guard pins it to FeaturesConfig fields - FeaturesSettings: toggles derived and grouped by sidebar section/category, extra block for non-nav flags - features.svelte.ts: reactive flags store; sidebar rebuilds live after settings save, untracked health polling
419 lines
18 KiB
TypeScript
419 lines
18 KiB
TypeScript
// #region Layout.SidebarNavigation.PageContracts [C:2] [TYPE Interface]
|
|
// @BRIEF Contract implemented by all Svelte route pages — defines page-level behavior and routing structure.
|
|
// #endregion Layout.SidebarNavigation.PageContracts
|
|
|
|
// #region Layout.SidebarNavigation.NavigationContracts [C:2] [TYPE Interface]
|
|
// @BRIEF Contract for navigation structure — sidebar sections, route categories, and menu item hierarchy.
|
|
// #endregion Layout.SidebarNavigation.NavigationContracts
|
|
|
|
// #region Layout.SidebarNavigation.SidebarNavigationModule [TYPE Function]
|
|
// @SEMANTICS: navigation, sidebar, rbac, menu, filtering
|
|
// @BRIEF: Build sidebar navigation sections and categories filtered by user permissions and feature flags.
|
|
// @LAYER UI
|
|
// @RELATION DEPENDS_ON -> [Auth.Permissions.PermissionsModuleModule]
|
|
// @RELATION BINDS_TO -> [Layout.Sidebar]
|
|
// @RELATION BINDS_TO -> [Settings.FeaturesSettings]
|
|
// @INVARIANT: Admin role can access all categories and subitems through permission utility.
|
|
// @INVARIANT: Every requiredFeature in SIDEBAR_SECTIONS is listed in projectSections.json and has a
|
|
// matching FeaturesConfig field on the backend (enforced by Tests.ProjectSections.Guard
|
|
// and the backend manifest guard test). The settings "Разделы проекта" tab derives its
|
|
// toggles from the same registry, so a new gated route appears there automatically.
|
|
|
|
import { hasPermission } from "$lib/auth/permissions.js";
|
|
import projectSections from "./projectSections.json";
|
|
|
|
/**
|
|
* Feature ids that gate sidebar routes. The id list lives in projectSections.json and is the
|
|
* single source shared with the backend manifest guard (FeaturesConfig field check).
|
|
*/
|
|
export type NavFeatureId = (typeof projectSections.navFeatures)[number];
|
|
|
|
interface NavItemDef {
|
|
labelKey: string;
|
|
labelFallback?: string;
|
|
path: string;
|
|
requiredPermission?: string;
|
|
requiredAction?: string;
|
|
requiredFeature?: NavFeatureId;
|
|
}
|
|
|
|
interface CategoryDef {
|
|
id: string;
|
|
labelKey: string;
|
|
labelFallback?: string;
|
|
icon: string;
|
|
tone: string;
|
|
path: string;
|
|
requiredPermission?: string;
|
|
requiredAction?: string;
|
|
requiredFeature?: NavFeatureId;
|
|
subItems: NavItemDef[];
|
|
}
|
|
|
|
interface SectionDef {
|
|
id: string;
|
|
labelKey: string;
|
|
categories: CategoryDef[];
|
|
}
|
|
|
|
interface NavItem {
|
|
label: string;
|
|
path: string;
|
|
requiredPermission?: string;
|
|
requiredAction?: string;
|
|
requiredFeature?: string;
|
|
}
|
|
|
|
interface Category {
|
|
id: string;
|
|
label: string;
|
|
icon: string;
|
|
tone: string;
|
|
path: string;
|
|
requiredPermission?: string;
|
|
requiredAction?: string;
|
|
requiredFeature?: string;
|
|
subItems: NavItem[];
|
|
}
|
|
|
|
interface Section {
|
|
id: string;
|
|
label: string;
|
|
categories: Category[];
|
|
}
|
|
|
|
interface I18nNavState {
|
|
nav?: Record<string, string>;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
interface UserRole {
|
|
name?: string;
|
|
permissions?: unknown[];
|
|
}
|
|
|
|
interface User {
|
|
roles?: UserRole[];
|
|
}
|
|
|
|
// #region Layout.SidebarNavigation.SidebarSectionsRegistry [TYPE Data]
|
|
// @BRIEF Static navigation registry — single source for the sidebar structure and the settings
|
|
// feature toggles (getTogglableFeatureNodes). Labels are stored as i18n keys and resolved
|
|
// at build time so the registry itself stays locale-independent.
|
|
const SIDEBAR_SECTIONS: SectionDef[] = [
|
|
// ── Section 1: Resources ─────────────────────────────────
|
|
{
|
|
id: "resources",
|
|
labelKey: "section_resources",
|
|
categories: [
|
|
{
|
|
id: "dashboards",
|
|
labelKey: "dashboards",
|
|
icon: "dashboard",
|
|
tone: "from-category-dashboards-from to-category-dashboards-to text-category-dashboards-text ring-category-dashboards-ring",
|
|
path: "/dashboards",
|
|
requiredPermission: "plugin:migration",
|
|
requiredAction: "READ",
|
|
subItems: [
|
|
{ labelKey: "overview", path: "/dashboards", requiredPermission: "plugin:migration", requiredAction: "READ" },
|
|
{ labelKey: "health_center", path: "/dashboards/health", requiredPermission: "plugin:migration", requiredAction: "READ", requiredFeature: "health_monitor" },
|
|
],
|
|
},
|
|
{
|
|
id: "datasets",
|
|
labelKey: "datasets",
|
|
icon: "database",
|
|
tone: "from-category-datasets-from to-category-datasets-to text-category-datasets-text ring-category-datasets-ring",
|
|
path: "/datasets",
|
|
requiredPermission: "plugin:migration",
|
|
requiredAction: "READ",
|
|
subItems: [
|
|
{ labelKey: "all_datasets", path: "/datasets", requiredPermission: "plugin:migration", requiredAction: "READ" },
|
|
],
|
|
},
|
|
{
|
|
id: "migration",
|
|
labelKey: "migration",
|
|
icon: "layers",
|
|
tone: "from-category-migration-from to-category-migration-to text-category-migration-text ring-category-migration-ring",
|
|
path: "/migration",
|
|
requiredPermission: "plugin:migration",
|
|
requiredAction: "READ",
|
|
requiredFeature: "migration",
|
|
subItems: [
|
|
{ labelKey: "migration_overview", labelFallback: "Overview", path: "/migration", requiredPermission: "plugin:migration", requiredAction: "READ", requiredFeature: "migration" },
|
|
{ labelKey: "migration_mappings", labelFallback: "Mappings", path: "/migration/mappings", requiredPermission: "plugin:migration", requiredAction: "READ", requiredFeature: "migration" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
|
|
// ── Section 2: Operations ──────────────────────────────────
|
|
{
|
|
id: "operations",
|
|
labelKey: "section_operations",
|
|
categories: [
|
|
{
|
|
id: "git",
|
|
labelKey: "git",
|
|
icon: "activity",
|
|
tone: "from-category-git-from to-category-git-to text-category-git-text ring-category-git-ring",
|
|
path: "/git",
|
|
requiredFeature: "git_integration",
|
|
subItems: [
|
|
{ labelKey: "git_repo_status", labelFallback: "Repository Status", path: "/git", requiredFeature: "git_integration" },
|
|
],
|
|
},
|
|
{
|
|
id: "translation",
|
|
labelKey: "translation",
|
|
icon: "translate",
|
|
tone: "from-category-translation-from to-category-translation-to text-category-translation-text ring-category-translation-ring",
|
|
path: "/translate",
|
|
requiredPermission: "translate.job",
|
|
requiredAction: "VIEW",
|
|
requiredFeature: "translate",
|
|
subItems: [
|
|
{ labelKey: "translation_jobs", path: "/translate", requiredPermission: "translate.job", requiredAction: "VIEW", requiredFeature: "translate" },
|
|
{ labelKey: "translation_dictionaries", path: "/translate/dictionaries", requiredPermission: "translate.dictionary", requiredAction: "VIEW", requiredFeature: "translate_dictionaries" },
|
|
{ labelKey: "translation_history", path: "/translate/history", requiredPermission: "translate.job", requiredAction: "VIEW", requiredFeature: "translate" },
|
|
],
|
|
},
|
|
{
|
|
id: "validation",
|
|
labelKey: "validation",
|
|
icon: "activity",
|
|
tone: "from-category-validation-from to-category-validation-to text-category-validation-text ring-category-validation-ring",
|
|
path: "/validation-tasks",
|
|
requiredPermission: "validation.task",
|
|
requiredAction: "VIEW",
|
|
requiredFeature: "llm_dashboard_validation",
|
|
subItems: [
|
|
{ labelKey: "validation_tasks", path: "/validation-tasks", requiredPermission: "validation.task", requiredAction: "VIEW", requiredFeature: "llm_dashboard_validation" },
|
|
{ labelKey: "validation_history", path: "/validation-tasks/history", requiredPermission: "validation.run", requiredAction: "VIEW", requiredFeature: "llm_dashboard_validation" },
|
|
],
|
|
},
|
|
{
|
|
// 042/044/045/046/047 dashboard-testing workspace. No category-level permission:
|
|
// visibility is derived from the RBAC-gated subitems (registry read / run / result /
|
|
// automation). Each subitem mirrors its exact backend has_permission gate so the nav
|
|
// never advertises a route the user cannot open. The runs hub is the human approval loop.
|
|
id: "dashboard_testing",
|
|
labelKey: "dashboard_testing",
|
|
icon: "clipboard",
|
|
tone: "from-category-testing-from to-category-testing-to text-category-testing-text ring-category-testing-ring",
|
|
path: "/dashboard-testing",
|
|
subItems: [
|
|
{ labelKey: "dt_scenarios", path: "/dashboard-testing/scenarios", requiredPermission: "dashboard:testing", requiredAction: "READ" },
|
|
{ labelKey: "dt_runs", path: "/dashboard-testing/runs", requiredPermission: "scenario", requiredAction: "RUN" },
|
|
{ labelKey: "dt_analytics", path: "/dashboard-testing/analytics", requiredPermission: "scenario:result", requiredAction: "VIEW" },
|
|
{ labelKey: "dt_automation", path: "/dashboard-testing/automation", requiredPermission: "scenario:automation", requiredAction: "TRIGGER" },
|
|
],
|
|
},
|
|
{
|
|
id: "reports",
|
|
labelKey: "reports",
|
|
icon: "reports",
|
|
tone: "from-category-reports-from to-category-reports-to text-category-reports-text ring-category-reports-ring",
|
|
path: "/reports",
|
|
requiredPermission: "tasks",
|
|
requiredAction: "READ",
|
|
subItems: [
|
|
{ labelKey: "all_reports", labelFallback: "All Reports", path: "/reports", requiredPermission: "tasks", requiredAction: "READ" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
|
|
// ── Section 3: System ────────────────────────────────────
|
|
{
|
|
id: "system",
|
|
labelKey: "section_system",
|
|
categories: [
|
|
{
|
|
id: "tools",
|
|
labelKey: "tools",
|
|
icon: "list",
|
|
tone: "from-category-tools-from to-category-tools-to text-category-tools-text ring-category-tools-ring",
|
|
path: "/tools/mapper",
|
|
subItems: [
|
|
{ labelKey: "tools_mapper", path: "/tools/mapper", requiredFeature: "dataset_mapper" },
|
|
{ labelKey: "tools_debug", path: "/tools/debug", requiredFeature: "debug" },
|
|
{ labelKey: "tools_storage", path: "/tools/storage", requiredFeature: "storage_manager" },
|
|
{ labelKey: "tools_backups", path: "/tools/backups", requiredFeature: "backup" },
|
|
],
|
|
},
|
|
{
|
|
id: "admin",
|
|
labelKey: "admin",
|
|
icon: "admin",
|
|
tone: "from-category-admin-from to-category-admin-to text-category-admin-text ring-category-admin-ring",
|
|
path: "/admin/users",
|
|
subItems: [
|
|
{ labelKey: "admin_users", path: "/admin/users", requiredPermission: "admin:users", requiredAction: "READ" },
|
|
{ labelKey: "admin_roles", path: "/admin/roles", requiredPermission: "admin:roles", requiredAction: "READ" },
|
|
{ labelKey: "admin_settings", path: "/admin/settings", requiredPermission: "admin:settings", requiredAction: "READ" },
|
|
{ labelKey: "admin_llm", path: "/admin/settings/llm", requiredPermission: "admin:settings", requiredAction: "READ" },
|
|
{ labelKey: "admin_mcp", path: "/admin/mcp", requiredPermission: "admin:roles", requiredAction: "READ" },
|
|
],
|
|
},
|
|
{
|
|
id: "maintenance",
|
|
labelKey: "maintenance",
|
|
labelFallback: "Maintenance",
|
|
icon: "activity",
|
|
tone: "from-category-maintenance-from to-category-maintenance-to text-category-maintenance-text ring-category-maintenance-ring",
|
|
path: "/maintenance",
|
|
requiredPermission: "maintenance",
|
|
requiredAction: "READ",
|
|
requiredFeature: "maintenance_banner",
|
|
subItems: [
|
|
{ labelKey: "maintenance", labelFallback: "Maintenance", path: "/maintenance", requiredPermission: "maintenance", requiredAction: "READ", requiredFeature: "maintenance_banner" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
];
|
|
// #endregion Layout.SidebarNavigation.SidebarSectionsRegistry
|
|
|
|
// #region Layout.SidebarNavigation.IsItemAllowedFunction [TYPE Function]
|
|
// @BRIEF: Check whether a single menu node can be shown for a given user.
|
|
// @PRE: item can contain optional requiredPermission/requiredAction.
|
|
// @POST: Returns true when no permission is required or permission check passes.
|
|
function isItemAllowed(user: User | null | undefined, item: { requiredPermission?: string; requiredAction?: string }): boolean {
|
|
if (!item?.requiredPermission) return true;
|
|
return hasPermission(
|
|
user,
|
|
item.requiredPermission,
|
|
item.requiredAction || "READ",
|
|
);
|
|
}
|
|
// #endregion Layout.SidebarNavigation.IsItemAllowedFunction
|
|
|
|
// #region Layout.SidebarNavigation.IsFeatureEnabledFunction [TYPE Function]
|
|
function isFeatureEnabled(item: { requiredFeature?: string }, features: Record<string, boolean | undefined>): boolean {
|
|
if (!item?.requiredFeature) return true;
|
|
return features[item.requiredFeature] !== false;
|
|
}
|
|
// #endregion Layout.SidebarNavigation.IsFeatureEnabledFunction
|
|
|
|
// #region Layout.SidebarNavigation.ResolveLabelFunction [TYPE Function]
|
|
// @BRIEF: Resolve a nav label from the i18n nav dictionary with optional static fallback.
|
|
function resolveLabel(i18nState: I18nNavState, item: { labelKey: string; labelFallback?: string }): string {
|
|
const nav = i18nState?.nav || {};
|
|
const value = nav[item.labelKey];
|
|
if (typeof value === "string" && value) return value;
|
|
return item.labelFallback || item.labelKey;
|
|
}
|
|
// #endregion Layout.SidebarNavigation.ResolveLabelFunction
|
|
|
|
// #region Layout.SidebarNavigation.BuildSidebarSectionsFunction [TYPE Function]
|
|
// @BRIEF: Build translated sidebar sections with categories, filtered by RBAC permissions and feature flags.
|
|
// @PRE: i18nState provides nav labels; user can be null; features default to {} (all enabled).
|
|
// @POST: Returns only sections/categories/subitems available for provided user and enabled features.
|
|
export function buildSidebarSections(i18nState: I18nNavState, user: User | null | undefined, features: Record<string, boolean | undefined> = {}): Section[] {
|
|
const allSections: Section[] = SIDEBAR_SECTIONS.map((section) => ({
|
|
id: section.id,
|
|
label: resolveLabel(i18nState, section),
|
|
categories: section.categories.map((category) => ({
|
|
id: category.id,
|
|
label: resolveLabel(i18nState, category),
|
|
icon: category.icon,
|
|
tone: category.tone,
|
|
path: category.path,
|
|
requiredPermission: category.requiredPermission,
|
|
requiredAction: category.requiredAction,
|
|
requiredFeature: category.requiredFeature,
|
|
subItems: category.subItems.map((sub) => ({
|
|
label: resolveLabel(i18nState, sub),
|
|
path: sub.path,
|
|
requiredPermission: sub.requiredPermission,
|
|
requiredAction: sub.requiredAction,
|
|
requiredFeature: sub.requiredFeature,
|
|
})),
|
|
})),
|
|
}));
|
|
|
|
// Filter by permissions and features
|
|
return allSections
|
|
.map((section) => ({
|
|
...section,
|
|
categories: section.categories
|
|
.map((category) => {
|
|
const visibleSubItems = (category.subItems || []).filter(
|
|
(sub) => isItemAllowed(user, sub) && isFeatureEnabled(sub, features),
|
|
);
|
|
return { ...category, subItems: visibleSubItems };
|
|
})
|
|
.filter((category) => {
|
|
if (!isItemAllowed(user, category)) return false;
|
|
if (!isFeatureEnabled(category, features)) return false;
|
|
return Array.isArray(category.subItems) && category.subItems.length > 0;
|
|
}),
|
|
}))
|
|
.filter((section) => section.categories.length > 0);
|
|
}
|
|
// #endregion Layout.SidebarNavigation.BuildSidebarSectionsFunction
|
|
|
|
// #region Layout.SidebarNavigation.GetTogglableFeatureNodesFunction [TYPE Function]
|
|
// @BRIEF: Collect every sidebar feature flag, for the settings "Разделы проекта" tab.
|
|
// @PRE: SIDEBAR_SECTIONS is the static registry; no user/RBAC filtering is applied (settings are admin-only).
|
|
// @POST: Returns one node per distinct requiredFeature in nav order (duplicates — a category and its
|
|
// subitems sharing one flag — collapse to the first occurrence), with section/category context.
|
|
// @RATIONALE: The settings tab derives its toggle list from this registry instead of a hardcoded
|
|
// copy, so adding a gated route to the sidebar makes it appear in settings automatically.
|
|
export interface TogglableFeatureNode {
|
|
featureId: NavFeatureId;
|
|
labelKey: string;
|
|
labelFallback?: string;
|
|
sectionId: string;
|
|
sectionLabelKey: string;
|
|
categoryId: string;
|
|
categoryLabelKey: string;
|
|
categoryLabelFallback?: string;
|
|
}
|
|
|
|
export function getTogglableFeatureNodes(): TogglableFeatureNode[] {
|
|
const nodes: TogglableFeatureNode[] = [];
|
|
const seenFeatureIds = new Set<NavFeatureId>();
|
|
const pushNode = (node: TogglableFeatureNode): void => {
|
|
if (seenFeatureIds.has(node.featureId)) return;
|
|
seenFeatureIds.add(node.featureId);
|
|
nodes.push(node);
|
|
};
|
|
for (const section of SIDEBAR_SECTIONS) {
|
|
for (const category of section.categories) {
|
|
if (category.requiredFeature) {
|
|
pushNode({
|
|
featureId: category.requiredFeature,
|
|
labelKey: category.labelKey,
|
|
labelFallback: category.labelFallback,
|
|
sectionId: section.id,
|
|
sectionLabelKey: section.labelKey,
|
|
categoryId: category.id,
|
|
categoryLabelKey: category.labelKey,
|
|
categoryLabelFallback: category.labelFallback,
|
|
});
|
|
}
|
|
for (const sub of category.subItems) {
|
|
if (sub.requiredFeature) {
|
|
pushNode({
|
|
featureId: sub.requiredFeature,
|
|
labelKey: sub.labelKey,
|
|
labelFallback: sub.labelFallback,
|
|
sectionId: section.id,
|
|
sectionLabelKey: section.labelKey,
|
|
categoryId: category.id,
|
|
categoryLabelKey: category.labelKey,
|
|
categoryLabelFallback: category.labelFallback,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nodes;
|
|
}
|
|
// #endregion Layout.SidebarNavigation.GetTogglableFeatureNodesFunction
|
|
|
|
// #endregion Layout.SidebarNavigation.SidebarNavigationModule
|