refactor: add tryCatch utility for error handling, update device-related components and types, and clean up unused code in payment actions
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 13m55s

This commit is contained in:
2025-04-05 16:07:11 +05:00
parent dbdc1df7d5
commit aa18484475
16 changed files with 641 additions and 599 deletions

View File

@ -10,6 +10,7 @@ import {
Wallet2Icon,
} from "lucide-react";
import { authOptions } from "@/app/auth";
import {
Collapsible,
CollapsibleContent,
@ -27,76 +28,130 @@ import {
SidebarMenuItem,
SidebarRail,
} from "@/components/ui/sidebar";
import { getServerSession } from "next-auth";
import Link from "next/link";
const data = {
navMain: [
type Permission = {
id: number;
name: string;
};
type Categories = {
id: string;
children: (
| {
title: string;
link: string;
perm_identifier: string;
icon: React.JSX.Element;
}
| {
title: string;
link: string;
icon: React.JSX.Element;
perm_identifier?: undefined;
}
)[];
}[];
export async function AppSidebar({
role,
...props
}: React.ComponentProps<typeof Sidebar>) {
const categories = [
{
title: "MENU",
id: "MENU",
url: "#",
requiredRoles: ["ADMIN", "USER"],
items: [
children: [
{
title: "Devices",
url: "/devices",
link: "/devices",
perm_identifier: "device",
icon: <Smartphone size={16} />,
},
{
title: "Payments",
url: "/payments",
link: "/payments",
icon: <CreditCard size={16} />,
perm_identifier: "payment",
},
{
title: "Parental Control",
url: "/parental-control",
link: "/parental-control",
icon: <CreditCard size={16} />,
perm_identifier: "device",
},
{
title: "Agreements",
url: "/agreements",
link: "/agreements",
icon: <Handshake size={16} />,
perm_identifier: "device",
},
{
title: "Wallet",
url: "/wallet",
link: "/wallet",
icon: <Wallet2Icon size={16} />,
perm_identifier: "wallet",
},
],
},
{
title: "ADMIN CONTROL",
id: "ADMIN CONTROL",
url: "#",
requiredRoles: ["ADMIN"],
items: [
children: [
{
title: "Users",
url: "/users",
link: "/users",
icon: <UsersRound size={16} />,
perm_identifier: "device",
},
{
title: "User Devices",
url: "/user-devices",
link: "/user-devices",
icon: <MonitorSpeaker size={16} />,
perm_identifier: "device",
},
{
title: "User Payments",
url: "/user-payments",
link: "/user-payments",
icon: <Coins size={16} />,
perm_identifier: "payment",
},
{
title: "Price Calculator",
url: "/price-calculator",
link: "/price-calculator",
icon: <Calculator size={16} />,
perm_identifier: "device",
},
],
},
],
};
];
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) {
CATEGORIES = categories;
} else {
CATEGORIES = filteredCategoriesWithChildren;
}
export function AppSidebar({
role,
...props
}: React.ComponentProps<typeof Sidebar> & { role: string }) {
return (
<Sidebar {...props} className="z-50">
<SidebarHeader>
@ -105,53 +160,46 @@ export function AppSidebar({
</h4>
</SidebarHeader>
<SidebarContent className="gap-0">
{data.navMain
.filter(
(item) =>
!item.requiredRoles || item.requiredRoles.includes(role || ""),
)
.map((item) => {
if (item.requiredRoles?.includes(role)) {
return (
<Collapsible
key={item.title}
title={item.title}
defaultOpen
className="group/collapsible"
{CATEGORIES.map((item) => {
return (
<Collapsible
key={item.id}
title={item.id}
defaultOpen
className="group/collapsible"
>
<SidebarGroup>
<SidebarGroupLabel
asChild
className="group/label text-sm text-sidebar-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground"
>
<SidebarGroup>
<SidebarGroupLabel
asChild
className="group/label text-sm text-sidebar-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground"
>
<CollapsibleTrigger>
{item.title}{" "}
<ChevronRight className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-90" />
</CollapsibleTrigger>
</SidebarGroupLabel>
<CollapsibleContent>
<SidebarGroupContent>
<SidebarMenu>
{item.items.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton className="py-6" asChild>
<Link className="text-md" href={item.url}>
{item.icon}
<span className="opacity-70 ml-2">
{item.title}
</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</CollapsibleContent>
</SidebarGroup>
</Collapsible>
);
}
})}
<CollapsibleTrigger>
{item.id}{" "}
<ChevronRight className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-90" />
</CollapsibleTrigger>
</SidebarGroupLabel>
<CollapsibleContent>
<SidebarGroupContent>
<SidebarMenu>
{item.children.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton className="py-6" asChild>
<Link className="text-md" href={item.link}>
{item.icon}
<span className="opacity-70 ml-2">
{item.title}
</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</CollapsibleContent>
</SidebarGroup>
</Collapsible>
);
})}
</SidebarContent>
<SidebarRail />
</Sidebar>