Files
temetro/frontend/components/sidebar-02/nav-main.tsx
T
Khalid Abdi 346954db3f Collapse sidebar footer into the user menu; tweak sub-nav & appointments
- Footer is now a single user row. The ⌘K command hint moved into the user
  menu below Theme, and the clinic switcher became a hover submenu below that
  (revealing the active clinic's name/slug/count, the switch list, and
  Create clinic). Removed SidebarCommandButton and deleted team-switcher.tsx.
- Patients sub-nav (Patients / Appointments & Schedule): no icons, no
  background change on hover/active — only the text color changes.
- Appointments & Schedule: added an "Add" button that opens the create-patient
  dialog.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 01:09:08 +03:00

108 lines
3.5 KiB
TypeScript

"use client";
import {
SidebarMenu,
SidebarMenuAction,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
useSidebar,
} from "@/components/ui/sidebar";
import { cn } from "@/lib/utils";
import { ChevronDown } from "lucide-react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import type React from "react";
import { useState } from "react";
export type Route = {
id: string;
title: string;
icon?: React.ReactNode;
link: string;
subs?: {
title: string;
link: string;
icon?: React.ReactNode;
}[];
};
// True when `link` matches the current path. "/" only matches exactly; any other
// link matches itself and its nested routes (so a parent stays lit on subpages).
function useIsActive() {
const pathname = usePathname();
return (link: string) =>
link === "/" ? pathname === "/" : pathname === link || pathname.startsWith(`${link}/`);
}
export default function DashboardNavigation({ routes }: { routes: Route[] }) {
const { state } = useSidebar();
const isCollapsed = state === "collapsed";
const isActive = useIsActive();
// Manual expand/collapse override per parent; falls back to "open when active".
const [overrides, setOverrides] = useState<Record<string, boolean>>({});
return (
<SidebarMenu>
{routes.map((route) => {
const hasSubs = !!route.subs?.length;
const sectionActive =
isActive(route.link) || !!route.subs?.some((s) => isActive(s.link));
const isOpen = !isCollapsed && (overrides[route.id] ?? sectionActive);
return (
<SidebarMenuItem key={route.id}>
<SidebarMenuButton
className="text-muted-foreground"
isActive={sectionActive}
render={<Link href={route.link} prefetch={true} />}
tooltip={route.title}
>
{route.icon}
{!isCollapsed && (
<span className="ml-2 flex-1 truncate font-medium text-sm">
{route.title}
</span>
)}
</SidebarMenuButton>
{hasSubs && !isCollapsed && (
<SidebarMenuAction
aria-label={isOpen ? "Collapse" : "Expand"}
onClick={() =>
setOverrides((prev) => ({ ...prev, [route.id]: !isOpen }))
}
>
<ChevronDown
className={cn(
"transition-transform duration-200",
isOpen && "rotate-180"
)}
/>
</SidebarMenuAction>
)}
{hasSubs && isOpen && (
<SidebarMenuSub>
{route.subs?.map((subRoute) => (
<SidebarMenuSubItem key={`${route.id}-${subRoute.link}`}>
<SidebarMenuSubButton
className="text-muted-foreground hover:bg-transparent hover:text-foreground active:bg-transparent data-[active=true]:bg-transparent data-[active=true]:font-medium data-[active=true]:text-foreground"
isActive={isActive(subRoute.link)}
render={<Link href={subRoute.link} prefetch={true} />}
>
<span className="truncate">{subRoute.title}</span>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
)}
</SidebarMenuItem>
);
})}
</SidebarMenu>
);
}