feat(admin): group sidebar navigation
This commit is contained in:
@@ -3,33 +3,66 @@
|
|||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import {
|
import {
|
||||||
|
Building2,
|
||||||
CalendarDays,
|
CalendarDays,
|
||||||
|
ChevronDown,
|
||||||
FileText,
|
FileText,
|
||||||
FolderTree,
|
FolderTree,
|
||||||
|
GraduationCap,
|
||||||
PanelRightClose,
|
PanelRightClose,
|
||||||
PanelRightOpen,
|
PanelRightOpen,
|
||||||
ShieldCheck,
|
ShieldCheck,
|
||||||
Tags,
|
Tags,
|
||||||
|
TicketPercent,
|
||||||
UsersRound,
|
UsersRound,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { Navigate, NavLink, useLocation } from "@/lib/router";
|
import { Navigate, NavLink, useLocation } from "@/lib/router";
|
||||||
import { useAuth } from "@/contexts/AuthContext";
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
const navItems = [
|
const navGroups = [
|
||||||
|
{
|
||||||
|
key: "users",
|
||||||
|
label: "کاربران",
|
||||||
|
items: [
|
||||||
{ to: "/admin/users", label: "کاربران", icon: UsersRound, visibility: "staff" },
|
{ to: "/admin/users", label: "کاربران", icon: UsersRound, visibility: "staff" },
|
||||||
|
{ to: "/admin/universities", label: "دانشگاهها", icon: Building2, visibility: "staff" },
|
||||||
|
{ to: "/admin/majors", label: "رشتهها", icon: GraduationCap, visibility: "staff" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "events",
|
||||||
|
label: "رویدادها",
|
||||||
|
items: [
|
||||||
{ to: "/admin/events", label: "رویدادها", icon: CalendarDays, visibility: "staff" },
|
{ to: "/admin/events", label: "رویدادها", icon: CalendarDays, visibility: "staff" },
|
||||||
|
{ to: "/admin/coupons", label: "کدهای تخفیف", icon: TicketPercent, visibility: "staff" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "blog",
|
||||||
|
label: "بلاگ و دسترسی",
|
||||||
|
items: [
|
||||||
{ to: "/admin/blog", label: "نوشتههای بلاگ", icon: FileText, visibility: "blog" },
|
{ to: "/admin/blog", label: "نوشتههای بلاگ", icon: FileText, visibility: "blog" },
|
||||||
{ to: "/admin/blog/categories", label: "دستهبندیها", icon: FolderTree, visibility: "taxonomy" },
|
{ to: "/admin/blog/categories", label: "دستهبندیها", icon: FolderTree, visibility: "taxonomy" },
|
||||||
{ to: "/admin/blog/tags", label: "برچسبها", icon: Tags, visibility: "taxonomy" },
|
{ to: "/admin/blog/tags", label: "برچسبها", icon: Tags, visibility: "taxonomy" },
|
||||||
{ to: "/admin/authorizations", label: "دسترسیها", icon: ShieldCheck, visibility: "superuser" },
|
{ to: "/admin/authorizations", label: "دسترسیها", icon: ShieldCheck, visibility: "superuser" },
|
||||||
|
],
|
||||||
|
},
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
type NavItem = (typeof navGroups)[number]["items"][number];
|
||||||
|
|
||||||
export default function AdminLayout({ children }: { children: ReactNode }) {
|
export default function AdminLayout({ children }: { children: ReactNode }) {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const { user, isAuthenticated, loading } = useAuth();
|
const { user, isAuthenticated, loading } = useAuth();
|
||||||
const [collapsed, setCollapsed] = useState(false);
|
const [collapsed, setCollapsed] = useState(false);
|
||||||
|
const [openGroups, setOpenGroups] = useState<Record<string, boolean>>({
|
||||||
|
users: true,
|
||||||
|
events: true,
|
||||||
|
blog: true,
|
||||||
|
});
|
||||||
const canAccessAdmin = useMemo(
|
const canAccessAdmin = useMemo(
|
||||||
() => isAuthenticated && Boolean(user?.is_staff || user?.is_superuser || user?.can_access_blog_admin),
|
() => isAuthenticated && Boolean(user?.is_staff || user?.is_superuser || user?.can_access_blog_admin),
|
||||||
[isAuthenticated, user?.can_access_blog_admin, user?.is_staff, user?.is_superuser],
|
[isAuthenticated, user?.can_access_blog_admin, user?.is_staff, user?.is_superuser],
|
||||||
@@ -60,18 +93,26 @@ export default function AdminLayout({ children }: { children: ReactNode }) {
|
|||||||
return <Navigate to="/" replace />;
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
const visibleNavItems = navItems.filter((item) => {
|
const canSeeItem = (item: NavItem) => {
|
||||||
if (item.visibility === "staff") return Boolean(user?.is_staff || user?.is_superuser);
|
if (item.visibility === "staff") return Boolean(user?.is_staff || user?.is_superuser);
|
||||||
if (item.visibility === "taxonomy") return Boolean(user?.is_staff || user?.is_superuser || user?.can_review_blog_posts);
|
if (item.visibility === "taxonomy") return Boolean(user?.is_staff || user?.is_superuser || user?.can_review_blog_posts);
|
||||||
if (item.visibility === "superuser") return Boolean(user?.is_superuser);
|
if (item.visibility === "superuser") return Boolean(user?.is_superuser);
|
||||||
return Boolean(user?.is_staff || user?.is_superuser || user?.can_access_blog_admin);
|
return Boolean(user?.is_staff || user?.is_superuser || user?.can_access_blog_admin);
|
||||||
});
|
};
|
||||||
|
|
||||||
|
const visibleGroups = navGroups
|
||||||
|
.map((group) => ({ ...group, items: group.items.filter(canSeeItem) }))
|
||||||
|
.filter((group) => group.items.length > 0);
|
||||||
|
const visibleNavItems = visibleGroups.flatMap((group) => group.items);
|
||||||
|
|
||||||
const isItemActive = (to: string) => {
|
const isItemActive = (to: string) => {
|
||||||
if (location.pathname === to) return true;
|
if (location.pathname === to) return true;
|
||||||
if (to === "/admin/blog") {
|
if (to === "/admin/blog") {
|
||||||
return /^\/admin\/blog\/(new|\d+)/.test(location.pathname ?? "");
|
return /^\/admin\/blog\/(new|\d+)/.test(location.pathname ?? "");
|
||||||
}
|
}
|
||||||
|
if (to === "/admin/events") {
|
||||||
|
return /^\/admin\/events(\/(create|\d+))?/.test(location.pathname ?? "");
|
||||||
|
}
|
||||||
return Boolean(location.pathname?.startsWith(`${to}/`));
|
return Boolean(location.pathname?.startsWith(`${to}/`));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -95,8 +136,23 @@ export default function AdminLayout({ children }: { children: ReactNode }) {
|
|||||||
{collapsed ? <PanelRightOpen className="h-5 w-5" /> : <PanelRightClose className="h-5 w-5" />}
|
{collapsed ? <PanelRightOpen className="h-5 w-5" /> : <PanelRightClose className="h-5 w-5" />}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<nav className="flex-1 space-y-2 p-3">
|
<nav className="flex-1 space-y-3 p-3">
|
||||||
{visibleNavItems.map((item) => {
|
{visibleGroups.map((group) => (
|
||||||
|
<Collapsible
|
||||||
|
key={group.key}
|
||||||
|
open={collapsed ? true : openGroups[group.key]}
|
||||||
|
onOpenChange={(open) => setOpenGroups((current) => ({ ...current, [group.key]: open }))}
|
||||||
|
>
|
||||||
|
{!collapsed ? (
|
||||||
|
<CollapsibleTrigger className="mb-1 flex w-full items-center justify-between rounded-xl px-3 py-2 text-xs font-semibold text-muted-foreground hover:bg-muted/70">
|
||||||
|
<span>{group.label}</span>
|
||||||
|
<ChevronDown
|
||||||
|
className={cn("h-4 w-4 transition-transform", openGroups[group.key] ? "rotate-180" : "")}
|
||||||
|
/>
|
||||||
|
</CollapsibleTrigger>
|
||||||
|
) : null}
|
||||||
|
<CollapsibleContent className="space-y-2">
|
||||||
|
{group.items.map((item) => {
|
||||||
const Icon = item.icon;
|
const Icon = item.icon;
|
||||||
const active = isItemActive(item.to);
|
const active = isItemActive(item.to);
|
||||||
return (
|
return (
|
||||||
@@ -117,6 +173,9 @@ export default function AdminLayout({ children }: { children: ReactNode }) {
|
|||||||
</NavLink>
|
</NavLink>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
</CollapsibleContent>
|
||||||
|
</Collapsible>
|
||||||
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user