2021-05-06 00:54:49 +03:00
|
|
|
import cx from 'classnames'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useMainContext } from './context/MainContext'
|
2021-06-03 18:21:33 +03:00
|
|
|
import { Link } from 'components/Link'
|
2021-05-06 00:54:49 +03:00
|
|
|
|
|
|
|
export type BreadcrumbT = {
|
|
|
|
title: string
|
|
|
|
documentType?: string
|
|
|
|
href?: string
|
|
|
|
}
|
|
|
|
|
2021-06-15 21:16:24 +03:00
|
|
|
type Props = {
|
|
|
|
variant?: 'default' | 'large'
|
|
|
|
}
|
|
|
|
export const Breadcrumbs = ({ variant = 'default' }: Props) => {
|
2021-05-06 00:54:49 +03:00
|
|
|
const router = useRouter()
|
2021-06-02 19:28:39 +03:00
|
|
|
const pathWithLocale = `/${router.locale}${router.asPath.split('?')[0]}` // remove query string
|
2021-05-06 00:54:49 +03:00
|
|
|
const { breadcrumbs } = useMainContext()
|
2021-06-10 08:07:30 +03:00
|
|
|
|
2021-05-06 00:54:49 +03:00
|
|
|
return (
|
2021-07-29 20:27:20 +03:00
|
|
|
<nav data-testid="breadcrumbs" className="f5" aria-label="Breadcrumb">
|
|
|
|
{Object.values(breadcrumbs).map((breadcrumb, i, arr) => {
|
2021-05-19 05:49:51 +03:00
|
|
|
if (!breadcrumb) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:54:49 +03:00
|
|
|
const title = `${breadcrumb.documentType}: ${breadcrumb.title}`
|
2021-07-29 20:27:20 +03:00
|
|
|
return [
|
|
|
|
!breadcrumb.href ? (
|
|
|
|
<span data-testid="breadcrumb-title" key={title} title={title} className="px-2">
|
|
|
|
{breadcrumb.title}
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<Link
|
|
|
|
key={title}
|
|
|
|
data-testid="breadcrumb-link"
|
|
|
|
href={breadcrumb.href}
|
|
|
|
title={title}
|
|
|
|
className={cx(
|
|
|
|
'd-inline-block px-2',
|
|
|
|
variant === 'large' && 'text-uppercase text-mono',
|
|
|
|
pathWithLocale === breadcrumb.href && 'color-text-tertiary'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{breadcrumb.title}
|
|
|
|
</Link>
|
|
|
|
),
|
|
|
|
i !== arr.length - 1 ? <span className="color-text-tertiary">/</span> : null,
|
|
|
|
]
|
2021-05-06 00:54:49 +03:00
|
|
|
})}
|
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|