refactor: update sidebar visibility rules to match user profession

This commit is contained in:
estifanos
2026-07-29 08:08:53 +00:00
parent 09d688b251
commit 8118b646bd

View File

@@ -158,8 +158,11 @@ const NAV_ITEMS: (NavItem & { i18nKey: string })[] = [
];
// ── Sidebar visibility rules ──────────────────────────────────────────────
// Edit these lists to change what each user type sees. Values are the `to`
// paths from NAV_ITEMS above. '*' means every item.
// Edit these lists to change what each profession sees. Each key is matched
// case-insensitively as a substring against the user's profession name
// (state.auth.currentProfile.profession.name.en) — e.g. the key "cadet"
// matches a profession named "Deck Cadet". Values are the `to` paths from
// NAV_ITEMS above. '*' means every item.
const ALWAYS_VISIBLE = ['/notifications', '/profile', '/support'];
@@ -194,7 +197,7 @@ const NAV_ACCESS: Record<string, string[] | '*'> = {
],
};
// Used when userType is missing or unrecognized. Set to '*' to show everything.
// Used when profession is missing or matches no key above. Set to '*' to show everything.
const FALLBACK_ACCESS: string[] | '*' = ['/dashboard'];
const PAGE_META: Record<string, { i18nKey: string }> = {
@@ -229,7 +232,12 @@ export function PortalLayout() {
const location = useLocation();
const dispatch = useDispatch();
const user = useAppSelector((state) => state.auth.user);
const allowed = NAV_ACCESS[user?.userType ?? ''] ?? FALLBACK_ACCESS;
const currentProfile = useAppSelector((state) => state.auth.currentProfile);
const professionName = (currentProfile?.profession?.name?.en ?? "").toLowerCase();
const matchedKey = Object.keys(NAV_ACCESS).find((key) =>
professionName.includes(key),
);
const allowed = (matchedKey ? NAV_ACCESS[matchedKey] : undefined) ?? FALLBACK_ACCESS;
const visibleNavItems =
allowed === '*'
? NAV_ITEMS
@@ -315,7 +323,7 @@ export function PortalLayout() {
}}
>
<AppSidebar
navItems={NAV_ITEMS.map(({ i18nKey, ...rest }) => ({
navItems={visibleNavItems.map(({ i18nKey, ...rest }) => ({
...rest,
label: t(i18nKey),
}))}