11'use client'
22
3+ import { AdsInTable } from '@/app/ads/AdsInTable'
4+ import { Ad , ad_per_content } from '@/app/lib/definitions'
35import FormattedDate from '@/app/ui/FormattedDate'
46import Table , { TableBody , TableHead } from '@/app/ui/Table'
5- import { allPosts } from 'contentlayer/generated'
7+ import { Post , allPosts } from 'contentlayer/generated'
68import Link from 'next/link'
79import { useRouter , useSearchParams } from 'next/navigation'
810
11+ type PostOrAd = Post | Ad
12+
13+ const insertAdsIntoPosts = ( posts : Post [ ] , interval : number ) : PostOrAd [ ] => {
14+ const result : PostOrAd [ ] = [ ]
15+ for ( let i = 0 ; i < posts . length ; i ++ ) {
16+ if ( i > 0 && i % interval === 0 ) {
17+ result . push ( { type : 'Ad' } )
18+ }
19+ result . push ( posts [ i ] )
20+ }
21+ return result
22+ }
23+
924export default function PostTable ( ) {
1025 const { push } = useRouter ( )
1126 const heads = [ 'Title' , 'Date' , 'Categories' ]
@@ -19,6 +34,8 @@ export default function PostTable() {
1934 : allPosts
2035 ) . sort ( ( a , b ) => new Date ( b . date ) . getTime ( ) - new Date ( a . date ) . getTime ( ) )
2136
37+ const postsWithAds = insertAdsIntoPosts ( posts , ad_per_content )
38+
2239 return (
2340 < Table >
2441 < TableHead >
@@ -31,29 +48,44 @@ export default function PostTable() {
3148 </ tr >
3249 </ TableHead >
3350 < TableBody >
34- { posts . map ( ( post ) => (
35- < tr
36- key = { post . slug }
37- onClick = { ( ) => push ( post . url ) }
38- className = "border-b bg-white hover:cursor-pointer hover:bg-gray-50 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-600"
39- >
40- < th
41- scope = "row"
42- className = "whitespace-nowrap px-6 py-4 font-medium text-gray-900 dark:text-white"
51+ { postsWithAds . map ( ( item , index ) => {
52+ if ( 'type' in item && item . type === 'Ad' ) {
53+ return (
54+ < tr
55+ key = { `ad-${ index } ` }
56+ className = "max-h-14 border-b bg-white dark:border-gray-700 dark:bg-gray-800"
57+ >
58+ < td colSpan = { heads . length } >
59+ < AdsInTable />
60+ </ td >
61+ </ tr >
62+ )
63+ }
64+ const post = item as Post
65+ return (
66+ < tr
67+ key = { post . slug }
68+ onClick = { ( ) => push ( post . url ) }
69+ className = "border-b bg-white hover:cursor-pointer hover:bg-gray-50 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-600"
4370 >
44- < Link
45- href = { post . url }
46- className = "hover: text-blue-600 dark:hover: text-blue-400 "
71+ < th
72+ scope = "row"
73+ className = "whitespace-nowrap px-6 py-4 font-medium text-gray-900 dark:text-white "
4774 >
48- { post . title }
49- </ Link >
50- </ th >
51- < td className = "truncate px-6 py-4" >
52- < FormattedDate date = { post . date } />
53- </ td >
54- < td className = "truncate px-6 py-4" > { post . tags . join ( ', ' ) } </ td >
55- </ tr >
56- ) ) }
75+ < Link
76+ href = { post . url }
77+ className = "hover:text-blue-600 dark:hover:text-blue-400"
78+ >
79+ { post . title }
80+ </ Link >
81+ </ th >
82+ < td className = "truncate px-6 py-4" >
83+ < FormattedDate date = { post . date } />
84+ </ td >
85+ < td className = "truncate px-6 py-4" > { post . tags . join ( ', ' ) } </ td >
86+ </ tr >
87+ )
88+ } ) }
5789 { posts . length === 0 && (
5890 < tr >
5991 < td
0 commit comments