Skip to content

Commit 0dfacf2

Browse files
committed
Fix types, add category deep-links
1 parent 2e00925 commit 0dfacf2

File tree

3 files changed

+25
-47
lines changed

3 files changed

+25
-47
lines changed

src/components/ProductCatalog.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { useState, type ChangeEvent } from "react";
1+
import { useEffect, useState, type ChangeEvent } from "react";
22
import type { CollectionEntry } from "astro:content";
33
import 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({

src/components/ThreeCardGrid.astro

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/pages/products.astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import iconCollection from "virtual:astro-icon";
55
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
66
import { getIconData, iconToSVG } from "@iconify/utils";
77
import ProductCatalog from "~/components/ProductCatalog";
8+
import type { ProductData } from "~/components/ProductCatalog";
89
910
let 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>

0 commit comments

Comments
 (0)