mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-03 06:48:21 +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 { redirect } from "next/navigation";
|
||||||
import { Wallet } from "@/components/wallet";
|
import { getServerSession } from "next-auth";
|
||||||
import { NuqsAdapter } from 'nuqs/adapters/next/app'
|
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 { getProfile } from "@/actions/payment";
|
||||||
import { authOptions } from "@/app/auth";
|
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 { Separator } from "@/components/ui/separator";
|
||||||
import {
|
import {
|
||||||
SidebarInset,
|
SidebarInset,
|
||||||
SidebarProvider,
|
SidebarProvider,
|
||||||
SidebarTrigger,
|
SidebarTrigger,
|
||||||
} from "@/components/ui/sidebar";
|
} from "@/components/ui/sidebar";
|
||||||
|
import { Wallet } from "@/components/wallet";
|
||||||
import { tryCatch } from "@/utils/tryCatch";
|
import { tryCatch } from "@/utils/tryCatch";
|
||||||
import { getServerSession } from "next-auth";
|
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
import { WelcomeBanner } from "../welcome-banner";
|
import { WelcomeBanner } from "../welcome-banner";
|
||||||
import { AccountPopover } from "./account-popver";
|
import { AccountPopover } from "./account-popver";
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ import {
|
|||||||
UsersRound,
|
UsersRound,
|
||||||
Wallet2Icon,
|
Wallet2Icon,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { getServerSession } from "next-auth";
|
||||||
import { authOptions } from "@/app/auth";
|
import { authOptions } from "@/app/auth";
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
@ -28,8 +29,6 @@ import {
|
|||||||
SidebarMenuItem,
|
SidebarMenuItem,
|
||||||
SidebarRail,
|
SidebarRail,
|
||||||
} from "@/components/ui/sidebar";
|
} from "@/components/ui/sidebar";
|
||||||
import { getServerSession } from "next-auth";
|
|
||||||
import Link from "next/link";
|
|
||||||
|
|
||||||
type Permission = {
|
type Permission = {
|
||||||
id: number;
|
id: number;
|
||||||
@ -40,17 +39,17 @@ type Categories = {
|
|||||||
id: string;
|
id: string;
|
||||||
children: (
|
children: (
|
||||||
| {
|
| {
|
||||||
title: string;
|
title: string;
|
||||||
link: string;
|
link: string;
|
||||||
perm_identifier: string;
|
perm_identifier: string;
|
||||||
icon: React.JSX.Element;
|
icon: React.JSX.Element;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
title: string;
|
title: string;
|
||||||
link: string;
|
link: string;
|
||||||
icon: React.JSX.Element;
|
icon: React.JSX.Element;
|
||||||
perm_identifier?: undefined;
|
perm_identifier?: undefined;
|
||||||
}
|
}
|
||||||
)[];
|
)[];
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
@ -128,27 +127,31 @@ export async function AppSidebar({
|
|||||||
|
|
||||||
const session = await getServerSession(authOptions);
|
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;
|
let CATEGORIES: Categories;
|
||||||
if (session?.user?.is_superuser) {
|
if (session?.user?.is_admin) {
|
||||||
CATEGORIES = categories;
|
CATEGORIES = categories;
|
||||||
} else {
|
} 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 (
|
return (
|
||||||
|
Reference in New Issue
Block a user