-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68d78cb
commit 5e363d1
Showing
12 changed files
with
65 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { QueryClientProvider, QueryClient } from '@tanstack/react-query' | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools' | ||
|
||
export function ReactQueryProvider({ children }: React.PropsWithChildren) { | ||
const [client] = React.useState(new QueryClient()) | ||
|
||
return ( | ||
<QueryClientProvider client={client}> | ||
{children} | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</QueryClientProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
'use client' | ||
|
||
import { useEffect, useState } from 'react' | ||
|
||
import { getPostViews } from '@/app/actions' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
export function PostViews({ slug, prev }: { slug: string; prev: number }) { | ||
return ( | ||
<span> | ||
<span title="views"> | ||
<ViewCount slug={slug} prev={prev} /> views | ||
</span> | ||
) | ||
} | ||
|
||
function ViewCount({ slug, prev }: { slug: string; prev: number }) { | ||
const [views, setViews] = useState(prev) | ||
|
||
useEffect(() => { | ||
// Fetch updated view count | ||
async function fetchViews() { | ||
// Wait 1 second to view to be added to db | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
getPostViews({ slug }).then((count) => { | ||
if (count != null) setViews(count) | ||
}) | ||
} | ||
fetchViews() | ||
}, [slug]) | ||
|
||
return <>{views ?? '-'}</> | ||
const { data } = useQuery({ | ||
queryKey: ['postViews', slug], | ||
queryFn: async () => { | ||
// Add artificial delay for smoother UI transition when switching between posts | ||
await new Promise((resolve) => setTimeout(resolve, 500)) | ||
return getPostViews({ slug }) | ||
}, | ||
}) | ||
return <>{data != null ? data : prev}</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters