feat: add replace_hero hook to custom section
This commit is contained in:
@@ -3,18 +3,20 @@
|
||||
/**
|
||||
* PostsGrid — client-side body for /u/[handle] (Posts tab).
|
||||
*
|
||||
* Split out from page.tsx so the page itself can be a server component
|
||||
* that exports `generateMetadata` for the public-profile OG card. The
|
||||
* /u/[handle]/layout.tsx still owns ProfileHeader + tab strip + the
|
||||
* outer max-w-3xl container; this grid just renders the post grid
|
||||
* inside that shell. Tapping a thumbnail jumps to
|
||||
* /u/[handle]/posts#post-<id> where the Instagram-style scroll feed
|
||||
* shows the full passages.
|
||||
* Split out from page.tsx so the page itself can be a server component that
|
||||
* exports `generateMetadata` for the public-profile OG card. The
|
||||
* /u/[handle]/layout.tsx still owns ProfileHeader + tab strip + the outer
|
||||
* container (and 404s unknown handles); this just renders the post grid.
|
||||
*
|
||||
* Window-virtualised, infinite-scroll 2-up grid (usePostScrollFeed keyset-pages
|
||||
* the user's posts). Tapping a thumbnail jumps to /u/[handle]/posts#post-<id>
|
||||
* where the scroll feed shows the full passages.
|
||||
*/
|
||||
|
||||
import { use, useEffect, useState } from "react";
|
||||
import { profile, type Post } from "@/lib/social";
|
||||
import { PostViews } from "@/components/social/post/post-views";
|
||||
import { use } from "react";
|
||||
import { profile } from "@/lib/social";
|
||||
import { VirtualPostGrid } from "@/components/social/post/virtual-post-grid";
|
||||
import { usePostScrollFeed } from "@/components/social/post/use-post-scroll-feed";
|
||||
import { usePublicProfile } from "./profile-context";
|
||||
|
||||
export function PostsGrid({
|
||||
@@ -24,34 +26,18 @@ export function PostsGrid({
|
||||
}) {
|
||||
const { handle } = use(params);
|
||||
const data = usePublicProfile();
|
||||
const [feed, setFeed] = useState<Post[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
profile
|
||||
.feed(handle, { limit: 30 })
|
||||
.then((rows) => {
|
||||
if (!cancelled) setFeed(rows ?? []);
|
||||
})
|
||||
.catch(() => {
|
||||
// Layout already 404'd if the handle is unknown; if the feed
|
||||
// itself fails we just show an empty grid rather than blocking
|
||||
// the page with an error toast.
|
||||
})
|
||||
.finally(() => !cancelled && setLoading(false));
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [handle]);
|
||||
|
||||
if (loading) {
|
||||
return <p className="text-sm text-muted-foreground">Loading…</p>;
|
||||
}
|
||||
const { feed, loading, hasMore, sentinelRef } = usePostScrollFeed(
|
||||
(before, limit) =>
|
||||
profile.feed(handle, { limit, before: before ?? undefined }),
|
||||
24,
|
||||
);
|
||||
|
||||
return (
|
||||
<PostViews
|
||||
<VirtualPostGrid
|
||||
posts={feed}
|
||||
loading={loading}
|
||||
hasMore={hasMore}
|
||||
sentinelRef={sentinelRef}
|
||||
hrefFor={(p) => `/u/${data.handle}/posts#post-${p.id}`}
|
||||
emptyMessage="No posts yet — quotes shared by this user will appear here."
|
||||
/>
|
||||
|
||||
+31
-60
@@ -3,47 +3,39 @@
|
||||
/**
|
||||
* /u/me — owner Posts grid (the default tab).
|
||||
*
|
||||
* Grid-only — no Grid/Scroll toggle. Tapping a thumbnail jumps to
|
||||
* /u/me/posts#post-<id> where the Instagram-style scroll feed renders
|
||||
* the full passages.
|
||||
* Window-virtualised, infinite-scroll 2-up grid (usePostScrollFeed keyset-pages
|
||||
* the owner's posts; VirtualPostGrid renders + windows them). Tapping a
|
||||
* thumbnail jumps to /u/me/posts#post-<id> where the scroll feed renders the
|
||||
* full passages.
|
||||
*
|
||||
* Editing the profile lives on its own page (/settings/profile), reached
|
||||
* from the header's "Edit profile" button.
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { LayoutGrid } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { useAuth } from "@/components/auth/auth-provider";
|
||||
import { PostGridCard } from "@/components/social/post/post-grid-card";
|
||||
import { VirtualPostGrid } from "@/components/social/post/virtual-post-grid";
|
||||
import { usePostScrollFeed } from "@/components/social/post/use-post-scroll-feed";
|
||||
import { ConfirmModal } from "@/components/ui/confirm-modal";
|
||||
import { posts, SocialError, type Post } from "@/lib/social";
|
||||
|
||||
export default function MyProfilePage() {
|
||||
const { user } = useAuth();
|
||||
// Defer the authed /posts/mine fetch until we have a user.
|
||||
if (!user) return null;
|
||||
return <OwnerGrid />;
|
||||
}
|
||||
|
||||
const [feed, setFeed] = useState<Post[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Post id pending confirmation — modal opens when non-null and runs
|
||||
// the actual delete on confirm.
|
||||
function OwnerGrid() {
|
||||
const { feed, setFeed, loading, hasMore, error, setError, sentinelRef } =
|
||||
usePostScrollFeed(
|
||||
(before, limit) => posts.mine({ limit, before: before ?? undefined }),
|
||||
24,
|
||||
);
|
||||
const [pendingDelete, setPendingDelete] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user) return;
|
||||
posts
|
||||
.mine()
|
||||
.then((rows) => setFeed(rows ?? []))
|
||||
.catch((err) =>
|
||||
setError(
|
||||
err instanceof SocialError ? err.message : "Could not load posts.",
|
||||
),
|
||||
)
|
||||
.finally(() => setLoading(false));
|
||||
}, [user]);
|
||||
|
||||
// Card delete buttons stage an id; ConfirmModal runs `performDelete`
|
||||
// when the user confirms. We throw on failure so the modal stays open
|
||||
// with an inline error message.
|
||||
// Card delete buttons stage an id; ConfirmModal runs `performDelete` when the
|
||||
// user confirms. We throw on failure so the modal stays open with an error.
|
||||
async function performDelete() {
|
||||
const id = pendingDelete;
|
||||
if (!id) return;
|
||||
@@ -64,29 +56,20 @@ export default function MyProfilePage() {
|
||||
setFeed((rows) => rows.map((p) => (p.id === updated.id ? updated : p)));
|
||||
}
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
return (
|
||||
<section>
|
||||
{error ? (
|
||||
<p className="text-sm text-destructive">{error}</p>
|
||||
) : loading ? (
|
||||
<p className="text-sm text-muted-foreground">Loading…</p>
|
||||
) : feed.length === 0 ? (
|
||||
<EmptyPosts />
|
||||
) : (
|
||||
<div className="grid grid-cols-2 gap-2 sm:gap-3">
|
||||
{feed.map((p) => (
|
||||
<PostGridCard
|
||||
key={p.id}
|
||||
post={p}
|
||||
onDelete={(id) => setPendingDelete(id)}
|
||||
onUpdated={onPostUpdated}
|
||||
href={`/u/me/posts#post-${p.id}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{error ? <p className="mb-3 text-sm text-destructive">{error}</p> : null}
|
||||
|
||||
<VirtualPostGrid
|
||||
posts={feed}
|
||||
loading={loading}
|
||||
hasMore={hasMore}
|
||||
sentinelRef={sentinelRef}
|
||||
hrefFor={(p) => `/u/me/posts#post-${p.id}`}
|
||||
onDelete={(id) => setPendingDelete(id)}
|
||||
onUpdated={onPostUpdated}
|
||||
emptyMessage="You haven't posted anything yet. Select any passage in the knowledgebase and use the toolbar to share it to your feed."
|
||||
/>
|
||||
|
||||
<ConfirmModal
|
||||
open={!!pendingDelete}
|
||||
@@ -99,15 +82,3 @@ export default function MyProfilePage() {
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function EmptyPosts() {
|
||||
return (
|
||||
<div className="flex flex-col items-center text-center gap-3 py-12 text-muted-foreground">
|
||||
<LayoutGrid className="size-8" />
|
||||
<p className="text-sm">
|
||||
You haven't posted anything yet. Select any passage in the
|
||||
knowledgebase and use the toolbar to share it to your feed.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user