Skip to content

Commit 36bb219

Browse files
aka-sacci-ccrdecobotcursoragent
authored
feat(blog): expose active category on listings (#1620)
* feat(blog): expose active category on listings Co-authored-by: Cursor <cursoragent@cursor.com> * Fix getRecordsByPath issue --------- Co-authored-by: decobot <capy@deco.cx> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a55584e commit 36bb219

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

blog/loaders/BlogpostListing.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { logger } from "@deco/deco/o11y";
1010
import { PageInfo } from "../../commerce/types.ts";
1111
import { RequestURLParam } from "../../website/functions/requestToParam.ts";
1212
import { AppContext } from "../mod.ts";
13-
import { BlogPost, BlogPostListingPage, SortBy } from "../types.ts";
13+
import { BlogPost, BlogPostListingPage, Category, SortBy } from "../types.ts";
1414
import handlePosts, { slicePosts } from "../core/handlePosts.ts";
1515
import { getRecordsByPath } from "../core/records.ts";
1616

1717
const COLLECTION_PATH = "collections/blog/posts";
1818
const ACCESSOR = "post";
19+
const CATEGORIES_PATH = "collections/blog/categories";
20+
const CATEGORY_ACCESSOR = "category";
1921

2022
export interface Props {
2123
/**
@@ -92,12 +94,29 @@ export default async function BlogPostList(
9294
return null;
9395
}
9496

95-
const category = slicedPosts[0].categories?.find((c) => c.slug === slug);
97+
let category: Category | null = null;
98+
if (slug) {
99+
try {
100+
const categories = await getRecordsByPath<Category>(
101+
ctx,
102+
CATEGORIES_PATH,
103+
CATEGORY_ACCESSOR,
104+
);
105+
category = categories?.find((c) => c.slug === slug) ?? null;
106+
} catch (e) {
107+
logger.error(e);
108+
}
109+
category ??= slicedPosts[0].categories?.find((c) => c.slug === slug) ??
110+
null;
111+
}
112+
96113
return {
97114
posts: slicedPosts,
115+
category,
98116
pageInfo: toPageInfo(handledPosts, postsPerPage, pageNumber, params),
99117
seo: {
100118
title: category?.name ?? "",
119+
description: category?.description,
101120
canonical: new URL(url.pathname, url.origin).href,
102121
},
103122
};

blog/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ export type SortBy =
120120

121121
export interface BlogPostListingPage {
122122
posts: BlogPost[];
123+
/** @title Active category */
124+
category?: Category | null;
123125
pageInfo: PageInfo;
124126
seo: Seo;
125127
}

0 commit comments

Comments
 (0)