feat: infinite query for home page posts
This commit is contained in:
@@ -4,77 +4,97 @@ import { Paginated, type PostOnSiteHome, Notes } from "~/lib/types"
|
||||
import { EmptyState } from "../ui/EmptyState"
|
||||
import { useRouter } from "next/router"
|
||||
import { Image } from "~/components/ui/Image"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
|
||||
export const SiteHome: React.FC<{
|
||||
posts?: Notes
|
||||
}> = ({ posts }) => {
|
||||
postPages?: Notes[]
|
||||
fetchNextPage: () => void
|
||||
hasNextPage?: boolean
|
||||
isFetchingNextPage?: boolean
|
||||
}> = ({ postPages, fetchNextPage, hasNextPage, isFetchingNextPage }) => {
|
||||
const router = useRouter()
|
||||
|
||||
if (!posts) return null
|
||||
if (!postPages?.length) return null
|
||||
|
||||
let currentLength = 0
|
||||
|
||||
return (
|
||||
<div className="">
|
||||
{posts.total === 0 && <EmptyState />}
|
||||
{posts.total > 0 && (
|
||||
<>
|
||||
{!postPages[0].total && <EmptyState />}
|
||||
{postPages[0].total && (
|
||||
<div className="xlog-posts space-y-8">
|
||||
{posts.list.map((post) => {
|
||||
const excerpt = post.summary?.content
|
||||
return (
|
||||
<Link
|
||||
key={post.id}
|
||||
href={`/${post.slug || post.id}`}
|
||||
className="xlog-post hover:bg-zinc-100 bg-white transition-colors px-5 py-7 -mx-5 first:-mt-5 sm:rounded-xl flex flex-col sm:flex-row items-center"
|
||||
>
|
||||
<div className="flex-1 flex justify-center flex-col w-full sm:w-auto">
|
||||
<h3 className="xlog-post-title text-2xl font-bold">
|
||||
{post.title}
|
||||
</h3>
|
||||
<div className="xlog-post-meta text-sm text-zinc-400 mt-1">
|
||||
<span className="xlog-post-date">
|
||||
{formatDate(post.date_published)}
|
||||
</span>
|
||||
<span className="xlog-post-tags ml-4 space-x-1">
|
||||
{post.tags
|
||||
?.filter((tag) => tag !== "post" && tag !== "page")
|
||||
.map((tag) => (
|
||||
<span
|
||||
className="hover:text-zinc-600"
|
||||
key={tag}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
router.push(`/tag/${tag}`)
|
||||
}}
|
||||
>
|
||||
#{tag}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
{postPages.map((posts) =>
|
||||
posts.list.map((post) => {
|
||||
currentLength++
|
||||
return (
|
||||
<Link
|
||||
key={post.id}
|
||||
href={`/${post.slug || post.id}`}
|
||||
className="xlog-post hover:bg-zinc-100 bg-white transition-colors px-5 py-7 -mx-5 first:-mt-5 sm:rounded-xl flex flex-col sm:flex-row items-center"
|
||||
>
|
||||
<div className="flex-1 flex justify-center flex-col w-full sm:w-auto">
|
||||
<h3 className="xlog-post-title text-2xl font-bold">
|
||||
{post.title}
|
||||
</h3>
|
||||
<div className="xlog-post-meta text-sm text-zinc-400 mt-1">
|
||||
<span className="xlog-post-date">
|
||||
{formatDate(post.date_published)}
|
||||
</span>
|
||||
<span className="xlog-post-tags ml-4 space-x-1">
|
||||
{post.tags
|
||||
?.filter((tag) => tag !== "post" && tag !== "page")
|
||||
.map((tag) => (
|
||||
<span
|
||||
className="hover:text-zinc-600"
|
||||
key={tag}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
router.push(`/tag/${tag}`)
|
||||
}}
|
||||
>
|
||||
#{tag}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
className="xlog-post-excerpt mt-3 text-zinc-500"
|
||||
style={{
|
||||
wordBreak: "break-word",
|
||||
}}
|
||||
>
|
||||
{post.summary?.content}
|
||||
{post.summary?.content && "..."}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
className="xlog-post-excerpt mt-3 text-zinc-500"
|
||||
style={{
|
||||
wordBreak: "break-word",
|
||||
}}
|
||||
>
|
||||
{excerpt}
|
||||
{excerpt && "..."}
|
||||
</div>
|
||||
</div>
|
||||
{post.cover && (
|
||||
<div className="xlog-post-cover flex items-center relative w-full sm:w-24 h-24 mt-2 sm:ml-4 sm:mt-0">
|
||||
<Image
|
||||
className="object-cover rounded"
|
||||
alt="cover"
|
||||
fill={true}
|
||||
src={post.cover}
|
||||
></Image>
|
||||
</div>
|
||||
)}
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
{post.cover && (
|
||||
<div className="xlog-post-cover flex items-center relative w-full sm:w-24 h-24 mt-2 sm:ml-4 sm:mt-0">
|
||||
<Image
|
||||
className="object-cover rounded"
|
||||
alt="cover"
|
||||
fill={true}
|
||||
src={post.cover}
|
||||
></Image>
|
||||
</div>
|
||||
)}
|
||||
</Link>
|
||||
)
|
||||
}),
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{hasNextPage && (
|
||||
<Button
|
||||
className="mt-8 w-full hover:bg-zinc-100 bg-zinc-50 transition-colors text-sm"
|
||||
variant="text"
|
||||
onClick={fetchNextPage}
|
||||
isLoading={isFetchingNextPage}
|
||||
>
|
||||
There are {postPages[0].total - currentLength} more{" "}
|
||||
{postPages[0].total - currentLength > 1 ? "posts" : "post"}, click to
|
||||
load more
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ export const getServerSideProps = async (
|
||||
await prefetchGetPagesBySite(
|
||||
{
|
||||
site: domainOrSubdomain,
|
||||
take: 1000,
|
||||
type: "post",
|
||||
visibility: PageVisibilityEnum.Published,
|
||||
render: true,
|
||||
|
||||
+11
-19
@@ -216,12 +216,13 @@ export async function getPagesBySite(
|
||||
source: "Crossbell Note",
|
||||
identity: input.site,
|
||||
platform: "Crossbell",
|
||||
limit: input.take || 1000,
|
||||
limit: input.take || 10,
|
||||
order_by: "date_published" as "date_published",
|
||||
filter: {
|
||||
tags: [...(input.tags || []), ...(crossbell ? [] : [input.type])],
|
||||
applications: [...(crossbell ? [] : ["xlog"])],
|
||||
},
|
||||
cursor: "",
|
||||
cursor: input.cursor,
|
||||
}
|
||||
|
||||
let pages: Notes = {
|
||||
@@ -229,17 +230,10 @@ export async function getPagesBySite(
|
||||
list: [],
|
||||
}
|
||||
|
||||
let cursor = ""
|
||||
do {
|
||||
options.cursor = cursor
|
||||
const res = (await (customUnidata || unidata).notes.get(options)) || {
|
||||
total: 0,
|
||||
list: [],
|
||||
}
|
||||
pages.total = res.total
|
||||
pages.list = pages.list.concat(res.list)
|
||||
cursor = res.cursor
|
||||
} while (cursor)
|
||||
pages = (await (customUnidata || unidata).notes.get(options)) || {
|
||||
total: 0,
|
||||
list: [],
|
||||
}
|
||||
|
||||
if (!crossbell) {
|
||||
const local = getLocalPages({
|
||||
@@ -284,12 +278,10 @@ export async function getPagesBySite(
|
||||
)
|
||||
break
|
||||
}
|
||||
pages.list = pages.list
|
||||
.filter(
|
||||
(page) => page.date_published !== new Date("9999-01-01").toISOString(),
|
||||
)
|
||||
.sort((a, b) => +new Date(b.date_published) - +new Date(a.date_published))
|
||||
pages.total = pages.list.length
|
||||
pages.list = pages.list.filter(
|
||||
(page) => page.date_published !== new Date("9999-01-01").toISOString(),
|
||||
)
|
||||
pages.total = pages.total - ((input.take || 10) - pages.list.length)
|
||||
|
||||
pages?.list.map((page) => {
|
||||
expandPage(page, input.render || false)
|
||||
|
||||
@@ -17,7 +17,6 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
const pages = await fetchGetPagesBySite(
|
||||
{
|
||||
site: domainOrSubdomain,
|
||||
take: 1000,
|
||||
type: "post",
|
||||
visibility: PageVisibilityEnum.Published,
|
||||
render: true,
|
||||
|
||||
@@ -14,7 +14,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
|
||||
return {
|
||||
props: {
|
||||
dehydratedState: dehydrate(queryClient),
|
||||
dehydratedState: JSON.parse(JSON.stringify(dehydrate(queryClient))),
|
||||
domainOrSubdomain,
|
||||
},
|
||||
}
|
||||
@@ -23,13 +23,19 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
function SiteIndexPage({ domainOrSubdomain }: { domainOrSubdomain: string }) {
|
||||
const posts = useGetPagesBySite({
|
||||
site: domainOrSubdomain,
|
||||
take: 1000,
|
||||
type: "post",
|
||||
visibility: PageVisibilityEnum.Published,
|
||||
render: true,
|
||||
})
|
||||
|
||||
return <SiteHome posts={posts.data} />
|
||||
return (
|
||||
<SiteHome
|
||||
postPages={posts.data?.pages}
|
||||
fetchNextPage={posts.fetchNextPage}
|
||||
hasNextPage={posts.hasNextPage}
|
||||
isFetchingNextPage={posts.isFetchingNextPage}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
SiteIndexPage.getLayout = (page: ReactElement) => {
|
||||
|
||||
@@ -17,8 +17,17 @@ export const prefetchGetPagesBySite = async (
|
||||
queryClient: QueryClient,
|
||||
) => {
|
||||
const key = ["getPagesBySite", input.site, input]
|
||||
await queryClient.prefetchQuery(key, async () => {
|
||||
return cacheGet(key, () => pageModel.getPagesBySite(input))
|
||||
await queryClient.prefetchInfiniteQuery({
|
||||
queryKey: key,
|
||||
queryFn: async ({ pageParam }) => {
|
||||
return cacheGet(key, () =>
|
||||
pageModel.getPagesBySite({
|
||||
...input,
|
||||
cursor: pageParam,
|
||||
}),
|
||||
)
|
||||
},
|
||||
getNextPageParam: (lastPage) => lastPage.cursor,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+18
-3
@@ -1,4 +1,9 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import {
|
||||
useQuery,
|
||||
useMutation,
|
||||
useQueryClient,
|
||||
useInfiniteQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import * as pageModel from "~/models/page.model"
|
||||
import { useUnidata } from "./unidata"
|
||||
import { useContract } from "./crossbell"
|
||||
@@ -7,8 +12,18 @@ export const useGetPagesBySite = (
|
||||
input: Parameters<typeof pageModel.getPagesBySite>[0],
|
||||
) => {
|
||||
const unidata = useUnidata()
|
||||
return useQuery(["getPagesBySite", input.site, input], async () => {
|
||||
return pageModel.getPagesBySite(input, unidata)
|
||||
return useInfiniteQuery({
|
||||
queryKey: ["getPagesBySite", input.site, input],
|
||||
queryFn: async ({ pageParam }) => {
|
||||
return pageModel.getPagesBySite(
|
||||
{
|
||||
...input,
|
||||
cursor: pageParam,
|
||||
},
|
||||
unidata,
|
||||
)
|
||||
},
|
||||
getNextPageParam: (lastPage) => lastPage.cursor,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user