Skip to content

Commit

Permalink
feat(web): issue detail 页面加入 breadcrumb
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyao27 committed Jun 8, 2022
1 parent 8631806 commit 22e061e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/web/src/components/breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Link from 'next/link'
import type { FC } from 'react'
import useBreadcrumb from '~/hooks/useBreadcrumb'

const Breadcrumb: FC = () => {
const [breadcrumbs] = useBreadcrumb()

return (
<div className="text-sm breadcrumbs">
<ul>
{
breadcrumbs
.slice(0, breadcrumbs.length - 1)
.map(v => (<li key={v.path}><Link href={v.path}><a>{v.breadcrumb}</a></Link></li>))
}
<li>{breadcrumbs.at(-1)?.breadcrumb}</li>
</ul>
</div>
)
}

export default Breadcrumb
27 changes: 27 additions & 0 deletions packages/web/src/hooks/useBreadcrumb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'

interface Breadcrumb {
breadcrumb: string
path: string
}
export default function useBreadcrumb() {
const router = useRouter()
const [breadcrumbs, setBreadcrumbs] = useState<Breadcrumb[]>([])
useEffect(() => {
if (router) {
const linkPath = router.asPath.split('/')
linkPath.shift()

const pathArray = linkPath.map((path, i) => {
return {
breadcrumb: path,
path: `/${linkPath.slice(0, i + 1).join('/')}`,
}
})
setBreadcrumbs(pathArray)
}
}, [router])

return [breadcrumbs] as const
}
3 changes: 3 additions & 0 deletions packages/web/src/pages/issues/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GetServerSideProps, NextPage } from 'next'
import type { Issue, OhbugEventLike } from 'types'
import Breadcrumb from '~/components/breadcrumb'
import IssueDetailActions from '~/components/issueDetailAction'
import IssueDetailProfile from '~/components/issueDetailProfile'
import IssueDetailStack from '~/components/issueDetailStack'
Expand Down Expand Up @@ -39,6 +40,8 @@ export const getServerSideProps: GetServerSideProps<Props> = async(context) => {
const Detail: NextPage<Props> = ({ issue, event, trends }) => {
return (
<div>
<Breadcrumb />

<IssueDetailTitle issue={issue} />

<IssueDetailProfile event={event} />
Expand Down

0 comments on commit 22e061e

Please sign in to comment.