import { useLocation } from 'react-router-dom'; import { Link } from 'react-router-dom'; import type { ReactElement } from 'react'; import { cn } from '@/lib/utils'; import { useAppSelector } from '@/hooks/redux-hooks'; export interface TabItem { label: string; path: string; } interface PageHeaderProps { title: string; description?: string; tabs?: TabItem[]; action?: React.ReactNode; } const defaultTabs: TabItem[] = [ { label: 'Overview', path: '/dashboard' }, { label: 'Tenants', path: '/tenants' }, // { label: 'Users', path: '/users' }, // { label: 'Roles', path: '/roles' }, { label: 'Modules', path: '/modules' }, { label: 'Audit Logs', path: '/audit-logs' }, ]; export const PageHeader = ({ title, description, tabs, action, }: PageHeaderProps): ReactElement => { const location = useLocation(); const { roles } = useAppSelector((state) => state.auth); // Check if user is super_admin let rolesArray: string[] = []; if (Array.isArray(roles)) { rolesArray = roles; } else if (typeof roles === 'string') { try { rolesArray = JSON.parse(roles); } catch { rolesArray = []; } } const isSuperAdmin = rolesArray.includes('super_admin'); const isPathMatch = (tabPath: string): boolean => location.pathname === tabPath || location.pathname.startsWith(`${tabPath}/`); const resolvedTabs = tabs ?? (isSuperAdmin ? defaultTabs : []); // Pick the most specific matching tab (longest path), so parent tabs // like /tenant/documents don't stay active on child routes. const activeTabPath = resolvedTabs .filter((tab) => isPathMatch(tab.path)) .sort((a, b) => b.path.length - a.path.length)[0]?.path ?? null; return (
{description}
)}