File tree Expand file tree Collapse file tree 3 files changed +25
-47
lines changed Expand file tree Collapse file tree 3 files changed +25
-47
lines changed Original file line number Diff line number Diff line change 1- import { useState , type ChangeEvent } from "react" ;
1+ import { useEffect , useState , type ChangeEvent } from "react" ;
22import type { CollectionEntry } from "astro:content" ;
33import type { IconifyIconBuildResult } from "@iconify/utils" ;
44
5- const ProductCatalog = ( {
6- products,
7- } : {
8- products : ( CollectionEntry < "products" > & {
9- icon : IconifyIconBuildResult ;
10- groups : string [ ] ;
11- } ) [ ] ;
12- } ) => {
5+ export type ProductData = CollectionEntry < "products" > & {
6+ icon ?: IconifyIconBuildResult ;
7+ groups : string [ ] ;
8+ } ;
9+
10+ const ProductCatalog = ( { products } : { products : ProductData [ ] } ) => {
1311 const [ filters , setFilters ] = useState < {
1412 search : string ;
1513 groups : string [ ] ;
@@ -41,6 +39,19 @@ const ProductCatalog = ({
4139 return true ;
4240 } ) ;
4341
42+ useEffect ( ( ) => {
43+ // On component load, check for deep-links to groups in the query param
44+ const params = new URLSearchParams ( window . location . search ) ;
45+ const groups = params . get ( "product-group" ) ;
46+
47+ if ( ! groups ) return ;
48+
49+ setFilters ( {
50+ ...filters ,
51+ groups : [ groups ] ,
52+ } ) ;
53+ } , [ ] ) ;
54+
4455 return (
4556 < div className = "md:flex" >
4657 < div className = "md:w-1/4 w-full mr-8" >
@@ -63,6 +74,7 @@ const ProductCatalog = ({
6374 type = "checkbox"
6475 className = "mr-2"
6576 value = { group }
77+ checked = { filters . groups . includes ( group ) }
6678 onChange = { ( e : ChangeEvent < HTMLInputElement > ) => {
6779 if ( e . target . checked ) {
6880 setFilters ( {
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import iconCollection from "virtual:astro-icon";
55import StarlightPage from " @astrojs/starlight/components/StarlightPage.astro" ;
66import { getIconData , iconToSVG } from " @iconify/utils" ;
77import ProductCatalog from " ~/components/ProductCatalog" ;
8+ import type { ProductData } from " ~/components/ProductCatalog" ;
89
910let products: CollectionEntry <" products" >[] = await getCollection (
1011 " products" ,
@@ -13,9 +14,9 @@ let products: CollectionEntry<"products">[] = await getCollection(
1314 },
1415);
1516
16- products = products .map ((product ) => {
17+ let productData : ProductData [] = products .map ((product ) => {
1718 const iconData = getIconData (iconCollection .local , product .id );
18- let icon = null ;
19+ let icon = undefined ;
1920 if (iconData ) {
2021 icon = iconToSVG (iconData );
2122 }
@@ -39,7 +40,7 @@ const frontmatter = {
3940---
4041
4142<StarlightPage frontmatter ={ frontmatter } >
42- <ProductCatalog products ={ products } client:load />
43+ <ProductCatalog products ={ productData } client:load />
4344</StarlightPage >
4445
4546<style >
You can’t perform that action at this time.
0 commit comments