feat: dispaly pageview at the bottom

This commit is contained in:
KazooTTT
2024-11-25 10:43:18 +08:00
parent b3da4152ac
commit 41f48998a2
3 changed files with 28 additions and 44 deletions

View File

@ -1,40 +1,36 @@
'use client'
import { useEffect, useState } from 'react';
import { useEffect, useState } from 'react'
interface PageViewsProps {
slug: string;
slug: string
}
export default function PageViews({ slug }: PageViewsProps) {
const [views, setViews] = useState<number | null>(null);
const [views, setViews] = useState<number | null>(null)
useEffect(() => {
console.log('PageViews component mounted with slug:', slug);
console.log('PageViews component mounted with slug:', slug)
async function updatePageView() {
try {
// First increment the view count
const postResponse = await fetch(`/api/pageview/${slug}`, {
method: 'POST',
});
if (!postResponse.ok) throw new Error('Failed to increment view count');
method: 'POST'
})
if (!postResponse.ok) throw new Error('Failed to increment view count')
// Then get the updated count
const { views } = await postResponse.json();
console.log('Updated views:', views);
setViews(views);
const { views } = await postResponse.json()
console.log('Updated views:', views)
setViews(views)
} catch (error) {
console.error('Error updating page views:', error);
setViews(null);
console.error('Error updating page views:', error)
setViews(null)
}
}
updatePageView();
}, [slug]);
updatePageView()
}, [slug])
return (
<span className="text-sm text-gray-500">
Views: {views === null ? '-' : views}
</span>
);
return <span className='text-sm text-gray-500'>Views: {views === null ? '-' : views}</span>
}

View File

@ -3,9 +3,7 @@ import { Icon } from 'astro-icon/components'
import PageViews from '../PageViews'
const pathname = new URL(Astro.request.url).pathname
const isBlogPost = pathname.startsWith('/blog/') && pathname !== '/blog'
const slug = pathname === '/' ? 'home' : pathname.replace(/^\/|\/$/g, '')
console.log('Slug:', slug)
---
<footer class='mx-auto mt-24 w-full'>
@ -17,18 +15,12 @@ console.log('Slug:', slug)
<p class=''>2024 kazoottt. All rights reserved.</p>
</div>
<div class='flex items-center justify-between'>
<!-- Social Brands -->
<div class='flex items-center gap-x-4'>
{
!isBlogPost && (
<div class='text-center'>
<PageViews client:load slug={slug} />
</div>
)
}
<!-- Linkedin -->
<a
class='inline-block text-muted-foreground transition-all hover:text-muted-foreground/75'

View File

@ -5,7 +5,6 @@ import BlogHero from '@/components/blog/Hero.astro'
import TOC from '@/components/blog/TOC.astro'
import Button from '@/components/Button.astro'
import PageLayout from './BaseLayout.astro'
import PageViews from '@/components/PageViews'
interface Props {
post: CollectionEntry<'post'>
@ -44,9 +43,6 @@ const { headings } = await post.render()
<article class='flex-1 flex-grow break-words' data-pagefind-body>
<div id='blog-hero'>
<BlogHero content={post} />
<div class="mt-2">
<PageViews client:load slug={slug} />
</div>
</div>
<div
id='blog-gallery'