-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Docs Site] Create TutorialsGrid component #20557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d033a47
[Docs Site] Create TutorialsGrid component
KianNH 4ea5598
type
KianNH 6ce5b7a
Update src/components/TutorialsGrid.astro
KianNH a70472e
youtube and community badges
KianNH 8f3a98b
move usages
KianNH 2dc67ce
make difficulty optional
KianNH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| --- | ||
| import { z } from "astro:schema"; | ||
| import { | ||
| getCollection, | ||
| getEntry, | ||
| reference, | ||
| type CollectionEntry, | ||
| } from "astro:content"; | ||
| import type { ComponentProps } from "astro/types"; | ||
| import { Badge } from "@astrojs/starlight/components"; | ||
|
|
||
| type Props = z.infer<typeof props>; | ||
| type BadgeType = ComponentProps<typeof Badge>; | ||
|
|
||
| const props = z.object({ | ||
| product: reference("products").optional(), | ||
| frontmatterBadges: z.string().array().optional(), | ||
| }); | ||
|
|
||
| let { product, frontmatterBadges } = props.parse(Astro.props); | ||
|
|
||
| type DocsEntry = CollectionEntry<"docs">; | ||
| type VideoEntry = CollectionEntry<"videos">; | ||
|
|
||
| const tutorials: Array<DocsEntry | VideoEntry> = []; | ||
|
|
||
| if (!product) { | ||
| product = { | ||
| id: Astro.url.pathname.split("/").filter(Boolean)[0], | ||
| collection: "products", | ||
| }; | ||
| } | ||
|
|
||
| const productEntry = await getEntry(product); | ||
|
|
||
| if (!productEntry) { | ||
| throw new Error( | ||
| `[TutorialsGrid] Unable to find product YAML for ${product.id}.`, | ||
| ); | ||
| } | ||
|
|
||
| const productTitle = productEntry.data.product.title; | ||
|
|
||
| const docs = await getCollection("docs", (entry) => { | ||
| return ( | ||
| // pcx_content_type: tutorial | ||
| entry.data.pcx_content_type === "tutorial" && | ||
| // /src/content/r2/**/*.mdx | ||
| (entry.id.startsWith(`${productEntry.id}/`) || | ||
| // products: [R2] | ||
| entry.data.products?.some( | ||
| (v: string) => v.toUpperCase() === productTitle.toUpperCase(), | ||
| )) | ||
| ); | ||
| }); | ||
|
|
||
| tutorials.push(...docs); | ||
|
|
||
| const videos = await getCollection("videos", (entry) => { | ||
| return entry.data.products.some( | ||
| (v: string) => v.toUpperCase() === productTitle.toUpperCase(), | ||
| ); | ||
| }); | ||
|
|
||
| tutorials.push(...videos); | ||
|
|
||
| tutorials.sort((a, b) => Number(b.data.updated) - Number(a.data.updated)); | ||
|
|
||
| const difficulties = { | ||
| Beginner: "success", | ||
| Intermediate: "caution", | ||
| Advanced: "danger", | ||
| } as Record<string, ComponentProps<typeof Badge>["variant"]>; | ||
| --- | ||
|
|
||
| <div class="grid grid-cols-2 gap-4"> | ||
| { | ||
| tutorials.map((tutorial) => { | ||
| const title = | ||
| tutorial.collection === "docs" ? tutorial.data.title : tutorial.id; | ||
|
|
||
| const href = | ||
| tutorial.collection === "docs" | ||
| ? `/${tutorial.id}/` | ||
| : tutorial.data.link; | ||
|
|
||
| const difficulty = tutorial.data.difficulty; | ||
|
|
||
| const YOUTUBE_DOMAINS = ["www.youtube.com", "youtube.com", "youtu.be"]; | ||
| const youtube = | ||
| tutorial.collection === "videos" && | ||
| YOUTUBE_DOMAINS.includes(new URL(tutorial.data.link).hostname); | ||
|
|
||
| const spotlight = | ||
| tutorial.collection === "docs" && tutorial.data.spotlight; | ||
|
|
||
| const badges: BadgeType[] = []; | ||
|
|
||
| if (difficulty) { | ||
| badges.push({ | ||
| text: difficulty, | ||
| variant: difficulties[difficulty], | ||
| }); | ||
| } | ||
|
|
||
| if (youtube) { | ||
| badges.push({ | ||
| text: "YouTube", | ||
| variant: "note", | ||
| }); | ||
| } | ||
|
|
||
| if (spotlight) { | ||
| badges.push({ | ||
| text: "Community", | ||
| variant: "tip", | ||
| }); | ||
| } | ||
|
|
||
| if (frontmatterBadges) { | ||
| for (const frontmatterBadge of frontmatterBadges) { | ||
| const values = | ||
| tutorial.data[frontmatterBadge as keyof typeof tutorial.data]; | ||
|
|
||
| if (values) { | ||
| if (Array.isArray(values)) { | ||
| for (const value of values) { | ||
| badges.push({ text: value.toString() }); | ||
| } | ||
| } else { | ||
| badges.push({ text: values.toString() }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <a | ||
| href={href} | ||
| 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" | ||
| > | ||
| <strong>{title}</strong> | ||
| <p class="!mt-auto"> | ||
| {badges.map((badge) => ( | ||
| <Badge {...badge} /> | ||
| ))} | ||
| </p> | ||
| </a> | ||
| ); | ||
| }) | ||
| } | ||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/content/docs/style-guide/components/tutorials-grid.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --- | ||
| title: Tutorials grid | ||
| --- | ||
|
|
||
| This component fetches all pages with `pcx_content_type: tutorial` under a given product. | ||
|
|
||
| ## Import | ||
|
|
||
| ```mdx | ||
| import { TutorialsGrid } from "~/components"; | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```mdx live | ||
| import { TutorialsGrid } from "~/components"; | ||
|
|
||
| <TutorialsGrid product="workers" frontmatterBadges={["languages"]} /> | ||
| ``` | ||
|
|
||
| ## `<TutorialsGrid>` Props | ||
|
|
||
| ### `product` | ||
|
|
||
| **type:** `string` | ||
|
|
||
| The product to fetch tutorials for, based on the file location and the `products` frontmatter property. | ||
|
|
||
| This property is optional, and will be inferred from the page it is used on if omitted. | ||
|
|
||
| ### `frontmatterBadges` | ||
|
|
||
| **type:** `string[]` | ||
|
|
||
| Frontmatter values to add badges from. | ||
|
|
||
| For example, given the below frontmatter, the badges `foo` and `bar` would be added when `frontmatterBadges={["stuff"]}` is provided. | ||
|
|
||
| ```mdx | ||
| --- | ||
| title: Example Title | ||
| stuff: | ||
| - foo | ||
| - bar | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feedback on the look / design based on previous things I've heard and experienced.