-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBreadcrumbs.tsx
More file actions
212 lines (200 loc) · 5.98 KB
/
Breadcrumbs.tsx
File metadata and controls
212 lines (200 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"use client";
import { track } from "@vercel/analytics";
import { ChevronsUpDown, Home, Slash } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import React, { type ReactNode } from "react";
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
ComboboxTrigger,
} from "@/components/ui/combobox";
import {
Item,
ItemContent,
ItemDescription,
ItemTitle,
} from "@/components/ui/item";
import { cn } from "@/lib/utils";
import type { Entity } from "@/types";
import { EntityBadge } from "./Badges";
import { Button } from "./ui/button";
export default function Breadcrumbs(props: { entity: Entity }) {
const pages = [
props.entity?.parent?.parent,
props.entity?.parent,
props.entity,
].filter((p) => !!p);
return (
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbLink href="/">
<Home />
</BreadcrumbLink>
<Separator />
{pages.map((page, index) => (
<React.Fragment key={page.id || index}>
{index > 0 && <Separator />}
<SiblingSelect parent={page.parent} selected={page}>
<BreadcrumbContent active={props.entity} entity={page} />
</SiblingSelect>
</React.Fragment>
))}
{props.entity && props.entity.children.length > 0 && (
<>
<Separator />
<SiblingSelect parent={props.entity} className="opacity-70">
Select{" "}
{props.entity.subtype === "consortium"
? "organization"
: "repository"}
...
</SiblingSelect>
</>
)}
</BreadcrumbList>
</Breadcrumb>
);
}
function Separator() {
return (
<BreadcrumbSeparator>
<Slash opacity={0.25} />
</BreadcrumbSeparator>
);
}
function BreadcrumbContent(props: {
active: { id: string };
entity: { id: string; name: string; subtype: string };
}) {
const className = `flex flex-row items-center ${props.entity.id === props.active.id ? "bg-black/0 font-semibold" : ""}`;
function onClick(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) {
track("breadcrumbs", { on: "breadcrumb entity", action: "clicked" });
e.stopPropagation();
}
const BreadcrumbPageLink = (wrapperProps: { children: ReactNode }) =>
props.entity.id === props.active.id ? (
<BreadcrumbPage {...wrapperProps} className={className} />
) : (
<BreadcrumbLink
{...wrapperProps}
href={`/${props.entity.id}`}
className={className}
onMouseDown={onClick}
/>
);
return (
<BreadcrumbItem>
<BreadcrumbPageLink>
<Item className="p-0 pt-5">
<ItemContent className="gap-0">
<ItemTitle
className={
props.entity.id === props.active.id
? "bg-black/0 font-semibold"
: ""
}
>
{props.entity.name} <EntityBadge entity={props.entity} />
</ItemTitle>
<ItemDescription className="text-muted-foreground/75 text-start">
{props.entity.id}
</ItemDescription>
</ItemContent>
</Item>
</BreadcrumbPageLink>
</BreadcrumbItem>
);
}
function SiblingSelect(props: {
parent: Entity | null;
selected?: Entity;
children?: ReactNode;
className?: string;
}) {
const items = props.parent?.children || [];
const searchParams = useSearchParams();
if (items.length === 0) return props.children;
return (
<Combobox
items={items}
itemToStringValue={(item) => item.id}
itemToStringLabel={(item) => item.name}
value={props.selected}
isItemEqualToValue={(item, value) => item.id === value.id}
filter={(itemValue, query) =>
itemValue.id.toLowerCase().indexOf(query.toLowerCase()) !== -1 ||
itemValue.name.toLowerCase().indexOf(query.toLowerCase()) !== -1
}
disabled={items.length === 0}
>
<ComboboxTrigger
render={
<Button variant="ghost" className={cn("h-min py-0", props.className)}>
{props.children}
{items.length > 0 && <ChevronsUpDown />}
</Button>
}
/>
<ComboboxContent className="w-125">
<ComboboxInput
placeholder={`Search ${props.parent?.name}`}
showTrigger={false}
/>
<ComboboxEmpty>
No{" "}
{props.parent?.type === "provider" ? "repositories" : "organizations"}{" "}
found.
</ComboboxEmpty>
<ComboboxList>
{(item: Entity) => {
return (
<ComboboxItem
onClick={() =>
track("breadcrumbs", {
on: "dropdown entity",
action: "clicked",
})
}
value={item}
key={item.id}
>
<Link
href={`/${item.id}?${searchParams.toString()}`}
prefetch
className="size-full"
>
<Item size="sm" className="px-0 py-0.5">
<ItemContent className="gap-0">
<ItemTitle>{item.name}</ItemTitle>
<ItemDescription className="text-muted-foreground/75">
{item.id}
</ItemDescription>
</ItemContent>
<ItemContent className="flex-none text-center">
<ItemDescription>
<EntityBadge entity={item} />
</ItemDescription>
</ItemContent>
</Item>
</Link>
</ComboboxItem>
);
}}
</ComboboxList>
</ComboboxContent>
</Combobox>
);
}