from django.urls import reverse def _tab(label: str, href: str, active: bool) -> dict: return { "label": label, "href": href, "active": bool(active), } def settings_hierarchy_nav(request): match = getattr(request, "resolver_match", None) if match is None: return {} url_name = str(getattr(match, "url_name", "") or "") namespace = str(getattr(match, "namespace", "") or "") path = str(getattr(request, "path", "") or "") notifications_href = reverse("notifications_settings") system_href = reverse("system_settings") accessibility_href = reverse("accessibility_settings") encryption_href = reverse("encryption_settings") permissions_href = reverse("permission_settings") security_2fa_href = reverse("security_2fa") ai_models_href = reverse("ai_models") ai_traces_href = reverse("ai_execution_log") commands_href = reverse("command_routing") business_plans_href = reverse("business_plan_inbox") tasks_href = reverse("tasks_settings") translation_href = reverse("translation_settings") behavioral_href = reverse("behavioral_signals_settings") categories = { "general": { "routes": { "notifications_settings", "notifications_update", "system_settings", "accessibility_settings", }, "title": "General", "tabs": [ ( "Notifications", notifications_href, lambda: path == notifications_href, ), ("System", system_href, lambda: path == system_href), ( "Accessibility", accessibility_href, lambda: path == accessibility_href, ), ], }, "security": { "routes": { "security_settings", "encryption_settings", "permission_settings", "security_2fa", }, "title": "Security", "tabs": [ ("Encryption", encryption_href, lambda: path == encryption_href), ("Permissions", permissions_href, lambda: path == permissions_href), ( "2FA", security_2fa_href, lambda: path == security_2fa_href or namespace == "two_factor", ), ], }, "ai": { "routes": { "ai_settings", "ai_models", "ais", "ai_create", "ai_update", "ai_delete", "ai_execution_log", }, "title": "AI", "tabs": [ ("Models", ai_models_href, lambda: path == ai_models_href), ("Traces", ai_traces_href, lambda: path == ai_traces_href), ], }, "modules": { "routes": { "modules_settings", "command_routing", "business_plan_inbox", "business_plan_editor", "tasks_settings", "translation_settings", "translation_preview", "availability_settings", "behavioral_signals_settings", "codex_settings", "codex_approval", }, "title": "Modules", "tabs": [ ("Commands", commands_href, lambda: path == commands_href), ( "Business Plans", business_plans_href, lambda: url_name in {"business_plan_inbox", "business_plan_editor"}, ), ("Task Automation", tasks_href, lambda: path == tasks_href), ( "Translation", translation_href, lambda: url_name in {"translation_settings", "translation_preview"}, ), ( "Behavioral Signals", behavioral_href, lambda: url_name in {"availability_settings", "behavioral_signals_settings"}, ), ], }, } two_factor_security_routes = { "profile", "setup", "backup_tokens", "disable", "phone_create", "phone_delete", } if url_name in categories["general"]["routes"]: category = categories["general"] elif url_name in categories["security"]["routes"] or ( namespace == "two_factor" and url_name in two_factor_security_routes ): category = categories["security"] elif url_name in categories["ai"]["routes"]: category = categories["ai"] elif url_name in categories["modules"]["routes"]: category = categories["modules"] else: category = None if category is None: settings_nav = None else: settings_nav = { "title": str(category.get("title") or "Settings"), "tabs": [ _tab(label, href, bool(is_active())) for label, href, is_active in category.get("tabs", []) ], } if not settings_nav: return {} return {"settings_nav": settings_nav}