Spaces:
Sleeping
Sleeping
File size: 8,817 Bytes
f305a41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | import { NavLink, useLocation } from 'react-router-dom';
import { cn } from '@/lib/utils';
import { useAuth } from '@/contexts/AuthContext';
import { getRoleLabel } from '@/data/dummyData';
import {
LayoutDashboard,
FileText,
Warehouse,
ClipboardCheck,
Award,
Receipt,
Users,
Settings,
LogOut,
ChevronLeft,
ChevronRight,
Package,
Factory,
BarChart3,
Fish,
X,
Calendar,
} from 'lucide-react';
import { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
import { useIsMobile } from '@/hooks/use-mobile';
interface NavItem {
label: string;
href: string;
icon: React.ComponentType<{ className?: string }>;
roles?: string[];
}
const navItems: NavItem[] = [
{ label: 'Dashboard', href: '/dashboard', icon: LayoutDashboard },
{ label: 'Hatcheries', href: '/hatcheries', icon: Fish },
{ label: 'Farms', href: '/farms', icon: Warehouse },
{ label: 'Applications', href: '/applications', icon: FileText },
{ label: 'Audits', href: '/audits', icon: ClipboardCheck },
{ label: 'FOI Applications', href: '/foi/applications', icon: ClipboardCheck, roles: ['foi'] },
{ label: 'FO Field Visits', href: '/fo/visits', icon: ClipboardCheck, roles: ['fo'] },
{ label: 'CC Officer Audits', href: '/cc-officer/audits', icon: ClipboardCheck, roles: ['cc_officer'] },
{ label: 'Certificates', href: '/certificates', icon: Award },
{ label: 'CC Certification', href: '/cc-officer/certification', icon: Award, roles: ['cc_officer'] },
{ label: 'JD Approvals', href: '/jd/approvals', icon: ClipboardCheck, roles: ['jd'] },
{ label: 'Director Approvals', href: '/director/approvals', icon: ClipboardCheck, roles: ['director'] },
{ label: 'Surveillance Schedule', href: '/surveillance-schedule', icon: Calendar },
{ label: 'Raw Materials', href: '/raw-materials', icon: Package },
{ label: 'Processors', href: '/processors', icon: Factory },
{ label: 'TA Claims', href: '/ta-claims', icon: Receipt },
{ label: 'Reports', href: '/reports', icon: BarChart3 },
{ label: 'Users', href: '/users', icon: Users, roles: ['admin'] },
{ label: 'Settings', href: '/settings', icon: Settings },
];
interface SidebarProps {
mobileOpen: boolean;
onMobileClose: () => void;
}
export function Sidebar({ mobileOpen, onMobileClose }: SidebarProps) {
const [collapsed, setCollapsed] = useState(false);
const { user, logout } = useAuth();
const location = useLocation();
const isMobile = useIsMobile();
// Close sidebar on mobile when route changes
useEffect(() => {
if (isMobile && mobileOpen) {
onMobileClose();
}
}, [location.pathname]);
const filteredNavItems = navItems.filter(item => {
if (!item.roles) return true;
return user && item.roles.includes(user.role);
});
const roleSpecificNavItems = filteredNavItems.filter((item) => {
if (!user) return false;
if (user.role === 'cc_officer') {
const allowedForCcOfficer = new Set<string>([
'/dashboard',
'/hatcheries',
'/farms',
'/applications',
'/cc-officer/audits',
'/certificates',
'/cc-officer/certification',
]);
return allowedForCcOfficer.has(item.href);
}
if (user.role === 'foi') {
const allowedForFoi = new Set<string>([
'/dashboard',
'/foi/applications',
]);
return allowedForFoi.has(item.href);
}
if (user.role === 'fo') {
const allowedForFo = new Set<string>([
'/dashboard',
'/fo/visits',
]);
return allowedForFo.has(item.href);
}
if (user.role === 'jd') {
const allowedForJd = new Set<string>([
'/dashboard',
'/jd/approvals',
'/surveillance-schedule',
]);
return allowedForJd.has(item.href);
}
if (user.role === 'director') {
const allowedForDirector = new Set<string>([
'/dashboard',
'/director/approvals',
'/surveillance-schedule',
]);
return allowedForDirector.has(item.href);
}
return true;
});
return (
<>
{/* Mobile overlay */}
{isMobile && mobileOpen && (
<div
className="fixed inset-0 z-40 bg-background/80 backdrop-blur-sm"
onClick={onMobileClose}
/>
)}
<aside
className={cn(
'fixed left-0 top-0 z-50 h-screen transition-all duration-300 ease-in-out',
'bg-sidebar border-r border-sidebar-border',
isMobile ? (
mobileOpen ? 'translate-x-0 w-64' : '-translate-x-full w-64'
) : (
collapsed ? 'w-[72px]' : 'w-64'
)
)}
>
{/* Logo */}
<div className="flex h-16 items-center justify-between px-4 border-b border-sidebar-border">
{(!collapsed || isMobile) && (
<div className="flex items-center gap-2">
<div className="w-8 h-8 rounded-lg bg-sidebar-primary flex items-center justify-center">
<Fish className="w-5 h-5 text-sidebar-primary-foreground" />
</div>
<div>
<h1 className="text-sm font-bold text-sidebar-foreground">SHAPHARI</h1>
<p className="text-[10px] text-sidebar-foreground/60">MPEDA Portal</p>
</div>
</div>
)}
{collapsed && !isMobile && (
<div className="w-8 h-8 mx-auto rounded-lg bg-sidebar-primary flex items-center justify-center">
<Fish className="w-5 h-5 text-sidebar-primary-foreground" />
</div>
)}
{isMobile && (
<Button
variant="ghost"
size="icon"
className="text-sidebar-foreground hover:bg-sidebar-accent"
onClick={onMobileClose}
>
<X className="h-5 w-5" />
</Button>
)}
</div>
{/* Toggle Button - Desktop only */}
{!isMobile && (
<Button
variant="ghost"
size="icon"
className="absolute -right-3 top-20 z-50 h-6 w-6 rounded-full border border-sidebar-border bg-sidebar text-sidebar-foreground hover:bg-sidebar-accent"
onClick={() => setCollapsed(!collapsed)}
>
{collapsed ? <ChevronRight className="h-4 w-4" /> : <ChevronLeft className="h-4 w-4" />}
</Button>
)}
{/* Navigation */}
<nav className="flex-1 overflow-y-auto py-4 px-3 h-[calc(100vh-180px)]">
<ul className="space-y-1">
{roleSpecificNavItems.map((item) => {
const isActive = location.pathname === item.href;
return (
<li key={item.href}>
<NavLink
to={item.href}
className={cn(
'sidebar-nav-item',
isActive && 'sidebar-nav-item-active'
)}
title={collapsed && !isMobile ? item.label : undefined}
>
<item.icon className={cn('h-5 w-5 flex-shrink-0', isActive && 'text-sidebar-primary')} />
{(!collapsed || isMobile) && <span className="truncate">{item.label}</span>}
</NavLink>
</li>
);
})}
</ul>
</nav>
{/* User Profile */}
<div className="absolute bottom-0 left-0 right-0 border-t border-sidebar-border bg-sidebar p-3">
{user && (
<div className={cn('flex items-center gap-3', collapsed && !isMobile && 'justify-center')}>
<Avatar className="h-9 w-9 flex-shrink-0">
<AvatarFallback className="bg-sidebar-primary text-sidebar-primary-foreground text-xs">
{user.name.split(' ').map(n => n[0]).join('')}
</AvatarFallback>
</Avatar>
{(!collapsed || isMobile) && (
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-sidebar-foreground truncate">{user.name}</p>
<p className="text-xs text-sidebar-foreground/60 truncate">{getRoleLabel(user.role)}</p>
</div>
)}
{(!collapsed || isMobile) && (
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-sidebar-foreground/60 hover:text-sidebar-foreground hover:bg-sidebar-accent"
onClick={logout}
title="Logout"
>
<LogOut className="h-4 w-4" />
</Button>
)}
</div>
)}
</div>
</aside>
</>
);
}
|