Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit e17e801

Browse files
committed
Update post views
1 parent a7108bb commit e17e801

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

app/[username]/[slug]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default async function PostPage({
108108
/>
109109
</div>
110110
<div className="flex space-x-4">
111-
<PostViews meta={post?.meta} />
111+
<PostViews post={post} />
112112
<FavoriteButton post={post} />
113113
</div>
114114
</div>

app/[username]/[slug]/post-views.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22

33
import * as React from 'react'
44

5+
import { createClient } from '@/supabase/client'
56
import { LucideIcon } from '@/lib/lucide-icon'
67
import { getMetaValue } from '@/lib/utils'
7-
import { type PostMeta } from '@/types/database'
8+
import { type Post } from '@/types/database'
89

910
interface PostViewsProps extends React.HTMLAttributes<HTMLDivElement> {
10-
meta?: PostMeta[]
11+
post?: Post
1112
}
1213

13-
const PostViews = ({ meta, ...props }: PostViewsProps) => {
14-
const views = getMetaValue(meta, 'views', '0')
14+
const PostViews = ({ post, ...props }: PostViewsProps) => {
15+
const views = getMetaValue(post?.meta, 'views', '0')
16+
const setPostViews = React.useCallback(async (postid: number) => {
17+
const supabase = createClient()
18+
const result = await supabase.rpc('set_post_views', { postid })
19+
// console.log(result)
20+
}, [])
21+
22+
React.useEffect(() => {
23+
if (typeof window !== 'undefined' && post?.id) {
24+
setPostViews(post?.id)
25+
}
26+
}, [post?.id, setPostViews])
1527

1628
return (
1729
<div className="flex items-center" {...props}>

0 commit comments

Comments
 (0)