feat: load more pagination for remote user posts
This commit is contained in:
@@ -65,8 +65,11 @@ export default async function RemoteActorPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const actor = actorResult.value;
|
const actor = actorResult.value;
|
||||||
const posts =
|
const postsData = postsResult.status === "fulfilled" ? postsResult.value : null;
|
||||||
postsResult.status === "fulfilled" ? postsResult.value.items : [];
|
const posts = postsData?.items ?? [];
|
||||||
|
const totalPages = postsData
|
||||||
|
? Math.ceil(postsData.total / postsData.per_page)
|
||||||
|
: 1;
|
||||||
const me =
|
const me =
|
||||||
meResult.status === "fulfilled" ? (meResult.value as Me | null) : null;
|
meResult.status === "fulfilled" ? (meResult.value as Me | null) : null;
|
||||||
const following =
|
const following =
|
||||||
@@ -77,7 +80,9 @@ export default async function RemoteActorPage({
|
|||||||
<RemoteUserProfile
|
<RemoteUserProfile
|
||||||
key={actor.url}
|
key={actor.url}
|
||||||
actor={actor}
|
actor={actor}
|
||||||
|
handle={handle}
|
||||||
initialPosts={posts}
|
initialPosts={posts}
|
||||||
|
initialTotalPages={totalPages}
|
||||||
me={me}
|
me={me}
|
||||||
initialFollowed={initialFollowed}
|
initialFollowed={initialFollowed}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { UserMinus, UserPlus } from "lucide-react";
|
import { UserMinus, UserPlus } from "lucide-react";
|
||||||
import { followUser, unfollowUser, RemoteActor, Thought, Me } from "@/lib/api";
|
import { followUser, unfollowUser, getRemoteActorPosts, RemoteActor, Thought, Me } from "@/lib/api";
|
||||||
import { Card } from "@/components/ui/card";
|
import { Card } from "@/components/ui/card";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||||
@@ -14,14 +14,18 @@ import { Connections } from "./connections";
|
|||||||
|
|
||||||
interface RemoteUserProfileProps {
|
interface RemoteUserProfileProps {
|
||||||
actor: RemoteActor;
|
actor: RemoteActor;
|
||||||
|
handle: string;
|
||||||
initialPosts: Thought[];
|
initialPosts: Thought[];
|
||||||
|
initialTotalPages: number;
|
||||||
me: Me | null;
|
me: Me | null;
|
||||||
initialFollowed?: boolean;
|
initialFollowed?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function RemoteUserProfile({
|
export function RemoteUserProfile({
|
||||||
actor,
|
actor,
|
||||||
|
handle,
|
||||||
initialPosts,
|
initialPosts,
|
||||||
|
initialTotalPages,
|
||||||
me,
|
me,
|
||||||
initialFollowed = false,
|
initialFollowed = false,
|
||||||
}: RemoteUserProfileProps) {
|
}: RemoteUserProfileProps) {
|
||||||
@@ -29,6 +33,24 @@ export function RemoteUserProfile({
|
|||||||
const [followLoading, setFollowLoading] = useState(false);
|
const [followLoading, setFollowLoading] = useState(false);
|
||||||
const { token } = useAuth();
|
const { token } = useAuth();
|
||||||
|
|
||||||
|
const [posts, setPosts] = useState<Thought[]>(initialPosts);
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [totalPages] = useState(initialTotalPages);
|
||||||
|
const [loadingMore, setLoadingMore] = useState(false);
|
||||||
|
|
||||||
|
const loadMore = async () => {
|
||||||
|
setLoadingMore(true);
|
||||||
|
try {
|
||||||
|
const result = await getRemoteActorPosts(handle, page + 1, token);
|
||||||
|
setPosts((prev) => [...prev, ...result.items]);
|
||||||
|
setPage((p) => p + 1);
|
||||||
|
} catch {
|
||||||
|
toast.error("Failed to load more posts.");
|
||||||
|
} finally {
|
||||||
|
setLoadingMore(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const [followersActive, setFollowersActive] = useState(false);
|
const [followersActive, setFollowersActive] = useState(false);
|
||||||
const [followingActive, setFollowingActive] = useState(false);
|
const [followingActive, setFollowingActive] = useState(false);
|
||||||
|
|
||||||
@@ -108,8 +130,20 @@ export function RemoteUserProfile({
|
|||||||
</TabsList>
|
</TabsList>
|
||||||
|
|
||||||
<TabsContent value="posts" className="space-y-4 mt-4">
|
<TabsContent value="posts" className="space-y-4 mt-4">
|
||||||
{initialPosts.length > 0 ? (
|
{posts.length > 0 ? (
|
||||||
<ThoughtList thoughts={initialPosts} currentUser={me} />
|
<>
|
||||||
|
<ThoughtList thoughts={posts} currentUser={me} />
|
||||||
|
{page < totalPages && (
|
||||||
|
<Button
|
||||||
|
onClick={loadMore}
|
||||||
|
disabled={loadingMore}
|
||||||
|
variant="outline"
|
||||||
|
className="w-full rounded-full"
|
||||||
|
>
|
||||||
|
{loadingMore ? "Loading…" : "Load more"}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
) : (
|
) : (
|
||||||
<Card className="flex items-center justify-center h-48">
|
<Card className="flex items-center justify-center h-48">
|
||||||
<p className="text-center text-muted-foreground">
|
<p className="text-center text-muted-foreground">
|
||||||
|
|||||||
Reference in New Issue
Block a user