Skip to content

Commit

Permalink
Move post thread metadata from page to layout
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelfan committed Oct 11, 2024
1 parent 774e527 commit 15a77e3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 43 deletions.
49 changes: 49 additions & 0 deletions src/app/dashboard/user/[handle]/(post)/post/[id]/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { getAgent } from "@/lib/api/bsky/agent";
import { getPostThread } from "@/lib/api/bsky/feed";
import { AppBskyFeedDefs, AppBskyFeedPost } from "@atproto/api";
import { Metadata } from "next";

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const agent = await getAgent();
const { handle, id } = params;
const { data } = await agent.resolveHandle({ handle });
const uri = `at://${data.did}/app.bsky.feed.post/${id}`;
const post = await getPostThread(uri);

const isThreadViewPost = AppBskyFeedDefs.isThreadViewPost(post)
? true
: false;
const threadPost = isThreadViewPost
? (post.post as AppBskyFeedDefs.PostView)
: null;

const text =
threadPost && AppBskyFeedPost.isRecord(threadPost.record)
? threadPost.record.text
: "";

const title =
text !== ""
? `${threadPost?.author.displayName || params.handle}: "${text}"`
: `Post by ${params.handle}`;

return {
title: title,
description: "Feed",
};
}

interface Props {
params: {
id: string;
handle: string;
};
}

export default function PostThreadLayout({
children,
}: {
children: React.ReactNode;
}) {
return <>{children}</>;
}
34 changes: 1 addition & 33 deletions src/app/dashboard/user/[handle]/(post)/post/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
import PostThreadContainer from "@/containers/thread/PostThreadContainer";
import { getSessionFromServer } from "@/lib/api/auth/session";
import { getProfile } from "@/lib/api/bsky/actor";
import { getAgent } from "@/lib/api/bsky/agent";
import { getPostThread } from "@/lib/api/bsky/feed";
import { AppBskyFeedDefs, AppBskyFeedPost } from "@atproto/api";
import { Metadata } from "next";

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const agent = await getAgent();
const { handle, id } = params;
const { data } = await agent.resolveHandle({ handle });
const uri = `at://${data.did}/app.bsky.feed.post/${id}`;
const post = await getPostThread(uri);

const isThreadViewPost = AppBskyFeedDefs.isThreadViewPost(post) ? true : false;
const threadPost = isThreadViewPost ? post.post as AppBskyFeedDefs.PostView : null;

const text =
threadPost && AppBskyFeedPost.isRecord(threadPost.record)
? threadPost.record.text
: "";

const title = text !== "" ? `${threadPost?.author.displayName || params.handle}: "${text}"` : `Post by ${params.handle}`;

return {
title: title,
description: "Feed",
};
}

interface Props {
params: {
Expand All @@ -39,17 +10,14 @@ interface Props {
};
}

export default async function Page(props: Props) {
export default function Page(props: Props) {
const { id, handle } = props.params;
const { query } = props.searchParams;
const session = await getSessionFromServer();
const profile = await getProfile(session?.user.bskySession.handle);

return (
<PostThreadContainer
id={id}
handle={handle}
viewerAvatar={profile?.avatar}
repliesTextFilter={query ?? ""}
/>
);
Expand Down
26 changes: 16 additions & 10 deletions src/containers/thread/PostThreadContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import { useEffect, useState } from "react";
import { useSearchParams } from "next/navigation";
import { AppBskyFeedDefs } from "@atproto/api";
import { PostView } from "@atproto/api/dist/client/types/app/bsky/feed/defs";
import ThreadPost from "@/components/contentDisplay/threadPost/ThreadPost";
Expand All @@ -23,19 +22,23 @@ import { MAX_REPLY_CONTAINERS } from "@/lib/consts/thread";
import ThreadActionsContainer from "./ThreadActionsContainer";
import { replyIncludes } from "@/lib/utils/text";
import { THREAD_VIEW_PREFS } from "@/lib/consts/settings";
import useProfile from "@/lib/hooks/bsky/actor/useProfile";
import { useSession } from "next-auth/react";

interface Props {
id: string;
handle: string;
repliesTextFilter: string;
viewerAvatar?: string;
}

export default function PostThreadContainer(props: Props) {
const { id, handle, viewerAvatar, repliesTextFilter } = props;
const { id, handle, repliesTextFilter } = props;
const [maxReplies, setMaxReplies] = useState(MAX_REPLY_CONTAINERS);
const agent = useAgent();
const router = useRouter();
const { data: session } = useSession();

const { data: profile } = useProfile(session?.user.bskySession.handle);

const {
data: thread,
Expand Down Expand Up @@ -139,13 +142,16 @@ export default function PostThreadContainer(props: Props) {
<div>
<ThreadPost post={thread?.post as PostView} filter={contentFilter} />
<WhoCanReply post={thread?.post as PostView} />
<ThreadActionsContainer
avatar={viewerAvatar}
post={thread?.post as PostView}
rounded={textSearch === "" && filteredReplies === 0}
onThreadSort={setThreadPreferences}
preferredSort={threadPreferences.sort}
/>
{profile && (
<ThreadActionsContainer
avatar={profile?.avatar}
post={thread?.post as PostView}
rounded={textSearch === "" && filteredReplies === 0}
onThreadSort={setThreadPreferences}
preferredSort={threadPreferences.sort}
/>
)}

{textSearch !== "" && filteredReplies === 0 && (
<div className="border-skin-base border-t">
<FeedAlert
Expand Down

0 comments on commit 15a77e3

Please sign in to comment.