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:
1 parent
9e157a4fa3
commit
fb8e42f632
11 files changed
+811
-266
No files matched your search
@@ -3,26 +3,26 @@ import { getCollection } from "astro:content"
|
||||
import PageLayout from "@layouts/PageLayout.astro"
|
||||
import TopLayout from "@layouts/TopLayout.astro"
|
||||
import BottomLayout from "@layouts/BottomLayout.astro"
|
||||
import Projects from "@components/Projects"
|
||||
import FilteredList from "@components/FilteredList"
|
||||
import { PROJECTS } from "@consts"
|
||||
import { byDateDesc } from "@lib/content"
|
||||
import { parseFilterParams } from "@lib/filters"
|
||||
|
||||
const entries = await getCollection("projects", ({ data }) => !data.draft)
|
||||
const posts = entries
|
||||
// Only the fields the cards and the filters read; see src/pages/blog/index.astro.
|
||||
const projects = 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 ?? "",
|
||||
demoUrl: entry.data.demoUrl,
|
||||
repoUrl: entry.data.repoUrl,
|
||||
}))
|
||||
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={PROJECTS.TITLE} description={PROJECTS.DESCRIPTION}>
|
||||
@@ -33,7 +33,7 @@ const tags = Array.from(new Set(posts.flatMap((p) => p.tags)))
|
||||
</TopLayout>
|
||||
<BottomLayout>
|
||||
<div class="animate">
|
||||
<Projects client:load tags={tags} data={posts} />
|
||||
<FilteredList client:load collection="projects" entries={projects} initial={initial} />
|
||||
</div>
|
||||
</BottomLayout>
|
||||
</PageLayout>
|
||||
Reference in new issue
Block a user