2022-07-23 00:39:50 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Children, Fragment, useEffect, useState } from "react";
|
|
|
|
|
|
|
|
type BreadcrumbProps = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
};
|
|
|
|
export const Breadcrumb = ({ children }: BreadcrumbProps) => {
|
|
|
|
const childrenArray = Children.toArray(children);
|
|
|
|
|
|
|
|
const childrenSeperated = childrenArray.map((child, index) => {
|
|
|
|
// If not the last item in the array insert a /
|
|
|
|
if (index !== childrenArray.length - 1) {
|
|
|
|
return (
|
|
|
|
<Fragment key={index}>
|
|
|
|
{child}
|
|
|
|
<span>/</span>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Else return just the child
|
|
|
|
return child;
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2022-07-27 22:12:27 +00:00
|
|
|
<nav className="text-sm font-normal leading-5 text-gray-600">
|
2023-01-04 07:38:45 +00:00
|
|
|
<ol className="flex items-center space-x-2 rtl:space-x-reverse">{childrenSeperated}</ol>
|
2022-07-23 00:39:50 +00:00
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
type BreadcrumbItemProps = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
href: string;
|
|
|
|
listProps?: JSX.IntrinsicElements["li"];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BreadcrumbItem = ({ children, href, listProps }: BreadcrumbItemProps) => {
|
|
|
|
return (
|
|
|
|
<li {...listProps}>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href={href}>{children}</Link>
|
2022-07-23 00:39:50 +00:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BreadcrumbContainer = () => {
|
|
|
|
const router = useRouter();
|
2023-01-06 12:13:56 +00:00
|
|
|
const [, setBreadcrumbs] = useState<{ href: string; label: string }[]>();
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const rawPath = router.asPath.split("?")[0]; // this will ignore any query params for now?
|
|
|
|
|
|
|
|
let pathArray = rawPath.split("/");
|
|
|
|
pathArray.shift();
|
|
|
|
|
|
|
|
pathArray = pathArray.filter((path) => path !== "");
|
|
|
|
|
|
|
|
const allBreadcrumbs = pathArray.map((path, idx) => {
|
|
|
|
const href = `/${pathArray.slice(0, idx + 1).join("/")}`;
|
|
|
|
return {
|
|
|
|
href,
|
|
|
|
label: path.charAt(0).toUpperCase() + path.slice(1),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
setBreadcrumbs(allBreadcrumbs);
|
2022-07-27 22:12:27 +00:00
|
|
|
}, [router.asPath]);
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
export default Breadcrumb;
|