refactor(content): separate blog and agenda feeds
This commit is contained in:
+65
-4
@@ -26,7 +26,15 @@ export type BlogPageData = {
|
|||||||
|
|
||||||
const DEFAULT_IMAGE = "/news/design.svg";
|
const DEFAULT_IMAGE = "/news/design.svg";
|
||||||
const DEFAULT_READ_TIME = "5 min";
|
const DEFAULT_READ_TIME = "5 min";
|
||||||
const BLOG_CATEGORIES = ["All", "Newsletter", "Frontend", "UX", "Software", "TypeScript", "Testing", "General"];
|
const NEWSLETTER_CATEGORY = "Newsletter";
|
||||||
|
const BLOG_CATEGORY_ORDER = [
|
||||||
|
"Frontend",
|
||||||
|
"UX",
|
||||||
|
"Software",
|
||||||
|
"TypeScript",
|
||||||
|
"Testing",
|
||||||
|
"General",
|
||||||
|
];
|
||||||
|
|
||||||
function toTimestamp(value: string) {
|
function toTimestamp(value: string) {
|
||||||
const timestamp = Date.parse(value);
|
const timestamp = Date.parse(value);
|
||||||
@@ -54,7 +62,11 @@ function normalizeCategory(value: string) {
|
|||||||
return value.trim().toLocaleLowerCase();
|
return value.trim().toLocaleLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getAllBlogArticles(locale?: string): Promise<BlogArticleItem[]> {
|
export function isNewsletterCategory(category: string) {
|
||||||
|
return normalizeCategory(category) === normalizeCategory(NEWSLETTER_CATEGORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAllArticles(locale?: string): Promise<BlogArticleItem[]> {
|
||||||
const posts = await listBlogDetails(locale);
|
const posts = await listBlogDetails(locale);
|
||||||
|
|
||||||
const articles = posts.map((post) => ({
|
const articles = posts.map((post) => ({
|
||||||
@@ -66,13 +78,25 @@ export async function getAllBlogArticles(locale?: string): Promise<BlogArticleIt
|
|||||||
image: post.coverImage || DEFAULT_IMAGE,
|
image: post.coverImage || DEFAULT_IMAGE,
|
||||||
date: post.date,
|
date: post.date,
|
||||||
readTime: post.readTime || DEFAULT_READ_TIME,
|
readTime: post.readTime || DEFAULT_READ_TIME,
|
||||||
href: `/blog/${post.slug}`,
|
href: isNewsletterCategory(post.category)
|
||||||
|
? `/agenda/${post.slug}`
|
||||||
|
: `/blog/${post.slug}`,
|
||||||
author: post.author || "Poyraz Avsever",
|
author: post.author || "Poyraz Avsever",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return sortByDateDesc(articles);
|
return sortByDateDesc(articles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAllBlogArticles(locale?: string): Promise<BlogArticleItem[]> {
|
||||||
|
const articles = await getAllArticles(locale);
|
||||||
|
return articles.filter((article) => !isNewsletterCategory(article.category));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAllAgendaArticles(locale?: string): Promise<BlogArticleItem[]> {
|
||||||
|
const articles = await getAllArticles(locale);
|
||||||
|
return articles.filter((article) => isNewsletterCategory(article.category));
|
||||||
|
}
|
||||||
|
|
||||||
export async function getHomeBlogNews(locale?: string, limit = 3) {
|
export async function getHomeBlogNews(locale?: string, limit = 3) {
|
||||||
const articles = await getAllBlogArticles(locale);
|
const articles = await getAllBlogArticles(locale);
|
||||||
|
|
||||||
@@ -94,7 +118,14 @@ export async function getBlogPageData(
|
|||||||
searchQueryParam?: string,
|
searchQueryParam?: string,
|
||||||
): Promise<BlogPageData> {
|
): Promise<BlogPageData> {
|
||||||
const articles = await getAllBlogArticles(locale);
|
const articles = await getAllBlogArticles(locale);
|
||||||
const categories = BLOG_CATEGORIES;
|
const availableCategories = new Set(articles.map((article) => article.category));
|
||||||
|
const categories = [
|
||||||
|
"All",
|
||||||
|
...BLOG_CATEGORY_ORDER.filter((category) => availableCategories.has(category)),
|
||||||
|
...[...availableCategories]
|
||||||
|
.filter((category) => !BLOG_CATEGORY_ORDER.includes(category))
|
||||||
|
.sort((a, b) => a.localeCompare(b)),
|
||||||
|
];
|
||||||
const categoryByNormalized = new Map(
|
const categoryByNormalized = new Map(
|
||||||
categories.map((category) => [normalizeCategory(category), category]),
|
categories.map((category) => [normalizeCategory(category), category]),
|
||||||
);
|
);
|
||||||
@@ -131,3 +162,33 @@ export async function getBlogPageData(
|
|||||||
currentPage,
|
currentPage,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAgendaPageData(
|
||||||
|
locale?: string,
|
||||||
|
page = 1,
|
||||||
|
pageSize = 12,
|
||||||
|
searchQueryParam?: string,
|
||||||
|
): Promise<BlogPageData> {
|
||||||
|
const articles = await getAllAgendaArticles(locale);
|
||||||
|
const searchQuery = (searchQueryParam ?? "").trim();
|
||||||
|
const searchLower = searchQuery.toLocaleLowerCase();
|
||||||
|
const filtered = searchLower
|
||||||
|
? articles.filter(
|
||||||
|
(item) =>
|
||||||
|
item.title.toLocaleLowerCase().includes(searchLower) ||
|
||||||
|
item.excerpt.toLocaleLowerCase().includes(searchLower),
|
||||||
|
)
|
||||||
|
: articles;
|
||||||
|
const totalPages = Math.max(1, Math.ceil(Math.max(filtered.length, 1) / pageSize));
|
||||||
|
const currentPage = Math.min(Math.max(1, page), totalPages);
|
||||||
|
const start = (currentPage - 1) * pageSize;
|
||||||
|
|
||||||
|
return {
|
||||||
|
articles: filtered.slice(start, start + pageSize),
|
||||||
|
categories: ["All"],
|
||||||
|
selectedCategory: "All",
|
||||||
|
searchQuery,
|
||||||
|
totalPages,
|
||||||
|
currentPage,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user