'use strict';
import { routes } from '../../config'
import articles from '../../articles'
import {useRouter} from 'next/router'
import Link from 'next/link'
export default props => {
const { asPath } = useRouter()
const routesArray = asPath.split('/')
const breadcrumbs = Array.from(new Set(routesArray.map((item) => {
let path = item === `` ? `/` : `/${item}`
let part = Object.values(routes).find(item => item.href === path)
if (!part) {
const article = articles.find(art => art.id === +item)
if (article) {
const { id, title } = article
return {
href: `/articles/[id]`,
as: `/articles/${id}`,
title
}
}
} else return part
})))
return <div className="breadcrumbs"><ul>{
breadcrumbs.map(({ title, href, as }, index, arr) => (index === arr.length -1)
? <li key={index}><span>{ title }</span></li>
: <li key={index}>
<Link href={href} as={as}>
<a>{ title }</a>
</Link>
</li>
)
}</ul></div>
}
return <div className="breadcrumbs"><ul>{
breadcrumbs.map(({ title, href, as }, index, arr) => (index === arr.length -1)
? <li key={index}><span>{ title }</span></li>
: <li key={index}>
<Link href={href} as={as}>
<a>{ title }</a>
</Link>
</li>
)
}</ul></div>