Skip to content

Commit

Permalink
CategoryCard in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Jun 8, 2024
1 parent 10b4e60 commit 12c73ea
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/todo3.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,6 @@ codesandbox embed
cva default styles vs default variant, primer
design transparent logo for default ogImage
astro 4.10 types and zod for env vars
CategoryListCard
small PostItem
```
22 changes: 22 additions & 0 deletions src/components/CategoryCard.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
import { Icon } from 'astro-icon/components';
import { getCategoryIcon } from '@/modules/post';
import { CATEGORIES } from '@/constants/collections';
import { ROUTES } from '@/constants/routes';
import { cn } from '@/utils/styles';
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
category: string;
text: string;
}
const { category, text, class: className, ...props } = Astro.props;
const categoryIcon = getCategoryIcon(category);
---

<div class={cn('inline-flex items-center gap-2 border border-base-300 p-4', className)}>
<Icon name={categoryIcon} class="h-8 w-8" />
<a {...props} href={`${ROUTES.CATEGORIES}${category}`}>{text}</a>
</div>
12 changes: 8 additions & 4 deletions src/components/FilterList.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { getFilterLinks } from '@/modules/post';
import CategoryCard from '@/components/CategoryCard.astro';
import Tag from '@/components/Tag.astro';
import { cn } from '@/utils/styles';
Expand All @@ -20,11 +21,14 @@ const itemLinks = getFilterLinks(items, pathname);
{
itemLinks.length > 0 && (
<ul {...props} class={cn('flex flex-wrap gap-2', className)}>
{itemLinks.map(({ href, text, isActive, type }) => (
{itemLinks.map(({ href, text, textWithCount, isActive, type }) => (
<li class="inline-block">
<Tag href={href} size="md" class={cn({ 'underline font-bold text-accent': isActive })}>
{text}
</Tag>
{type === 'tag' && (
<Tag href={href} size="md" class={cn({ 'underline font-bold text-accent': isActive })}>
{textWithCount}
</Tag>
)}
{type === 'category' && <CategoryCard {href} category={text} text={textWithCount} />}
</li>
))}
</ul>
Expand Down
6 changes: 2 additions & 4 deletions src/components/PostMeta.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CATEGORIES } from '@/constants/collections';
import { ROUTES } from '@/constants/routes';
import { formatDate, formatDateIso } from '@/utils/datetime';
import { cn } from '@/utils/styles';
import { getCategoryIcon } from '@/modules/post';
interface Props {
publishDate: Date;
Expand All @@ -17,9 +18,6 @@ interface Props {
const { category, publishDate, updatedDate, readingTime, class: className } = Astro.props;
const categoryIcon = CATEGORIES.find((item) => item.name === category)?.icon;
const shouldDisplay = Object.values(Astro.props).some(Boolean);
// add variance authority
// fix this html
Expand All @@ -36,7 +34,7 @@ const shouldDisplay = Object.values(Astro.props).some(Boolean);
)}
{category && (
<span>·</span>
<Icon name={categoryIcon} />
<Icon name={getCategoryIcon(category)} />
<Link href={`${ROUTES.CATEGORIES}${category}`}>{category}</Link>
)}
{readingTime && (
Expand Down
4 changes: 4 additions & 0 deletions src/constants/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const TAGS = [

/** adjust this later */
export const CATEGORIES = [
// add color here
// extract find function
{
name: 'tutorials',
icon: 'mdi:teach',
Expand Down Expand Up @@ -50,6 +52,8 @@ export const CATEGORIES = [
},
] as const;

export type CategoryIconType = (typeof CATEGORIES)[number]['icon'];

// todo: use imported images here
export const DEFAULTS_POST = {
NO_HERO: false,
Expand Down
19 changes: 16 additions & 3 deletions src/modules/post.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getAllEntries } from '@/modules/common';
import { COLLECTIONS } from '@/constants/collections';
import { CATEGORIES, COLLECTIONS } from '@/constants/collections';
import { ROUTES } from '@/constants/routes';
import { CONFIG } from '@/config';
import { renderMarkdown } from '@/utils/markdown';

import type { CategoryIconType } from '@/constants/collections';
import type { Post, PostCollection } from '@/types/post';
import type { MarkdownProcessorRenderResult } from '@astrojs/markdown-remark';
import type { MarkdownHeading } from 'astro';
Expand Down Expand Up @@ -144,6 +145,8 @@ export interface Filter {
export interface FilterLink {
href: string;
text: string;
count: number;
textWithCount: string;
isActive: boolean;
type: FilterType;
}
Expand All @@ -156,12 +159,12 @@ export const getFilterLinks = (filterItems: Filter[], pathname?: string): Filter
const itemText = type === 'tag' ? `#${text}` : text;

const href = `${pathSegment}${text}`;
const textWithCount = `${itemText}(${count})`;
const textWithCount = `${itemText} ${count}`;

// unused, wont display in category and tag list
const isActive = href === pathname;

const link = { href, text: textWithCount, isActive, type };
const link = { href, text, count, textWithCount, isActive, type };

return link;
});
Expand Down Expand Up @@ -218,3 +221,13 @@ export const getSortedUniqueCategoriesWithCount = (posts: PostCollection[]): Fil
const sortedCategoriesWithCount = categoriesWithCount.slice().sort((a, b) => b.count - a.count);
return sortedCategoriesWithCount;
};

export const getCategoryIcon = (category: string): CategoryIconType => {
// set default to prevent breaking build
const defaultIcon = CATEGORIES[0].icon;
const foundIcon = CATEGORIES.find((item) => item.name === category)?.icon;

const categoryIcon = foundIcon ?? defaultIcon;

return categoryIcon;
};

0 comments on commit 12c73ea

Please sign in to comment.