Skip to content

Commit c7c2a4f

Browse files
committed
Add badges to functions list
1 parent 581ef2b commit c7c2a4f

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

web/src/components/CategoryList.astro

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { getFunctionInfo } from '@src/utils/functions';
33
import { getEventTypePretty } from '@src/utils/events';
44
import { Icon } from '@astrojs/starlight/components';
5+
import { badgeConfig } from "@src/utils/badges";
6+
import { Badge } from "@astrojs/starlight/components";
57
68
interface Props {
79
itemsByCategory: any;
@@ -134,14 +136,50 @@ function displayCategory(category: string) {
134136
<h4>{displayTitle(category)}
135137
<a href={`#${slugify(category)}`}><Icon size="1.5rem" name="external" class="mtainfoicon"/></a>
136138
</h4>
139+
137140
<ul>
138141
{items
139142
.slice()
140143
.sort((a, b) => a.id.localeCompare(b.id))
141-
.map(item => (
142-
<li set:html={displayItem(item)}></li>
143-
))}
144+
.map((item) => {
145+
const isFunction = item.collection === "functions";
146+
const funcInfo = isFunction ? getFunctionInfo(item.data) : null;
147+
148+
const badgeType = funcInfo?.version?.added ? "new" : funcInfo?.version?.updated ? "updated" : null;
149+
const badge = badgeType ? badgeConfig[badgeType] : null;
150+
151+
return (
152+
<li>
153+
<a href={`/reference/${item.id}`}>{item.id}</a>
154+
155+
{what === "all_functions" && funcInfo && (
156+
<>
157+
(<span class={`side-${funcInfo.type}`}>{funcInfo.typePretty}</span>)
158+
</>
159+
)}
160+
161+
{what === "all_events" && (
162+
<>
163+
{" "}
164+
(<span class={`side-${item.data.type}`}>
165+
{getEventTypePretty(item.data.type)}-side
166+
</span>)
167+
</>
168+
)}
169+
170+
{badge && (
171+
<Badge
172+
text={badge.text}
173+
variant={badge.variant}
174+
size="medium"
175+
style="font-weight: bold;"
176+
/>
177+
)}
178+
</li>
179+
);
180+
})}
144181
</ul>
182+
145183
</section>
146184
))}
147185
</section>

web/src/components/SeeAlsoSection.astro

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import type { SeeAlsoLinkGroup } from "@src/utils/general";
3+
import { getBadgeConfig } from "@src/utils/badges";
34
import { Badge } from "@astrojs/starlight/components";
45
56
export interface Props {
@@ -8,16 +9,6 @@ export interface Props {
89
}
910
1011
const { seeAlsoLinks, currentId } = Astro.props;
11-
12-
const badgeConfig: {
13-
[key: string]: {
14-
text: string;
15-
variant: "note" | "danger" | "success" | "caution" | "tip" | "default";
16-
};
17-
} = {
18-
new: { text: "New", variant: "success" },
19-
updated: { text: "Updated", variant: "caution" },
20-
};
2112
---
2213

2314
{
@@ -44,10 +35,7 @@ const badgeConfig: {
4435

4536
{link.badge &&
4637
(() => {
47-
const config = badgeConfig[link.badge] ?? {
48-
text: link.badge,
49-
variant: "default",
50-
};
38+
const config = getBadgeConfig(link.badge) || {text: link.badge, variant: "default"};
5139

5240
return (
5341
<Badge

web/src/utils/badges.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export type BadgeVariant =
2+
| "note"
3+
| "danger"
4+
| "success"
5+
| "caution"
6+
| "tip"
7+
| "default";
8+
9+
export type BadgeType = "new" | "updated";
10+
11+
export const badgeConfig: Record<
12+
BadgeType,
13+
{
14+
text: string;
15+
variant: BadgeVariant;
16+
}
17+
> = {
18+
new: {
19+
text: "New",
20+
variant: "success",
21+
},
22+
updated: {
23+
text: "Updated",
24+
variant: "caution",
25+
},
26+
};
27+
28+
export function getBadgeConfig(type?: string) {
29+
if (!type) return null;
30+
return badgeConfig[type as BadgeType] ?? null;
31+
}

0 commit comments

Comments
 (0)