|
| 1 | +--- |
| 2 | +import { z } from "astro:schema"; |
| 3 | +import { |
| 4 | + getCollection, |
| 5 | + getEntry, |
| 6 | + reference, |
| 7 | + type CollectionEntry, |
| 8 | +} from "astro:content"; |
| 9 | +import type { ComponentProps } from "astro/types"; |
| 10 | +import { Badge } from "@astrojs/starlight/components"; |
| 11 | +
|
| 12 | +type Props = z.infer<typeof props>; |
| 13 | +type Badge = ComponentProps<typeof Badge>; |
| 14 | +
|
| 15 | +const props = z.object({ |
| 16 | + product: reference("products").optional(), |
| 17 | + frontmatterBadges: z.string().array().optional(), |
| 18 | +}); |
| 19 | +
|
| 20 | +let { product, frontmatterBadges } = props.parse(Astro.props); |
| 21 | +
|
| 22 | +type DocsEntry = CollectionEntry<"docs">; |
| 23 | +type VideoEntry = CollectionEntry<"videos">; |
| 24 | +
|
| 25 | +const tutorials: Array<DocsEntry | VideoEntry> = []; |
| 26 | +
|
| 27 | +if (!product) { |
| 28 | + product = { |
| 29 | + id: Astro.url.pathname.split("/").filter(Boolean)[0], |
| 30 | + collection: "products", |
| 31 | + }; |
| 32 | +} |
| 33 | +
|
| 34 | +const productEntry = await getEntry(product); |
| 35 | +
|
| 36 | +if (!productEntry) { |
| 37 | + throw new Error( |
| 38 | + `[ListTutorials] Unable to find product YAML for ${product.id}.`, |
| 39 | + ); |
| 40 | +} |
| 41 | +
|
| 42 | +const productTitle = productEntry.data.product.title; |
| 43 | +
|
| 44 | +const docs = await getCollection("docs", (entry) => { |
| 45 | + return ( |
| 46 | + // pcx_content_type: tutorial |
| 47 | + entry.data.pcx_content_type === "tutorial" && |
| 48 | + // /src/content/r2/**/*.mdx |
| 49 | + (entry.id.startsWith(`${productEntry.id}/`) || |
| 50 | + // products: [R2] |
| 51 | + entry.data.products?.some( |
| 52 | + (v: string) => v.toUpperCase() === productTitle.toUpperCase(), |
| 53 | + )) |
| 54 | + ); |
| 55 | +}); |
| 56 | +
|
| 57 | +tutorials.push(...docs); |
| 58 | +
|
| 59 | +const videos = await getCollection("videos", (entry) => { |
| 60 | + return entry.data.products.some( |
| 61 | + (v: string) => v.toUpperCase() === productTitle.toUpperCase(), |
| 62 | + ); |
| 63 | +}); |
| 64 | +
|
| 65 | +tutorials.push(...videos); |
| 66 | +
|
| 67 | +tutorials.sort((a, b) => Number(b.data.updated) - Number(a.data.updated)); |
| 68 | +
|
| 69 | +const difficulties = { |
| 70 | + Beginner: "success", |
| 71 | + Intermediate: "caution", |
| 72 | + Advanced: "danger", |
| 73 | +} as Record<string, ComponentProps<typeof Badge>["variant"]>; |
| 74 | +--- |
| 75 | + |
| 76 | +<div class="grid grid-cols-2 gap-4"> |
| 77 | + { |
| 78 | + tutorials.map((tutorial) => { |
| 79 | + const title = |
| 80 | + tutorial.collection === "docs" ? tutorial.data.title : tutorial.id; |
| 81 | + |
| 82 | + const href = |
| 83 | + tutorial.collection === "docs" |
| 84 | + ? `/${tutorial.id}/` |
| 85 | + : tutorial.data.link; |
| 86 | + |
| 87 | + const difficulty = tutorial.data.difficulty; |
| 88 | + |
| 89 | + if (!difficulty) { |
| 90 | + throw new Error(`${tutorial.id} is missing difficulty property`); |
| 91 | + } |
| 92 | + |
| 93 | + const video = tutorial.collection === "videos"; |
| 94 | + |
| 95 | + const spotlight = |
| 96 | + tutorial.collection === "docs" && tutorial.data.spotlight; |
| 97 | + |
| 98 | + const badges: Badge[] = [ |
| 99 | + { |
| 100 | + text: difficulty, |
| 101 | + variant: difficulties[difficulty], |
| 102 | + }, |
| 103 | + ]; |
| 104 | + |
| 105 | + if (video) { |
| 106 | + badges.push({ |
| 107 | + text: "Video", |
| 108 | + variant: "note", |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + if (spotlight) { |
| 113 | + badges.push({ |
| 114 | + text: "Spotlight", |
| 115 | + variant: "tip", |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + if (frontmatterBadges) { |
| 120 | + for (const frontmatterBadge of frontmatterBadges) { |
| 121 | + const values = |
| 122 | + tutorial.data[frontmatterBadge as keyof typeof tutorial.data]; |
| 123 | + |
| 124 | + if (values) { |
| 125 | + if (Array.isArray(values)) { |
| 126 | + for (const value of values) { |
| 127 | + badges.push({ text: value.toString() }); |
| 128 | + } |
| 129 | + } else { |
| 130 | + badges.push({ text: values.toString() }); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return ( |
| 137 | + <a |
| 138 | + href={href} |
| 139 | + class="flex flex-col rounded border border-cl1-gray-8 p-6 !text-black no-underline hover:bg-cl1-gray-9 dark:border-cl1-gray-2 dark:bg-cl1-gray-0 dark:hover:bg-cl1-gray-1" |
| 140 | + > |
| 141 | + <strong>{title}</strong> |
| 142 | + <p class="!mt-auto"> |
| 143 | + {badges.map((badge) => ( |
| 144 | + <Badge {...badge} /> |
| 145 | + ))} |
| 146 | + </p> |
| 147 | + </a> |
| 148 | + ); |
| 149 | + }) |
| 150 | + } |
| 151 | +</div> |
0 commit comments