([])
- useEffect(() => {
- FetchPost().then(e => {
- if (e) {
- setFetched(e)
- }
- }
- )
- }, [])
- return (
- {fetched.map(post =>
+const Single = ({ post }: { post: z.infer
}) => {
+ return (
+
{post.content}
{post.enclosure.length > 0 && post.enclosure[0] != null &&
{post.enclosure.map((enc, index) => {
if (enc) {
return
})
+ key={index}
+ src={MediaPathTrc(enc)}
+ alt={`Post media ${post.id}-${index}`}
+ className="rounded-md h-32 object-cover aspect-video flex-shrink-0"
+ />
} else {
return ""
}
@@ -36,6 +28,43 @@ export const Fetch = () => {
PID: {post.id}
發文於: {post.post_at.toLocaleTimeString()}
-
)}
- )
+
+ )
+}
+
+export const Fetch = () => {
+ const {
+ data,
+ error,
+ fetchNextPage,
+ hasNextPage,
+ isFetching,
+ status,
+ } = useInfiniteQuery({
+ queryKey: ['id'],
+ queryFn: async ({ pageParam }) => {
+ return await FetchPost(pageParam == 0 ? undefined : pageParam)
+ },
+ initialPageParam: 0,
+ getNextPageParam: (lastPage, _) => {
+ return lastPage !== null && lastPage.length > 0 && (lastPage[lastPage.length - 1].id - 1) > 0 ? lastPage[lastPage.length - 1].id - 1 : undefined
+ },
+ })
+
+ return (
+ hasNextPage && !isFetching && fetchNextPage()}>
+ {status === 'pending' &&
Loading
}
+ {status === 'error' &&
Error: {error.message}
}
+ {data && data.pages.map((group, i) => (
+
+ {group && group.map(post => (
+
+ ))}
+
+ ))}
+ {hasNextPage &&
+
}
+ {!hasNextPage &&
見底了
}
+
+ )
}
diff --git a/src/Post.tsx b/src/Post.tsx
index ff3b335..8e27f0a 100644
--- a/src/Post.tsx
+++ b/src/Post.tsx
@@ -113,7 +113,7 @@ export default function PostForm() {
{/* Aligned items, added items-center */}
>
)
-}
\ No newline at end of file
+}
diff --git a/src/api.ts b/src/api.ts
index c9453a2..e78ed21 100644
--- a/src/api.ts
+++ b/src/api.ts
@@ -6,18 +6,17 @@ type Inputs = {
signing?: string
files?: FileList
}
+export const SinglePost_t = z.object({
+ id: z.number(),
+ content: z.string(),
+ signing: z.nullable(z.string()),
+ post_at: z.coerce.date(),
+ heart: z.number(),
+ igid: z.nullable(z.string()),
+ enclosure: z.array(z.nullable(z.string())),
+})
-export const FetchPost_t = z.array(
- z.object({
- id: z.number(),
- content: z.string(),
- signing: z.nullable(z.string()),
- post_at: z.coerce.date(),
- heart: z.number(),
- igid: z.nullable(z.string()),
- enclosure: z.array(z.nullable(z.string())),
- })
-)
+export const FetchPost_t = z.array(SinglePost_t)
export const Post = async (post_form: Inputs) => {
// Delete Blank Item