refactor: consolidate blog and project filtering into shared component

The /blog and /projects listings previously used byte-identical copies of the same
filtering island, which meant bugs like inconsistent AND-vs-OR tag semantics,
duplicate-case tags, and a missing empty state were duplicated across both.

This commit introduces `FilteredList`, a single shared Solid component that:

- Unifies tag, keyword, and sort filtering for both collections
- Seeds initial filter state from the SSR request URL to avoid hydration mismatches
- Syncs active filters into the address bar via `history.replaceState` so filtered
  views survive refreshes and can be shared
- Restores filters on browser back/forward navigation (`popstate`)
- Case-folds and dedupes tags when parsing URL params

Filter logic (collectTags, filterEntries, sortKeys, URL param encoding) is extracted
into `src/lib/filters.ts` with full unit test coverage.
This commit is contained in:
spaghet committed 2026-11-12 22:38:58 -05:00
1 parent 9e157a4fa3
commit fb8e42f632
11 files changed
+811 -266

No files matched your search

+10 -8
View File
@@ -3,26 +3,28 @@ import { getCollection } from "astro:content"
import PageLayout from "@layouts/PageLayout.astro"
import TopLayout from "@layouts/TopLayout.astro"
import BottomLayout from "@layouts/BottomLayout.astro"
import Blog from "@components/Blog"
import FilteredList from "@components/FilteredList"
import { BLOG } from "@consts"
import { byDateDesc } from "@lib/content"
import { parseFilterParams } from "@lib/filters"
const entries = await getCollection("blog", ({ data }) => !data.draft)
// Only the fields the cards and the filters read. `content` was serialized into
// the island's props on every request and never matched against — the markdown
// body is the single largest thing on this page.
const posts = entries
.sort(byDateDesc)
.map((entry) => ({
id: entry.id,
slug: entry.id,
title: entry.data.title,
summary: entry.data.summary,
date: entry.data.date.toISOString(),
tags: entry.data.tags,
draft: entry.data.draft,
content: entry.body ?? "",
template: entry.data.template,
featured: entry.data.featured,
}))
const tags = Array.from(new Set(posts.flatMap((p) => p.tags)))
// Frontmatter runs on the server, so the initial filter state has to come from
// the request URL rather than `window.location` (see src/pages/search/index.astro).
const initial = parseFilterParams(Astro.url.searchParams)
---
<PageLayout title={BLOG.TITLE} description={BLOG.DESCRIPTION}>
@@ -33,7 +35,7 @@ const tags = Array.from(new Set(posts.flatMap((p) => p.tags)))
</TopLayout>
<BottomLayout>
<div class="animate">
<Blog client:load tags={tags} data={posts} />
<FilteredList client:load collection="blog" entries={posts} initial={initial} />
</div>
</BottomLayout>
</PageLayout>