Skip to content

Commit 8b7967f

Browse files
authored
Merge pull request #28 from ansidev/feature/page-difficulty
Page: /difficulties/{slug}
2 parents fd7b2a9 + a2c5527 commit 8b7967f

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

src/components/LeetCodeDifficulty.astro

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
---
2+
import type { HTMLAttributes } from 'astro/types'
23
import kebabCase from 'lodash.kebabcase'
34
4-
export interface Props {
5+
export interface Props extends HTMLAttributes<'a' | 'span'> {
6+
tag?: string
57
difficulty: string
68
}
79
8-
const { difficulty } = Astro.props
10+
const { difficulty, tag = 'a', ...props } = Astro.props
11+
12+
const Tag = tag
13+
14+
const tagProps: Record<string, string> = {}
15+
if (tag === 'a') {
16+
tagProps.href = `/difficulties/${kebabCase(difficulty)}`
17+
}
18+
919
const difficultyStyleMap: Record<string, string> = {
1020
Easy: 'difficulty-easy',
1121
Medium: 'difficulty-medium',
@@ -21,12 +31,17 @@ const getDifficultyCssClass: (difficulty: string) => string = (
2131
}
2232
---
2333

24-
<a
25-
href={`/difficulties/${kebabCase(difficulty)}`}
26-
class:list={['px-2 py-1', getDifficultyCssClass(difficulty)]}
34+
<Tag
35+
{...tagProps}
36+
class:list={[
37+
'px-2 py-1',
38+
getDifficultyCssClass(difficulty),
39+
tag === 'a' ? 'link' : '',
40+
props.class,
41+
]}
2742
>
2843
{difficulty}
29-
</a>
44+
</Tag>
3045

3146
<style lang="scss">
3247
$difficulties: 'easy', 'medium', 'hard';
@@ -37,9 +52,11 @@ const getDifficultyCssClass: (difficulty: string) => string = (
3752
@apply border-2;
3853
@apply border-difficulty-#{$difficulty};
3954
@apply text-difficulty-#{$difficulty};
40-
@apply hover:bg-site-bg;
41-
@apply hover:border-difficulty-#{$difficulty}-hover;
42-
@apply hover:text-difficulty-#{$difficulty}-hover;
55+
&.link {
56+
@apply hover:bg-site-bg;
57+
@apply hover:border-difficulty-#{$difficulty}-hover;
58+
@apply hover:text-difficulty-#{$difficulty}-hover;
59+
}
4360
}
4461
}
4562
</style>

src/pages/difficulties/[slug].astro

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
import { getCollection } from 'astro:content'
3+
import kebabCase from 'lodash.kebabcase'
4+
5+
import LeetCodeDifficulty from '@/components/LeetCodeDifficulty.astro'
6+
import PostList from '@/components/post/PostList.astro'
7+
import siteConfig from '@/configs/site'
8+
import AppLayout from '@/layouts/AppLayout.astro'
9+
10+
export async function getStaticPaths() {
11+
const allDifficulties = new Set<string>()
12+
const allPosts = await getCollection(
13+
'leetcode-solutions',
14+
({ data }) => data.difficulty?.length > 0
15+
)
16+
allPosts.forEach((post) => allDifficulties.add(post.data.difficulty))
17+
18+
return Array.from(allDifficulties).map((difficulty) => {
19+
const slug = kebabCase(difficulty)
20+
const filteredPosts = allPosts.filter(
21+
(post) => post.data.difficulty === difficulty
22+
)
23+
return {
24+
params: { slug },
25+
props: {
26+
posts: filteredPosts,
27+
difficulty,
28+
},
29+
}
30+
})
31+
}
32+
33+
const { difficulty, posts } = Astro.props
34+
const { title, description, author } = siteConfig
35+
---
36+
37+
<AppLayout
38+
title={`${difficulty} - ${title}`}
39+
description={description}
40+
author={author.name}
41+
headerCssClasses="max-w-xl px-8"
42+
>
43+
<main class="mx-auto my-4 p-4 max-w-xl text-site-header-text">
44+
<div class="grid grid-flow-col auto-cols-max gap-2 m-4 justify-center">
45+
<h1 class="text-style-primary">
46+
Posts by difficulty:
47+
<LeetCodeDifficulty tag="span" difficulty={difficulty} class="mx-2" />
48+
</h1>
49+
</div>
50+
<PostList posts={posts} />
51+
</main>
52+
</AppLayout>

0 commit comments

Comments
 (0)