"use client"; import { useEffect, useState } from "react"; import { usePathname, useRouter } from "next/navigation"; import { ArticleIcon, DashboardIcon, MagazineIcon, MasterCategoryIcon, MasterRoleIcon, MasterUsersIcon, StaticPageIcon, } from "../icons/sidebar-icon"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbSeparator, } from "../ui/breadcrumb"; import React from "react"; import { motion } from "framer-motion"; export const Breadcrumbs = () => { const [currentPage, setCurrentPage] = useState(""); const [mounted, setMounted] = useState(false); const router = useRouter(); const pathname = usePathname(); const pathnameSplit = pathname.split("/"); pathnameSplit.shift(); const pathnameTransformed = pathnameSplit.map((item) => { const words = item.split("-"); const capitalizedWords = words.map( (word) => word.charAt(0).toUpperCase() + word.slice(1), ); return capitalizedWords.join(" "); }); useEffect(() => { setMounted(true); }, []); useEffect(() => { setCurrentPage(pathnameSplit[pathnameSplit.length - 1]); }, [pathnameSplit]); const handleAction = (key: any) => { const keyIndex = pathnameSplit.indexOf(key); const combinedPath = pathnameSplit.slice(0, keyIndex + 1).join("/"); router.push("/" + combinedPath); }; const getPageIcon = () => { if (pathname.includes("dashboard")) return ; if (pathname.includes("article")) return ; if (pathname.includes("master-category")) return ; if (pathname.includes("magazine")) return ; if (pathname.includes("static-page")) return ; if (pathname.includes("master-user")) return ; if (pathname.includes("master-role")) return ; return null; }; if (!mounted) { return (
); } return ( {/* Page Icon */} {getPageIcon()} {/* Page Title and Breadcrumbs */}
{pathnameTransformed[pathnameTransformed.length - 1]} {pathnameTransformed ?.filter((item) => item !== "Admin") .map((item, index, array) => ( handleAction(pathnameSplit[index])} className={`text-sm transition-all duration-200 hover:text-blue-600 ${ pathnameSplit[index] === currentPage ? "font-semibold text-blue-600" : "text-slate-500 hover:text-slate-700" }`} > {item} {index < array.length - 1 && ( )} ))}
); };