-
Notifications
You must be signed in to change notification settings - Fork 10k
[Docs Site] Refactor ResourcesBySelector #23423
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9835ac
[Docs Site] Refactor ResourcesBySelector
KianNH 071e616
move to React component
KianNH 06338c4
read filter from url on load
KianNH f5e3ef3
update props docs
KianNH be8a466
rename prop
KianNH 6f0a594
Apply suggestions from code review
kodster28 81d8bbf
add meta descriptions
kodster28 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,78 @@ | ||
| import Select, { type Props } from "react-select"; | ||
| import type { ActionMeta, StylesConfig } from "react-select"; | ||
| import { setSearchParams } from "~/util/url"; | ||
|
|
||
| export type Option = { | ||
| label: string; | ||
| value: string; | ||
| }; | ||
|
|
||
| export default function ReactSelect(props: Props & { urlParam?: string }) { | ||
| const selectStyles: StylesConfig = { | ||
| control: (base, state) => ({ | ||
| ...base, | ||
| backgroundColor: "var(--sl-color-gray-6)", | ||
| borderColor: state.isFocused | ||
| ? "var(--sl-color-gray-3)" | ||
| : "var(--sl-color-gray-4)", | ||
| "&:hover": { | ||
| borderColor: "var(--sl-color-gray-3)", | ||
| }, | ||
| boxShadow: state.isFocused ? "0 0 0 1px var(--sl-color-gray-3)" : "none", | ||
| }), | ||
| menu: (base) => ({ | ||
| ...base, | ||
| backgroundColor: "var(--sl-color-gray-6)", | ||
| borderColor: "var(--sl-color-gray-4)", | ||
| }), | ||
| option: (base, state) => ({ | ||
| ...base, | ||
| backgroundColor: state.isFocused | ||
| ? "var(--sl-color-gray-5)" | ||
| : "var(--sl-color-gray-6)", | ||
| color: "var(--sl-color-gray-1)", | ||
| "&:active": { | ||
| backgroundColor: "var(--sl-color-gray-4)", | ||
| }, | ||
| }), | ||
| singleValue: (base) => ({ | ||
| ...base, | ||
| color: "var(--sl-color-gray-1)", | ||
| }), | ||
| input: (base) => ({ | ||
| ...base, | ||
| color: "var(--sl-color-gray-1)", | ||
| }), | ||
| groupHeading: (base) => ({ | ||
| ...base, | ||
| color: "var(--sl-color-gray-3)", | ||
| }), | ||
| }; | ||
|
|
||
| const onChangeHandler = ( | ||
| option: Option | null, | ||
| actionMeta: ActionMeta<Option>, | ||
| ) => { | ||
| props.onChange?.(option, actionMeta); | ||
|
|
||
| const params = new URLSearchParams(window.location.search); | ||
|
|
||
| if (option) { | ||
| params.set(props.urlParam || "filters", option.value); | ||
| } else { | ||
| params.delete(props.urlParam || "filters"); | ||
| } | ||
|
|
||
| setSearchParams(params); | ||
| }; | ||
|
|
||
| return ( | ||
| <Select | ||
| {...props} | ||
| styles={selectStyles} | ||
| onChange={(val: unknown, meta: ActionMeta<unknown>) => | ||
| onChangeHandler(val as Option | null, meta as ActionMeta<Option>) | ||
| } | ||
| /> | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -1,36 +1,85 @@ | ||
| --- | ||
| import { z } from "astro:schema"; | ||
| import { getCollection } from "astro:content"; | ||
| import { getCollection, type CollectionEntry } from "astro:content"; | ||
| import ReactSelect from "./ReactSelect"; | ||
|
|
||
| type Props = z.infer<typeof props>; | ||
| type Frontmatter = keyof CollectionEntry<"docs">["data"]; | ||
|
|
||
| const props = z.object({ | ||
| tags: z.string().array().optional(), | ||
| types: z.string().array(), | ||
| products: z.string().array().optional(), | ||
| directory: z.string().optional(), | ||
| filters: z.custom<Frontmatter>().array().optional(), | ||
| }); | ||
|
|
||
| const { tags, types, products } = props.parse(Astro.props); | ||
| const { tags, types, products, directory, filters } = props.parse(Astro.props); | ||
|
|
||
| const resources = await getCollection("docs", ({ data }) => { | ||
| const resources = await getCollection("docs", ({ id, data }) => { | ||
| return ( | ||
| types.includes(data.pcx_content_type ?? "") && | ||
| (directory ? id.startsWith(directory) : true) && | ||
| (tags ? data.tags?.some((v: string) => tags.includes(v)) : true) && | ||
| (products ? data.products?.some((v: string) => products.includes(v)) : true) | ||
| ); | ||
| }); | ||
|
|
||
| const facets = resources.reduce( | ||
| (acc, page) => { | ||
| if (!filters) return acc; | ||
|
|
||
| for (const filter of filters) { | ||
| const val = page.data[filter]; | ||
| if (val) { | ||
| if (Array.isArray(val) && val.every((v) => typeof v === "string")) { | ||
| acc[filter] = [...new Set([...(acc[filter] || []), ...val])]; | ||
| } else if (typeof val === "string") { | ||
| acc[filter] = [...new Set([...(acc[filter] || []), val])]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return acc; | ||
| }, | ||
| {} as Record<string, string[]>, | ||
| ); | ||
| --- | ||
|
|
||
| <ul> | ||
| { | ||
| filters && ( | ||
| <div class="not-content"> | ||
| <ReactSelect | ||
| id="resources-filters" | ||
| className="mt-2" | ||
| options={Object.entries(facets).map(([key, values]) => ({ | ||
| label: key, | ||
| options: values.map((v) => ({ | ||
| value: v, | ||
| label: v, | ||
| })), | ||
| }))} | ||
| onChange={(opt) => console.log(opt)} | ||
| client:idle | ||
| /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| <div class="grid grid-cols-2 gap-4"> | ||
| { | ||
| resources.map((page) => { | ||
| const description = page.data.description; | ||
| return ( | ||
| <li> | ||
| <!-- prettier-ignore --> | ||
| <a href={`/${page.id}/`}>{page.data.title}</a>{description && `: ${description}`} | ||
| </li> | ||
| ); | ||
| }) | ||
| resources.map((page) => ( | ||
| <a | ||
| href={`/${page.id}/`} | ||
| class="flex flex-col gap-2 rounded-sm border border-solid border-gray-200 p-6 text-black no-underline hover:bg-gray-50 dark:border-gray-700 dark:hover:bg-gray-800" | ||
| > | ||
| <p class="decoration-accent underline decoration-2 underline-offset-4"> | ||
| {page.data.title} | ||
| </p> | ||
| <span class="line-clamp-3" title={page.data.description}> | ||
| {page.data.description} | ||
| </span> | ||
| </a> | ||
| )) | ||
| } | ||
| </ul> | ||
| </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
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.
Uh oh!
There was an error while loading. Please reload this page.