-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 블로그 데이터 함수 추가 * feat: 블로그 글 리스트 추가 * feat: 블로그 글 보기 구현
- Loading branch information
Showing
17 changed files
with
493 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
INTERNAL_API_KEY=TEST_API_KEY_ONLY_FOR_DEVELOPMENT | ||
RECRUIT_NOTION_API_KEY= | ||
RECRUIT_NOTION_PAGE_ID= | ||
BLOG_NOTION_API_KEY= | ||
BLOG_NOTION_DB_ID= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { z } from 'zod'; | ||
|
||
import { propertyResolver } from '../notion/property'; | ||
import { NotionPage } from '../notion/types'; | ||
import { internalProcedure, router } from '../trpc/stub'; | ||
|
||
export const blogRouter = router({ | ||
list: internalProcedure | ||
.input( | ||
z.object({ | ||
category: z.string().optional(), | ||
}), | ||
) | ||
.query(async ({ input, ctx }) => { | ||
const pages = await (async () => { | ||
const cacheKey = () => `cache:blog:list`; | ||
|
||
const cached = await ctx.kv.get(cacheKey(), 'json'); | ||
if (cached) { | ||
return cached as PagesType; | ||
} | ||
|
||
const pages = await ctx.blog.notion.getDatabaseContents(ctx.blog.databaseId); | ||
type PagesType = typeof pages; | ||
|
||
await ctx.kv.put(cacheKey(), JSON.stringify(pages)); | ||
|
||
return pages; | ||
})(); | ||
|
||
const articles = pages.map((page) => { | ||
const properties = extractArticleProperties(page.properties); | ||
|
||
return { | ||
...properties, | ||
id: page.id, | ||
}; | ||
}); | ||
const categories = articles | ||
.map((article) => article.category) | ||
.filter((category): category is string => !!category); | ||
|
||
const filtered = articles.filter((article) => !input.category || article.category === input.category); | ||
|
||
const data = { | ||
articles: filtered, | ||
categories, | ||
}; | ||
|
||
return data; | ||
}), | ||
invalidateList: internalProcedure.mutation(async ({ ctx }) => { | ||
await ctx.kv.delete('cache:blog:list'); | ||
}), | ||
article: internalProcedure.input(z.object({ id: z.string() })).query(async ({ input, ctx }) => { | ||
const cached = await ctx.kv.get(`cache:blog:article:${input.id}`, 'json'); | ||
if (cached) { | ||
return cached as ArticleData; | ||
} | ||
|
||
const [page, blocks] = await Promise.all([ctx.blog.notion.getPage(input.id), ctx.blog.notion.getBlocks(input.id)]); | ||
const properties = extractArticleProperties(page.properties); | ||
|
||
const articleData = { ...properties, blocks }; | ||
type ArticleData = typeof articleData; | ||
|
||
await ctx.kv.put(`cache:blog:article:${input.id}`, JSON.stringify(articleData)); | ||
|
||
return articleData; | ||
}), | ||
invalidateArticle: internalProcedure.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => { | ||
await ctx.kv.delete(`cache:blog:article:${input.id}`); | ||
}), | ||
invalidateAllArticles: internalProcedure.mutation(async ({ ctx }) => { | ||
const { keys } = await ctx.kv.list({ prefix: 'cache:blog:article:' }); | ||
|
||
await Promise.all(keys.map(async ({ name }) => ctx.kv.delete(name))); | ||
}), | ||
}); | ||
|
||
function extractArticleProperties(properties: NotionPage['properties']) { | ||
const resolver = propertyResolver(properties); | ||
|
||
const title = resolver.title('title'); | ||
const editors = resolver.multiSelect('editors').map((raw) => { | ||
const [name, role = undefined] = raw.split('|'); | ||
|
||
return { | ||
name, | ||
role, | ||
}; | ||
}); | ||
const publishedAt = resolver.date('publishedAt'); | ||
const category = resolver.select('category'); | ||
const thumbnailFiles = resolver.files('thumbnail'); | ||
const thumbnail = thumbnailFiles.length > 0 ? thumbnailFiles[0] : null; | ||
const publish = resolver.checkbox('publish'); | ||
|
||
return { | ||
title, | ||
editors, | ||
publishedAt, | ||
category, | ||
thumbnail, | ||
publish, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { FC } from 'react'; | ||
|
||
import ArticlePage from '@/components/blog/ArticleDetail'; | ||
|
||
interface BlogArticlePageProps { | ||
params: { id: string }; | ||
} | ||
|
||
export const runtime = 'edge'; | ||
|
||
const BlogArticlePage: FC<BlogArticlePageProps> = ({ params }) => { | ||
return ( | ||
<> | ||
<ArticlePage id={params.id} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default BlogArticlePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { FC } from 'react'; | ||
|
||
import ArticleList from '@/components/blog/ArticleList'; | ||
|
||
interface BlogCategoryPageProps { | ||
params: { category: string }; | ||
} | ||
|
||
export const runtime = 'edge'; | ||
|
||
const BlogCategoryPage: FC<BlogCategoryPageProps> = ({ params }) => { | ||
return ( | ||
<> | ||
<ArticleList category={params.category} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default BlogCategoryPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FC } from 'react'; | ||
|
||
import ArticleList from '@/components/blog/ArticleList'; | ||
|
||
interface BlogPageProps {} | ||
|
||
export const runtime = 'edge'; | ||
|
||
const BlogPage: FC<BlogPageProps> = async ({}) => { | ||
return ( | ||
<div> | ||
<ArticleList /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BlogPage; |
Oops, something went wrong.