import { EachRoute } from "@/lib/routes-config";
import Anchor from "./anchor";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { cn } from "@/lib/utils";
import { SheetClose } from "@/components/ui/sheet";
import { ChevronDown, ChevronRight } from "lucide-react";
import { useEffect, useState, useMemo } from "react";
import { usePathname } from "next/navigation";
interface SubLinkProps extends EachRoute {
level: number;
isSheet: boolean;
parentHref?: string;
}
export default function SubLink({
title,
href,
items,
noLink,
level,
isSheet,
parentHref = "",
}: SubLinkProps) {
const path = usePathname();
const [isOpen, setIsOpen] = useState(level === 0);
// Full path including parent's href
const fullHref = `${parentHref}${href}`;
// Check if current path exactly matches this link's href
const isExactActive = useMemo(() => path === fullHref, [path, fullHref]);
// Check if any child is active (for parent items)
const hasActiveChild = useMemo(() => {
if (!items) return false;
return items.some(item => {
const childHref = `${fullHref}${item.href}`;
return path.startsWith(childHref) && path !== fullHref;
});
}, [items, path, fullHref]);
// Auto-expand if current path is a child of this item
useEffect(() => {
if (items && (path.startsWith(fullHref) && path !== fullHref)) {
setIsOpen(true);
}
}, [path, fullHref, items]);
// Only apply active styles if it's an exact match and not a parent with active children
const Comp = useMemo(() => (