mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-02 03:38:22 +00:00
refactor: reorganize imports and enhance admin category filtering in AppSidebar 🔨
This commit is contained in:
@ -1,21 +1,19 @@
|
||||
import { DeviceCartDrawer } from "@/components/device-cart";
|
||||
import { Wallet } from "@/components/wallet";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { NuqsAdapter } from 'nuqs/adapters/next/app'
|
||||
|
||||
import { ModeToggle } from "@/components/theme-toggle";
|
||||
import { AppSidebar } from "@/components/ui/app-sidebar";
|
||||
|
||||
import { getProfile } from "@/actions/payment";
|
||||
import { authOptions } from "@/app/auth";
|
||||
import { DeviceCartDrawer } from "@/components/device-cart";
|
||||
import { ModeToggle } from "@/components/theme-toggle";
|
||||
import { AppSidebar } from "@/components/ui/app-sidebar";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import {
|
||||
SidebarInset,
|
||||
SidebarProvider,
|
||||
SidebarTrigger,
|
||||
} from "@/components/ui/sidebar";
|
||||
import { Wallet } from "@/components/wallet";
|
||||
import { tryCatch } from "@/utils/tryCatch";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { redirect } from "next/navigation";
|
||||
import { WelcomeBanner } from "../welcome-banner";
|
||||
import { AccountPopover } from "./account-popver";
|
||||
|
||||
|
@ -9,7 +9,8 @@ import {
|
||||
UsersRound,
|
||||
Wallet2Icon,
|
||||
} from "lucide-react";
|
||||
|
||||
import Link from "next/link";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/auth";
|
||||
import {
|
||||
Collapsible,
|
||||
@ -28,8 +29,6 @@ import {
|
||||
SidebarMenuItem,
|
||||
SidebarRail,
|
||||
} from "@/components/ui/sidebar";
|
||||
import { getServerSession } from "next-auth";
|
||||
import Link from "next/link";
|
||||
|
||||
type Permission = {
|
||||
id: number;
|
||||
@ -40,17 +39,17 @@ type Categories = {
|
||||
id: string;
|
||||
children: (
|
||||
| {
|
||||
title: string;
|
||||
link: string;
|
||||
perm_identifier: string;
|
||||
icon: React.JSX.Element;
|
||||
}
|
||||
title: string;
|
||||
link: string;
|
||||
perm_identifier: string;
|
||||
icon: React.JSX.Element;
|
||||
}
|
||||
| {
|
||||
title: string;
|
||||
link: string;
|
||||
icon: React.JSX.Element;
|
||||
perm_identifier?: undefined;
|
||||
}
|
||||
title: string;
|
||||
link: string;
|
||||
icon: React.JSX.Element;
|
||||
perm_identifier?: undefined;
|
||||
}
|
||||
)[];
|
||||
}[];
|
||||
|
||||
@ -128,27 +127,31 @@ export async function AppSidebar({
|
||||
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
const filteredCategories = categories.map((category) => {
|
||||
const filteredChildren = category.children.filter((child) => {
|
||||
const permIdentifier = child.perm_identifier;
|
||||
return session?.user?.user_permissions?.some((permission: Permission) => {
|
||||
const permissionParts = permission.name.split(" ");
|
||||
const modelNameFromPermission = permissionParts.slice(2).join(" ");
|
||||
return modelNameFromPermission === permIdentifier;
|
||||
});
|
||||
});
|
||||
|
||||
return { ...category, children: filteredChildren };
|
||||
});
|
||||
const filteredCategoriesWithChildren = filteredCategories.filter(
|
||||
(category) => category.children.length > 0,
|
||||
);
|
||||
|
||||
let CATEGORIES: Categories;
|
||||
if (session?.user?.is_superuser) {
|
||||
if (session?.user?.is_admin) {
|
||||
CATEGORIES = categories;
|
||||
} else {
|
||||
CATEGORIES = filteredCategoriesWithChildren;
|
||||
// Filter out ADMIN CONTROL category for non-admin users
|
||||
const nonAdminCategories = categories.filter(
|
||||
(category) => category.id !== "ADMIN CONTROL"
|
||||
);
|
||||
|
||||
const filteredCategories = nonAdminCategories.map((category) => {
|
||||
const filteredChildren = category.children.filter((child) => {
|
||||
const permIdentifier = child.perm_identifier;
|
||||
return session?.user?.user_permissions?.some((permission: Permission) => {
|
||||
const permissionParts = permission.name.split(" ");
|
||||
const modelNameFromPermission = permissionParts.slice(2).join(" ");
|
||||
return modelNameFromPermission === permIdentifier;
|
||||
});
|
||||
});
|
||||
|
||||
return { ...category, children: filteredChildren };
|
||||
});
|
||||
|
||||
CATEGORIES = filteredCategories.filter(
|
||||
(category) => category.children.length > 0,
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
|
Reference in New Issue
Block a user