Fix recent article count

This commit is contained in:
Carter McBride 2024-08-23 15:55:20 -06:00
parent 325e5459fb
commit 29540d285d

View file

@ -6,22 +6,32 @@ function isRecent(date: number): boolean {
const sixHours = 6 * 60 * 60 * 1000; const sixHours = 6 * 60 * 60 * 1000;
return now - date < sixHours; return now - date < sixHours;
} }
const STATIC_CATEGORIES = {
All: "All",
Recent: "Recent",
};
const yazzyUrl = import.meta.env.YAZZY_URL; const yazzyUrl = import.meta.env.YAZZY_URL;
const feedItems = await getAllFeedItems(); const feedItems = await getAllFeedItems();
const categories = [ const categories = [
"All", STATIC_CATEGORIES.All,
"Recent", STATIC_CATEGORIES.Recent,
...Array.from( ...Array.from(
new Set(feedItems.contents.map((item) => item.category)), new Set(feedItems.contents.map((item) => item.category)),
).sort(), ).sort(),
]; ];
const categoryCounts = categories.reduce((acc, category) => { const categoryCounts = categories.reduce((acc, category) => {
if (category === "All") { if (category === STATIC_CATEGORIES.All) {
acc[category] = feedItems.contents.length; acc[category] = feedItems.contents.length;
return acc; return acc;
} }
if (category === STATIC_CATEGORIES.Recent) {
acc[category] = feedItems.contents.filter((item) =>
isRecent(item.pubIsoDate),
).length;
return acc;
}
acc[category] = feedItems.contents.filter( acc[category] = feedItems.contents.filter(
(item) => item.category === category, (item) => item.category === category,
).length; ).length;
@ -91,7 +101,7 @@ const categoryColors = categories.reduce((acc, category) => {
}, {}); }, {});
const categoriesSelectorCss = categories const categoriesSelectorCss = categories
.filter((c) => c !== "All") .filter((c) => c !== STATIC_CATEGORIES.All)
.map( .map(
(c) => ` (c) => `
#category-picker:has(#${c}:checked) ~ main ul { #category-picker:has(#${c}:checked) ~ main ul {
@ -148,7 +158,7 @@ const categoriesSelectorCss = categories
id={category} id={category}
name="category" name="category"
class="hidden peer" class="hidden peer"
checked={category === "All"} checked={category === STATIC_CATEGORIES.All}
/> />
<label <label
class="cursor-pointer transition-all pr-4 opacity-25 peer-checked:opacity-100 hover:opacity-100 flex flex-row gap-2 items-center" class="cursor-pointer transition-all pr-4 opacity-25 peer-checked:opacity-100 hover:opacity-100 flex flex-row gap-2 items-center"
@ -175,7 +185,9 @@ const categoriesSelectorCss = categories
class:list={[ class:list={[
`${item.category}-item`, `${item.category}-item`,
{ {
"Recent-item": isRecent(item.pubIsoDate), [`${STATIC_CATEGORIES.Recent}-item`]: isRecent(
item.pubIsoDate,
),
}, },
"w-full", "w-full",
"border-l-8", "border-l-8",