187 lines
11 KiB
TypeScript
187 lines
11 KiB
TypeScript
// * Packages
|
|
import { Link, usePage } from "@inertiajs/react";
|
|
import { useEffect, useRef } from "react";
|
|
import { ChevronDown, MoreHorizontal, Home, Users, SquareArrowOutUpRight, Telescope } from "lucide-react";
|
|
// * Components shadcn
|
|
import { Sidebar, SidebarHeader, SidebarContent, SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarFooter, SidebarMenuAction, useSidebar } from "@/components/ui/sidebar";
|
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
|
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "@/components/ui/collapsible";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { Button } from "@/components/ui/button";
|
|
// * Other
|
|
import ApplicationLogo from "@/back/Components/ApplicationLogo";
|
|
import { checkConceptor, Views } from "@/lib/utils";
|
|
import str from "@/hooks/use-string";
|
|
import trans from "@/hooks/use-translations";
|
|
import { AuthProp } from "@/back/types/global";
|
|
|
|
export function AppSidebar() {
|
|
const appMode = import.meta.env.VITE_MODE
|
|
const { state } = useSidebar();
|
|
const prevStateRef = useRef(state);
|
|
const isCollapsed = state === "collapsed";
|
|
const auth = usePage().props.auth as AuthProp;
|
|
const navList = [
|
|
{
|
|
title: trans("back.pages"),
|
|
show: true,
|
|
links: [
|
|
{
|
|
title: trans("back.dashboard"),
|
|
model: "dashboard",
|
|
show: true,
|
|
icon: Home,
|
|
actions: [],
|
|
},
|
|
{
|
|
title: trans("back.your_website"),
|
|
link: route("front.homepage"),
|
|
show: true,
|
|
icon: SquareArrowOutUpRight,
|
|
actions: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: trans("back.administration"),
|
|
show: checkConceptor(),
|
|
links: [
|
|
{
|
|
title: str(trans("models.classes.user")).ucFirst().plural().value(),
|
|
model: "users",
|
|
icon: Users,
|
|
show: auth.policies.users['viewAny'],
|
|
actions: [Views.CREATION],
|
|
},
|
|
{
|
|
title: trans("back.telescope"),
|
|
link: route("telescope"),
|
|
icon: Telescope,
|
|
show: appMode === "local",
|
|
actions: [],
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
useEffect(() => {
|
|
if (prevStateRef.current !== state) {
|
|
const elmts = document.querySelectorAll(".sidebar-transition");
|
|
elmts.forEach((element) => {
|
|
element.classList.add("animation-transition");
|
|
setTimeout(() => {
|
|
element.classList.remove("animation-transition");
|
|
}, 400);
|
|
});
|
|
prevStateRef.current = state;
|
|
}
|
|
}, [state]);
|
|
|
|
return (
|
|
<Sidebar collapsible="icon" className="z-30">
|
|
<SidebarHeader className="sidebar-transition">
|
|
<ApplicationLogo isCollapsed={isCollapsed} />
|
|
</SidebarHeader>
|
|
<SidebarContent className={`sidebar-transition ${isCollapsed ? "pt-2" : ""}`}>
|
|
{navList.map((navGroup, navGroupIndex) => navGroup.show && (
|
|
<Collapsible
|
|
key={navGroupIndex}
|
|
className="group/collapsible"
|
|
defaultOpen
|
|
>
|
|
<SidebarGroup>
|
|
{!isCollapsed && (
|
|
<SidebarGroupLabel asChild>
|
|
<CollapsibleTrigger>
|
|
{navGroup.title}
|
|
<ChevronDown className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-180" />
|
|
</CollapsibleTrigger>
|
|
</SidebarGroupLabel>
|
|
)}
|
|
<CollapsibleContent>
|
|
<SidebarGroupContent>
|
|
<SidebarMenu>
|
|
{navGroup.links.map((navLink, navLinkIndex) => {
|
|
const sidebarButton = (
|
|
<SidebarMenuButton className={`${route().current(`back.${navLink.model}.*`) ? 'text-secondary-foreground' : 'text-primary-foreground'}`} asChild>
|
|
{navLink.link ? (
|
|
<a
|
|
href={navLink.link}
|
|
target="_blank"
|
|
>
|
|
<navLink.icon />
|
|
{navLink.title}
|
|
</a>
|
|
) : (
|
|
<Link
|
|
href={route(`back.${navLink.model}.index`)}
|
|
className="bg-transparent"
|
|
>
|
|
<navLink.icon />
|
|
{navLink.title}
|
|
</Link>
|
|
)}
|
|
</SidebarMenuButton>
|
|
);
|
|
return navLink.show && (
|
|
<SidebarMenuItem
|
|
className={`transition-colors rounded-md ${route().current(`back.${navLink.model}.*`) ? 'bg-secondary text-secondary-foreground' : 'bg-opacity-0'}`}
|
|
key={navLinkIndex}
|
|
>
|
|
{isCollapsed ? (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
{sidebarButton}
|
|
</TooltipTrigger>
|
|
<TooltipContent side="right">
|
|
<p>{navLink.title}</p>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
) : (sidebarButton)}
|
|
{navLink.actions.length > 0 && (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<SidebarMenuAction className={
|
|
`${route().current(`back.${navLink.model}.*`) ? 'text-secondary-foreground' : ''} !top-0 !right-0 px-4 h-full`}>
|
|
<MoreHorizontal />
|
|
</SidebarMenuAction>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent
|
|
side="right"
|
|
align="start"
|
|
>
|
|
{navLink.actions.map((action, actionIndex) => auth.policies.users[action.policy] && (
|
|
<DropdownMenuItem
|
|
className="p-0"
|
|
key={actionIndex}
|
|
>
|
|
<Button
|
|
variant="ghost"
|
|
asChild
|
|
>
|
|
<Link
|
|
href={route(`back.${navLink.model}.${action.value}`)}
|
|
className="w-full"
|
|
>
|
|
<action.icon />
|
|
{str(trans("crud.actions." + action.label)).ucFirst().value()}
|
|
</Link>
|
|
</Button>
|
|
</DropdownMenuItem>
|
|
))}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)}
|
|
</SidebarMenuItem>
|
|
);
|
|
})}
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</CollapsibleContent>
|
|
</SidebarGroup>
|
|
</Collapsible>
|
|
))}
|
|
</SidebarContent>
|
|
</Sidebar>
|
|
);
|
|
}
|