The Node.js® Website
at main 1.2 kB view raw
1import type { FC } from 'react'; 2 3import { getClientContext } from '@/client-context'; 4import CrossLink from '@/components/Common/CrossLink'; 5import getBlogData from '@/next-data/blogData'; 6 7const WithBlogCrossLinks: FC = async () => { 8 const { pathname } = getClientContext(); 9 10 // Extracts from the static URL the components used for the Blog Post slug 11 const [, , category, postname] = pathname.split('/'); 12 13 const { posts } = await getBlogData(category); 14 15 const currentItem = posts.findIndex( 16 ({ slug }) => slug === `/blog/${category}/${postname}` 17 ); 18 19 const [previousCrossLink, nextCrossLink] = [ 20 posts[currentItem - 1], 21 posts[currentItem + 1], 22 ]; 23 24 return ( 25 <div className="mt-4 grid w-full grid-cols-2 gap-4 xs:grid-cols-1"> 26 {(previousCrossLink && ( 27 <CrossLink 28 type="previous" 29 text={previousCrossLink.title} 30 link={previousCrossLink.slug} 31 /> 32 )) || <div />} 33 34 {nextCrossLink && ( 35 <CrossLink 36 type="next" 37 text={nextCrossLink.title} 38 link={nextCrossLink.slug} 39 /> 40 )} 41 </div> 42 ); 43}; 44 45export default WithBlogCrossLinks;