|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useMemo } from "react" |
|
4
|
import clsx from "clsx" |
|
5
|
import Link from "next/link" |
|
6
|
import { useSidebar } from "@/providers/Sidebar" |
|
7
|
import { useSiteConfig } from "@/providers/SiteConfig" |
|
8
|
import { Button } from "../Button" |
|
9
|
import { TriangleRightMini } from "@medusajs/icons" |
|
10
|
import { Sidebar } from "types" |
|
11
|
import { getJsonLd } from "@/utils/get-json-ld" |
|
12
|
import type { BreadcrumbList } from "schema-dts" |
|
13
|
|
|
14
|
type BreadcrumbItems = { |
|
15
|
title: string |
|
16
|
link: string |
|
17
|
}[] |
|
18
|
|
|
19
|
export const Breadcrumbs = () => { |
|
20
|
const { sidebarHistory, getSidebarFirstLinkChild, getSidebar } = useSidebar() |
|
21
|
const { |
|
22
|
config: { breadcrumbOptions, baseUrl, basePath }, |
|
23
|
} = useSiteConfig() |
|
24
|
|
|
25
|
const getLinkPath = (item: Sidebar.SidebarItemLink): string => { |
|
26
|
return item.path |
|
27
|
} |
|
28
|
|
|
29
|
const breadcrumbItems = useMemo(() => { |
|
30
|
const items: BreadcrumbItems = [] |
|
31
|
if (breadcrumbOptions?.startItems) { |
|
32
|
items.push(...breadcrumbOptions.startItems) |
|
33
|
} |
|
34
|
|
|
35
|
sidebarHistory.forEach((sidebar_id) => { |
|
36
|
const sidebar = getSidebar(sidebar_id) |
|
37
|
|
|
38
|
if (!sidebar) { |
|
39
|
return |
|
40
|
} |
|
41
|
|
|
42
|
const sidebarFirstChild = getSidebarFirstLinkChild(sidebar) |
|
43
|
|
|
44
|
if (!sidebarFirstChild) { |
|
45
|
return |
|
46
|
} |
|
47
|
|
|
48
|
items.push({ |
|
49
|
title: sidebar.title, |
|
50
|
link: getLinkPath(sidebarFirstChild), |
|
51
|
}) |
|
52
|
}) |
|
53
|
|
|
54
|
// make sure items are unique (no duplicate links) |
|
55
|
const uniqueItems = items.filter( |
|
56
|
(item, index, self) => |
|
57
|
index === self.findIndex((t) => t.link === item.link) |
|
58
|
) |
|
59
|
return uniqueItems |
|
60
|
}, [sidebarHistory, breadcrumbOptions]) |
|
61
|
|
|
62
|
const jsonLd = useMemo(() => { |
|
63
|
const baseLink = `${baseUrl}${basePath}`.replace(/\/+$/, "") |
|
64
|
return getJsonLd<BreadcrumbList>({ |
|
65
|
"@context": "https://schema.org", |
|
66
|
"@type": "BreadcrumbList", |
|
67
|
itemListElement: breadcrumbItems.map((item, index) => ({ |
|
68
|
"@type": "ListItem", |
|
69
|
position: index + 1, |
|
70
|
name: item.title, |
|
71
|
item: item.link.startsWith("#") |
|
72
|
? baseLink |
|
73
|
: item.link.startsWith("/") |
|
74
|
? `${baseLink}${item.link}` |
|
75
|
: item.link, |
|
76
|
})), |
|
77
|
}) |
|
78
|
}, [breadcrumbItems, baseUrl, basePath]) |
|
79
|
|
|
80
|
return ( |
|
81
|
<div |
|
82
|
className={clsx( |
|
83
|
"flex items-center gap-docs_0.25", |
|
84
|
"text-medusa-fg-muted text-compact-small", |
|
85
|
"mb-docs_1 flex-wrap" |
|
86
|
)} |
|
87
|
> |
|
88
|
{breadcrumbItems.map(({ title, link }, index) => ( |
|
89
|
<React.Fragment key={link}> |
|
90
|
{index > 0 && <TriangleRightMini />} |
|
91
|
<Button |
|
92
|
variant="transparent-clear" |
|
93
|
className={clsx( |
|
94
|
"px-docs_0.5 py-docs_0.25", |
|
95
|
link === "#" && "hover:cursor-default", |
|
96
|
"!p-0 hover:!bg-transparent hover:!text-medusa-fg-subtle" |
|
97
|
)} |
|
98
|
> |
|
99
|
<Link |
|
100
|
href={link} |
|
101
|
className={clsx(link === "#" && "hover:cursor-default")} |
|
102
|
> |
|
103
|
{title} |
|
104
|
</Link> |
|
105
|
</Button> |
|
106
|
</React.Fragment> |
|
107
|
))} |
|
108
|
<script |
|
109
|
type="application/ld+json" |
|
110
|
dangerouslySetInnerHTML={{ |
|
111
|
__html: jsonLd, |
|
112
|
}} |
|
113
|
/> |
|
114
|
</div> |
|
115
|
) |
|
116
|
} |