Spaces:
Sleeping

proje / apps /web /src /components /layout /Sidebar.tsx
rodopsin
feat: restructure navigation per Navi doc and make all screens responsive
7cebfbf
Raw History Blame Contribute Delete
4.1 kB
'use client';
import { ChevronDown } from 'lucide-react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';
import { cn } from '@/lib/utils';
import { getVisibleExtras, getVisibleGroups, type AppRole, type NavGroup } from './nav-items';
function isActive(pathname: string, href: string) {
if (href === '/') return pathname === '/';
return pathname === href || pathname.startsWith(`${href}/`);
}
function groupIsActive(pathname: string, group: NavGroup) {
if (group.href && isActive(pathname, group.href)) return true;
return Boolean(group.children?.some((c) => isActive(pathname, c.href)));
}
function NavGroupBlock({ group, pathname }: { group: NavGroup; pathname: string }) {
const active = groupIsActive(pathname, group);
const [open, setOpen] = useState(active);
// Başka bir sayfaya geçildiğinde ilgili grup kendiliğinden açılsın.
useEffect(() => {
if (active) setOpen(true);
}, [active]);
const Icon = group.icon;
if (!group.children) {
return (
<Link
href={group.href ?? '#'}
className={cn('ajet-nav-item', active && 'active')}
>
<Icon className="h-[16px] w-[16px] flex-shrink-0" />
{group.title}
</Link>
);
}
return (
<div className="ajet-nav-group">
<button
type="button"
className={cn('ajet-nav-item ajet-nav-parent', active && 'parent-active')}
onClick={() => setOpen((v) => !v)}
aria-expanded={open}
>
<Icon className="h-[16px] w-[16px] flex-shrink-0" />
{group.title}
<ChevronDown
className={cn('ajet-nav-chevron h-[14px] w-[14px]', open && 'open')}
/>
</button>
{open ? (
<div className="ajet-nav-children">
{group.children.map((child) => (
<Link
key={child.href}
href={child.href}
className={cn('ajet-nav-child', isActive(pathname, child.href) && 'active')}
>
{child.title}
</Link>
))}
</div>
) : null}
</div>
);
}
export function Sidebar({ role }: { role: AppRole }) {
const pathname = usePathname();
const groups = getVisibleGroups(role);
const extras = getVisibleExtras(role);
return (
<aside className="ajet-sidebar ajet-sidebar-desktop">
{/* Header */}
<div className="ajet-sidebar-head">
<div className="ajet-sidebar-logo" aria-hidden="true">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src="/brand/ajet-mark-white.png" alt="" />
</div>
<div className="ajet-sidebar-wordmark">
AJet <span>360</span>
</div>
</div>
{/* Nav */}
<nav className="ajet-sidebar-nav">
<div className="ajet-sidebar-section">Ana Modüller</div>
{groups.map((group) => (
<NavGroupBlock key={group.title} group={group} pathname={pathname} />
))}
{extras.length ? (
<>
<div className="ajet-sidebar-section">Diğer</div>
{extras.map((group) => (
<NavGroupBlock key={group.title} group={group} pathname={pathname} />
))}
</>
) : null}
</nav>
{/* Footer */}
<div className="ajet-sidebar-foot">
<div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
<div
style={{
width: 32,
height: 32,
borderRadius: 10,
background: 'var(--ajet-navy-700)',
color: '#fff',
display: 'grid',
placeItems: 'center',
fontWeight: 700,
fontSize: 12,
}}
>
AA
</div>
<div>
<div style={{ color: 'var(--dark-fg-2)', fontSize: 13, fontWeight: 600 }}>
AJet Admin
</div>
<div style={{ fontSize: 11 }}>admin@ajet.com.tr</div>
</div>
</div>
</div>
</aside>
);
}