114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
// // "use client";
|
|
|
|
import { type ReactNode } from "react";
|
|
import { Fragment } from "react/jsx-runtime";
|
|
import { SidebarGroupDto } from "~/application/sidebar/SidebarGroupDto";
|
|
|
|
import {
|
|
SidebarGroup,
|
|
SidebarGroupLabel,
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
SidebarMenuSub,
|
|
SidebarMenuSubButton,
|
|
SidebarMenuSubItem,
|
|
} from "~/components/ui/sidebar";
|
|
import SidebarIcon from "../../icons/SidebarIcon";
|
|
import { getUiIdAttributes } from "~/application/ui/uiRegistry";
|
|
|
|
type SidebarNavItem = SidebarGroupDto["items"][number];
|
|
|
|
function getEnabledItems(items: SidebarNavItem[] = []): SidebarNavItem[] {
|
|
return items.filter((item) => !item.disabled);
|
|
}
|
|
|
|
function hasConfiguredChildren(item: SidebarNavItem) {
|
|
return (item.items?.length ?? 0) > 0;
|
|
}
|
|
|
|
function SidebarLink({ to, children, ...props }: { to: string; children: ReactNode } & Record<string, unknown>) {
|
|
return (
|
|
<a href={to} {...props}>
|
|
{children}
|
|
</a>
|
|
);
|
|
}
|
|
|
|
function SidebarItemContent({ item }: { item: SidebarNavItem }) {
|
|
return (
|
|
<>
|
|
{item.icon !== undefined && <SidebarIcon className="size-4" item={item} />}
|
|
<span>{item.title}</span>
|
|
</>
|
|
);
|
|
}
|
|
|
|
// Subframe SidebarWithSections is flat: a parent that groups children is a
|
|
// (non-interactive) section header — like Subframe's NavSection label — with its
|
|
// sub-items always visible beneath it. No collapse/accordion, no chevron, and
|
|
// the parent is not a link (these parents never navigated; they were toggles).
|
|
function NavMainExpandedItem({ item }: { item: SidebarNavItem }) {
|
|
const subItems = getEnabledItems(item.items);
|
|
return (
|
|
<>
|
|
<SidebarMenuItem>
|
|
<div
|
|
data-sidebar-group-parent=""
|
|
className="text-sidebar-foreground/80 flex w-full items-center gap-2.5 rounded-md px-3 py-2 text-base font-medium sm:text-sm [&>svg]:size-4 [&>svg]:shrink-0"
|
|
{...getUiIdAttributes(item)}
|
|
>
|
|
<SidebarItemContent item={item} />
|
|
</div>
|
|
</SidebarMenuItem>
|
|
<SidebarMenuSub>
|
|
{subItems.map((subItem) => (
|
|
<SidebarMenuSubItem key={subItem.title}>
|
|
<SidebarMenuSubButton asChild isActive={subItem.isActive} className="h-9 text-base sm:h-8 sm:text-sm">
|
|
<SidebarLink to={subItem.redirectTo ?? subItem.path} {...getUiIdAttributes(subItem)}>
|
|
<span>{subItem.title}</span>
|
|
</SidebarLink>
|
|
</SidebarMenuSubButton>
|
|
</SidebarMenuSubItem>
|
|
))}
|
|
</SidebarMenuSub>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function NavMain({ items }: { items: SidebarGroupDto[] }) {
|
|
return (
|
|
<Fragment>
|
|
{items.map((group, idxGroup) => (
|
|
<SidebarGroup key={idxGroup} className="p-0 px-2" {...getUiIdAttributes(group)}>
|
|
{group.title && <SidebarGroupLabel>{group.title}</SidebarGroupLabel>}
|
|
<SidebarMenu>
|
|
{getEnabledItems(group.items).map((item, idx) => {
|
|
const enabledChildren = getEnabledItems(item.items);
|
|
if (hasConfiguredChildren(item) && enabledChildren.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<Fragment key={idx}>
|
|
{enabledChildren.length === 0 ? (
|
|
<SidebarMenuItem>
|
|
<SidebarMenuButton asChild isActive={item.isActive} size="default" className="h-9 text-base font-medium sm:h-8 sm:text-sm">
|
|
<SidebarLink to={item.path} {...getUiIdAttributes(item)}>
|
|
<SidebarItemContent item={item} />
|
|
</SidebarLink>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
) : (
|
|
<NavMainExpandedItem item={item} />
|
|
)}
|
|
</Fragment>
|
|
);
|
|
})}
|
|
</SidebarMenu>
|
|
</SidebarGroup>
|
|
))}
|
|
</Fragment>
|
|
);
|
|
}
|