From feea83a5884be4c70d390b868623ea552c594e8a Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Wed, 5 Aug 2026 12:14:42 +0300 Subject: [PATCH] Fix admin white screen from circular imports and route guards. Break teamRoles/adminAccess cycle and restrict reduced navigation to Content Manager only so legacy team roles no longer redirect-loop on dashboard. Co-authored-by: Cursor --- src/lib/adminAccess.ts | 45 +++++++++------------ src/lib/teamRoles.ts | 8 +++- src/pages/role-management/RolesListPage.tsx | 3 +- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/lib/adminAccess.ts b/src/lib/adminAccess.ts index 7b5ce27..7d08acd 100644 --- a/src/lib/adminAccess.ts +++ b/src/lib/adminAccess.ts @@ -2,13 +2,6 @@ import { getNormalizedSessionTeamRole } from "./teamRole" export const FULL_PANEL_TEAM_ROLES = new Set(["SUPER_ADMIN", "ADMIN"]) -/** The three assignable staff roles for the admin panel. */ -export const STAFF_TEAM_ROLE_OPTIONS = [ - { value: "SUPER_ADMIN", label: "Super Admin" }, - { value: "ADMIN", label: "Admin" }, - { value: "CONTENT_MANAGER", label: "Content Manager" }, -] as const - const CONTENT_MANAGER_PATH_PREFIXES = [ "/new-content", "/personas", @@ -16,34 +9,38 @@ const CONTENT_MANAGER_PATH_PREFIXES = [ "/content", ] +function normalizedTeamRole(role?: string): string { + return (role ?? getNormalizedSessionTeamRole()).toUpperCase() +} + +/** Only Content Manager gets a reduced admin panel; all other roles keep full navigation. */ +export function isContentManagerPanelRole(role?: string): boolean { + return normalizedTeamRole(role) === "CONTENT_MANAGER" +} + export function hasFullAdminPanelAccess(role?: string): boolean { - const r = (role ?? getNormalizedSessionTeamRole()).toUpperCase() - return FULL_PANEL_TEAM_ROLES.has(r) + const r = normalizedTeamRole(role) + if (!r) return true + return !isContentManagerPanelRole(r) } export function isContentManagerRole(role?: string): boolean { - const r = (role ?? getNormalizedSessionTeamRole()).toUpperCase() - return r === "CONTENT_MANAGER" + return isContentManagerPanelRole(role) } export function canSendTeamInvitations(role?: string): boolean { - const r = (role ?? getNormalizedSessionTeamRole()).toUpperCase() - return r === "SUPER_ADMIN" + return normalizedTeamRole(role) === "SUPER_ADMIN" } export function isPathAllowedForTeamRole(pathname: string, role?: string): boolean { - const r = (role ?? getNormalizedSessionTeamRole()).toUpperCase() - if (!r || hasFullAdminPanelAccess(r)) return true - if (isContentManagerRole(r)) { - return CONTENT_MANAGER_PATH_PREFIXES.some( - (p) => pathname === p || pathname.startsWith(`${p}/`), - ) - } - return pathname === "/profile" || pathname.startsWith("/profile/") + if (!isContentManagerPanelRole(role)) return true + return CONTENT_MANAGER_PATH_PREFIXES.some( + (p) => pathname === p || pathname.startsWith(`${p}/`), + ) } export function getDefaultAppHome(role?: string): string { - if (isContentManagerRole(role)) return "/new-content" + if (isContentManagerPanelRole(role)) return "/new-content" return "/dashboard" } @@ -61,9 +58,7 @@ export function isNavEntryAllowedForTeamRole( entry: FilterableNavEntry, role?: string, ): boolean { - const r = (role ?? getNormalizedSessionTeamRole()).toUpperCase() - if (!r || hasFullAdminPanelAccess(r)) return true - if (!isContentManagerRole(r)) return false + if (!isContentManagerPanelRole(role)) return true if (entry.kind === "section") { return entry.label === "Learning content" || entry.label === "Account" diff --git a/src/lib/teamRoles.ts b/src/lib/teamRoles.ts index b9b1a8e..723feaf 100644 --- a/src/lib/teamRoles.ts +++ b/src/lib/teamRoles.ts @@ -1,5 +1,11 @@ import type { Role } from "../types/rbac.types" -import { STAFF_TEAM_ROLE_OPTIONS } from "./adminAccess" + +/** The three assignable staff roles for the admin panel. */ +export const STAFF_TEAM_ROLE_OPTIONS = [ + { value: "SUPER_ADMIN", label: "Super Admin" }, + { value: "ADMIN", label: "Admin" }, + { value: "CONTENT_MANAGER", label: "Content Manager" }, +] as const export const TEAM_ROLE_OPTIONS = [...STAFF_TEAM_ROLE_OPTIONS] diff --git a/src/pages/role-management/RolesListPage.tsx b/src/pages/role-management/RolesListPage.tsx index b68d5a3..64ae146 100644 --- a/src/pages/role-management/RolesListPage.tsx +++ b/src/pages/role-management/RolesListPage.tsx @@ -43,8 +43,9 @@ import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tabl import { toast } from "sonner" import { SpinnerIcon } from "../../components/ui/spinner-icon" import { InviteTeamMemberDialog } from "./components/InviteTeamMemberDialog" -import { canSendTeamInvitations, STAFF_TEAM_ROLE_OPTIONS } from "../../lib/adminAccess" +import { canSendTeamInvitations } from "../../lib/adminAccess" import { getNormalizedSessionTeamRole } from "../../lib/teamRole" +import { STAFF_TEAM_ROLE_OPTIONS } from "../../lib/teamRoles" export function RolesListPage() { const navigate = useNavigate()