Skip to content
Snippets Groups Projects
nav.tsx 2.82 KiB
Newer Older
import { Icons, IconsType } from "@/components/icons";
import { buttonVariants } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { SidebarNavItem } from "@/types";
import { UserButton, useUser } from "@clerk/nextjs";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { ModeToggle } from "./mode-toggle";

interface DashboardNavProps {
    items: SidebarNavItem[]
}

export default function DashboardNav({ items }: DashboardNavProps) {
    const path = usePathname()

    const { isLoaded, user } = useUser()

    if (!items?.length) {
        return null
    }

    return (
        <nav className="grid items-start gap-2">
            <div className="flex items-center">
                <Link href="/" className={cn("rounded-full p-3 hover:bg-accent")}>
                    <Icons.logo className="h-7 w-7 dark:hidden" />
                    <Icons.logoWhite className="h-7 w-7 hidden dark:block" />
                </Link>
            </div>
            {isLoaded && user ?
                (items.map((item, index) => {
                    const Icon = Icons[item.icon as keyof IconsType || "arrowRight"];
                    if (item.title === "My Profile") {
                        item.href = `/${user.username}`
                    }
                    return (
                        item.href && (
                            <Link key={index} href={item.disabled ? "/" : item.href} className={index == 6 ? "mt-10" : ""}>
                                <span
                                    className={cn(
                                        "group flex items-center rounded-md px-3 py-2 font-medium hover:bg-accent hover:text-accent-foreground",
                                        path === item.href ? "bg-accent" : "transparent",
                                        item.disabled && "cursor-not-allowed opacity-80"
                                    )}
                                >
                                    <Icon className="mr-2 h-4 w-4" />
                                    <span>{item.title}</span>
                                </span>
                            </Link>
                        )
                    )
                }))
                :
                <div className="space-x-2 space-y-2 justify-center text-center">
                    <Link href="/login" className={cn(buttonVariants({ size: "lg" }))}>Log In</Link>
                    <Link href="/signup" className={cn(buttonVariants({ size: "lg", variant: "outline" }))}>Sign Up</Link>
                    <p>
                        Unlock endless possibilities - register or log in to unleash the full potential of our website.
                    </p>
                </div>
            }
            <UserButton afterSignOutUrl="/" />