1+ import { useEffect , useState } from 'react' ;
12import Link from 'next/link' ;
23import Image from 'next/image' ;
34import { useRouter } from 'next/router' ;
@@ -7,22 +8,33 @@ import Heading from '@/components/heading';
78import PostSeo from '@/components/post-seo' ;
89import useTranslation from '@/i18n/useTranslation' ;
910import { getAllPostSlugs , getContent } from '@/lib/content' ;
10- import { dateOptions } from '@/lib/constants' ;
11- import { getComponents , getLocalizedUrl } from '@/lib/util' ;
11+ import { dateOptions , siteBaseUrl } from '@/lib/constants' ;
12+ import { getPathViews } from '@/lib/goat-counter' ;
13+ import { getComponents , getLocalizedPath } from '@/lib/util' ;
1214
1315const Post = ( { mdxSource, frontMatter, hydrationComponentsList } ) => {
1416 const { t } = useTranslation ( ) ;
1517 const router = useRouter ( ) ;
1618 const { locale } = router ;
19+ const [ views , setViews ] = useState ( 0 ) ;
1720 const content = hydrate ( mdxSource , {
1821 components : getComponents ( hydrationComponentsList ) ,
1922 } ) ;
20- const localizedUrl = getLocalizedUrl ( router ) ;
23+ const localizedPath = getLocalizedPath ( router ) ;
2124 const { image : imagePath } = frontMatter ;
2225
26+ useEffect ( async ( ) => {
27+ const viewsCount = await getPathViews ( localizedPath ) ;
28+ setViews ( viewsCount ) ;
29+ } , [ ] ) ;
30+
2331 return (
2432 < >
25- < PostSeo locale = { locale } url = { localizedUrl } { ...frontMatter } />
33+ < PostSeo
34+ locale = { locale }
35+ url = { `${ siteBaseUrl } ${ localizedPath } ` }
36+ { ...frontMatter }
37+ />
2638 < div className = 'flex items-center px-2 py-1 mb-4 space-x-1 overflow-hidden text-xs font-medium text-gray-600 uppercase bg-gray-100 border border-gray-300 rounded-md' >
2739 < Link href = '/' >
2840 < a className = 'inline-flex items-center' > { t ( 'home' ) } </ a >
@@ -36,9 +48,14 @@ const Post = ({ mdxSource, frontMatter, hydrationComponentsList }) => {
3648 </ div >
3749 < article >
3850 < Heading > { frontMatter . title } </ Heading >
39- < time className = 'block my-2 text-sm text-center text-gray-600 md:text-left' >
40- { new Date ( frontMatter . date ) . toLocaleDateString ( locale , dateOptions ) }
41- </ time >
51+ < div className = 'flex justify-between my-2 text-sm text-gray-600' >
52+ < time >
53+ { new Date ( frontMatter . date ) . toLocaleDateString ( locale , dateOptions ) }
54+ </ time >
55+ < span >
56+ { views } { views !== 1 ? t ( 'views' ) : t ( 'view' ) }
57+ </ span >
58+ </ div >
4259 { imagePath ? (
4360 < picture className = 'block mx-auto my-3 max-w-media' >
4461 < Image
0 commit comments