|
1 | | -import { BlogPost } from '@/types' |
2 | | -import { getAllPosts } from '../../../utils/blog' |
3 | | -import { Pagination } from '../../components/blog/Pagination' |
4 | | -import { PostCard } from '../../components/blog/PostCard' |
5 | | -import { notFound } from 'next/navigation' |
6 | | -import { getUniqueCategoriesAndTags } from '../../../utils/getUniqueCategoriesAndTags' |
| 1 | +import { notFound } from "next/navigation"; |
| 2 | +import type { BlogPost } from "@/types"; |
| 3 | +import { getAllPosts } from "../../../utils/blog"; |
| 4 | +import { getUniqueCategoriesAndTags } from "../../../utils/getUniqueCategoriesAndTags"; |
| 5 | +import { Pagination } from "../../components/blog/Pagination"; |
| 6 | +import { PostCard } from "../../components/blog/PostCard"; |
7 | 7 |
|
8 | | -export async function generateStaticParams() { |
9 | | - const posts = (await getAllPosts()) ?? [] |
| 8 | +// export const |
10 | 9 |
|
11 | | - const postsPerPage = 10 |
| 10 | +// export async function generateStaticParams() { |
| 11 | +// const posts = (await getAllPosts()) ?? [] |
12 | 12 |
|
13 | | - const totalPages = Math.ceil(posts.length / postsPerPage) |
| 13 | +// const postsPerPage = 10 |
14 | 14 |
|
15 | | - const blogPages = Array.from({ length: totalPages }, (_, i) => ({ |
16 | | - page: (i + 1).toString(), |
17 | | - })) |
18 | | - const { categories, tags } = getUniqueCategoriesAndTags(posts) |
| 15 | +// const totalPages = Math.ceil(posts.length / postsPerPage) |
19 | 16 |
|
20 | | - const categoryPages = categories.map((category) => ({ |
21 | | - page: category.slug, |
22 | | - })) |
| 17 | +// const blogPages = Array.from({ length: totalPages }, (_, i) => ({ |
| 18 | +// page: (i + 1).toString(), |
| 19 | +// })) |
| 20 | +// const { categories, tags } = getUniqueCategoriesAndTags(posts) |
23 | 21 |
|
24 | | - const tagPages = tags.map((tag) => ({ |
25 | | - page: tag.slug, |
26 | | - })) |
27 | | - const allPages = [...blogPages, ...categoryPages, ...tagPages] |
| 22 | +// const categoryPages = categories.map((category) => ({ |
| 23 | +// page: category.slug, |
| 24 | +// })) |
28 | 25 |
|
29 | | - return allPages |
30 | | -} |
| 26 | +// const tagPages = tags.map((tag) => ({ |
| 27 | +// page: tag.slug, |
| 28 | +// })) |
| 29 | +// const allPages = [...blogPages, ...categoryPages, ...tagPages] |
| 30 | + |
| 31 | +// return allPages |
| 32 | +// } |
31 | 33 |
|
32 | 34 | export default async function BlogPage({ |
33 | | - params: { page }, |
| 35 | + params: { page }, |
34 | 36 | }: { |
35 | | - params: { page: string; final: string } |
| 37 | + params: { page: string; final: string }; |
36 | 38 | }) { |
37 | | - const posts: BlogPost[] = (await getAllPosts()) ?? [] |
38 | | - let actualPosts: BlogPost[] = [] |
39 | | - |
40 | | - const { categories, tags } = getUniqueCategoriesAndTags(posts) |
41 | | - |
42 | | - let intPage = 0 |
43 | | - let nextUrl = '' |
44 | | - let prevUrl = '' |
45 | | - |
46 | | - let isBasicBlog = false |
47 | | - |
48 | | - try { |
49 | | - intPage = parseInt(page) |
50 | | - } catch (e) {} |
51 | | - |
52 | | - const pageType = |
53 | | - intPage && !isNaN(intPage) |
54 | | - ? 'blog' |
55 | | - : tags.some((tag) => tag.slug === page) |
56 | | - ? 'tag' |
57 | | - : categories.some((cat) => cat.slug === page) |
58 | | - ? 'category' |
59 | | - : '' |
60 | | - |
61 | | - if (!pageType) { |
62 | | - return notFound() |
63 | | - } |
64 | | - |
65 | | - let title = 'Blog' |
66 | | - |
67 | | - if (pageType === 'blog') { |
68 | | - if (intPage < 1 || intPage > posts.length / 10 + 1) { |
69 | | - return notFound() |
70 | | - } |
71 | | - isBasicBlog = true |
72 | | - |
73 | | - actualPosts = posts.slice((intPage - 1) * 10, intPage * 10) |
74 | | - const hasNext = intPage < posts.length / 10 + 1 |
75 | | - const hasPrev = intPage > 1 |
76 | | - |
77 | | - nextUrl = hasNext ? `/blog/${intPage + 1}` : '' |
78 | | - prevUrl = hasPrev ? `/blog/${intPage - 1}` : '' |
79 | | - } |
80 | | - |
81 | | - if (pageType === 'category') { |
82 | | - actualPosts = posts.filter((post) => { |
83 | | - if (post.category?.slug === page) { |
84 | | - title = post.category?.title |
85 | | - return true |
86 | | - } |
87 | | - return false |
88 | | - }) |
89 | | - } |
90 | | - |
91 | | - if (pageType === 'tag') { |
92 | | - actualPosts = posts.filter((post) => |
93 | | - post.blog_tags?.some((tag) => { |
94 | | - if (tag.slug === page) { |
95 | | - title = tag.title |
96 | | - return true |
97 | | - } |
98 | | - return false |
99 | | - }), |
100 | | - ) |
101 | | - } |
102 | | - |
103 | | - const Title = |
104 | | - pageType === 'blog' ? ( |
105 | | - <h1 className="inline text-center font-sans text-8xl font-black">Blog</h1> |
106 | | - ) : pageType === 'tag' ? ( |
107 | | - <h1 className="button px-4 py-1 text-xl">{title}</h1> |
108 | | - ) : ( |
109 | | - <h1 className="border-2 border-black bg-orange-500 px-4 py-1 text-2xl font-bold">{title}</h1> |
110 | | - ) |
111 | | - |
112 | | - return ( |
113 | | - <> |
114 | | - <div className="flex h-40 items-center">{Title}</div> |
115 | | - {actualPosts?.map((post) => ( |
116 | | - <PostCard post={post} wide key={post.id} /> |
117 | | - ))} |
118 | | - {isBasicBlog && <Pagination prevUrl={prevUrl} nextUrl={nextUrl} />} |
119 | | - </> |
120 | | - ) |
| 39 | + const posts: BlogPost[] = (await getAllPosts()) ?? []; |
| 40 | + let actualPosts: BlogPost[] = []; |
| 41 | + |
| 42 | + const { categories, tags } = getUniqueCategoriesAndTags(posts); |
| 43 | + |
| 44 | + let intPage = 0; |
| 45 | + let nextUrl = ""; |
| 46 | + let prevUrl = ""; |
| 47 | + |
| 48 | + let isBasicBlog = false; |
| 49 | + |
| 50 | + try { |
| 51 | + intPage = parseInt(page); |
| 52 | + } catch (e) {} |
| 53 | + |
| 54 | + const pageType = |
| 55 | + intPage && !isNaN(intPage) |
| 56 | + ? "blog" |
| 57 | + : tags.some((tag) => tag.slug === page) |
| 58 | + ? "tag" |
| 59 | + : categories.some((cat) => cat.slug === page) |
| 60 | + ? "category" |
| 61 | + : ""; |
| 62 | + |
| 63 | + if (!pageType) { |
| 64 | + return notFound(); |
| 65 | + } |
| 66 | + |
| 67 | + let title = "Blog"; |
| 68 | + |
| 69 | + if (pageType === "blog") { |
| 70 | + if (intPage < 1 || intPage > posts.length / 10 + 1) { |
| 71 | + return notFound(); |
| 72 | + } |
| 73 | + isBasicBlog = true; |
| 74 | + |
| 75 | + actualPosts = posts.slice((intPage - 1) * 10, intPage * 10); |
| 76 | + const hasNext = intPage < posts.length / 10 + 1; |
| 77 | + const hasPrev = intPage > 1; |
| 78 | + |
| 79 | + nextUrl = hasNext ? `/blog/${intPage + 1}` : ""; |
| 80 | + prevUrl = hasPrev ? `/blog/${intPage - 1}` : ""; |
| 81 | + } |
| 82 | + |
| 83 | + if (pageType === "category") { |
| 84 | + actualPosts = posts.filter((post) => { |
| 85 | + if (post.category?.slug === page) { |
| 86 | + title = post.category?.title; |
| 87 | + return true; |
| 88 | + } |
| 89 | + return false; |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + if (pageType === "tag") { |
| 94 | + actualPosts = posts.filter((post) => |
| 95 | + post.blog_tags?.some((tag) => { |
| 96 | + if (tag.slug === page) { |
| 97 | + title = tag.title; |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | + }), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + const Title = |
| 106 | + pageType === "blog" ? ( |
| 107 | + <h1 className="inline text-center font-sans text-8xl font-black">Blog</h1> |
| 108 | + ) : pageType === "tag" ? ( |
| 109 | + <h1 className="button px-4 py-1 text-xl">{title}</h1> |
| 110 | + ) : ( |
| 111 | + <h1 className="border-2 border-black bg-orange-500 px-4 py-1 text-2xl font-bold"> |
| 112 | + {title} |
| 113 | + </h1> |
| 114 | + ); |
| 115 | + |
| 116 | + return ( |
| 117 | + <> |
| 118 | + <div className="flex h-40 items-center">{Title}</div> |
| 119 | + {actualPosts?.map((post) => ( |
| 120 | + <PostCard post={post} wide key={post.id} /> |
| 121 | + ))} |
| 122 | + {isBasicBlog && <Pagination prevUrl={prevUrl} nextUrl={nextUrl} />} |
| 123 | + </> |
| 124 | + ); |
121 | 125 | } |
0 commit comments