From a5e4155a752fa090c7bc3751a803b4359453e56c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 25 Apr 2026 12:30:41 +0000 Subject: fix: frontend-backend connection bugs Agent-Logs-Url: https://github.com/bertyuan/next-blog/sessions/f86da32b-3af7-4393-8077-ce3435137221 Co-authored-by: bertyuan <189593334+bertyuan@users.noreply.github.com> --- src/app/(main)/(home)/actions.ts | 5 ++-- src/app/(main)/(home)/posts/[slug]/page.client.tsx | 5 ++-- src/app/(main)/(home)/posts/page.tsx | 19 ++++++++++---- src/app/(main)/(home)/tags/[...slug]/page.tsx | 29 ++++++++++++++++------ 4 files changed, 41 insertions(+), 17 deletions(-) (limited to 'src/app/(main)') diff --git a/src/app/(main)/(home)/actions.ts b/src/app/(main)/(home)/actions.ts index 5b0c456..c4f586f 100644 --- a/src/app/(main)/(home)/actions.ts +++ b/src/app/(main)/(home)/actions.ts @@ -1,13 +1,14 @@ 'use server'; +import { env } from '@/env'; import { getContact, updateContact } from '@/lib/resend'; import { ActionError, actionClient } from '@/lib/safe-action'; import { NewsletterSchema } from '@/lib/validators'; import { getSession } from '@/server/auth'; import { Resend } from 'resend'; -const resend = new Resend(process.env.RESEND_API_KEY as string); -const audienceId = process.env.RESEND_AUDIENCE_ID as string; +const resend = new Resend(env.RESEND_API_KEY); +const audienceId = env.RESEND_AUDIENCE_ID; const splitName = (name = '') => { const [firstName, ...lastName] = name.split(' ').filter(Boolean); diff --git a/src/app/(main)/(home)/posts/[slug]/page.client.tsx b/src/app/(main)/(home)/posts/[slug]/page.client.tsx index 7a97f56..13dbfeb 100644 --- a/src/app/(main)/(home)/posts/[slug]/page.client.tsx +++ b/src/app/(main)/(home)/posts/[slug]/page.client.tsx @@ -7,7 +7,7 @@ import { Icons } from '@/components/icons/icons'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import { Comments } from '@fuma-comment/react'; -import { redirect } from 'next/navigation'; +import { useRouter } from 'next/navigation'; import { useRef } from 'react'; import { toast } from 'sonner'; import { useCopyToClipboard } from 'usehooks-ts'; @@ -42,6 +42,7 @@ export function PostComments({ slug, className, }: { slug: string; className?: string }) { + const router = useRouter(); return ( { - redirect('/login'); + router.push('/login'); }, }} /> diff --git a/src/app/(main)/(home)/posts/page.tsx b/src/app/(main)/(home)/posts/page.tsx index 40ebcda..daced35 100644 --- a/src/app/(main)/(home)/posts/page.tsx +++ b/src/app/(main)/(home)/posts/page.tsx @@ -58,12 +58,14 @@ export default async function Page(props: { const pageIndex = searchParams.page ? Number.parseInt( Array.isArray(searchParams.page) - ? searchParams.page[0] ?? '' + ? (searchParams.page[0] ?? '') : searchParams.page, - 10 + 10, ) - 1 : 0; + if (Number.isNaN(pageIndex)) notFound(); + // 获取文章(带分页) const { posts, totalDocs, totalPages } = await getPublishedPosts({ limit: postsPerPage, @@ -106,7 +108,9 @@ export default async function Page(props: { })} - {totalPages > 1 && } + {totalPages > 1 && ( + + )} ); } @@ -118,12 +122,17 @@ type Props = { export async function generateMetadata( props: Props, - parent: ResolvingMetadata + parent: ResolvingMetadata, ): Promise { const searchParams = await props.searchParams; const pageIndex = searchParams.page - ? Number.parseInt(searchParams.page as string, 10) + ? Number.parseInt( + Array.isArray(searchParams.page) + ? (searchParams.page[0] ?? '') + : searchParams.page, + 10, + ) : 1; const isFirstPage = pageIndex === 1 || !searchParams.page; diff --git a/src/app/(main)/(home)/tags/[...slug]/page.tsx b/src/app/(main)/(home)/tags/[...slug]/page.tsx index 71615cb..45fa52f 100644 --- a/src/app/(main)/(home)/tags/[...slug]/page.tsx +++ b/src/app/(main)/(home)/tags/[...slug]/page.tsx @@ -5,7 +5,7 @@ import { NumberedPagination } from '@/components/numbered-pagination'; import { PostCard } from '@/components/posts/post-card'; import { Section } from '@/components/section'; import { createMetadata } from '@/lib/metadata'; -import { getPostsByTag, getAllTags } from '@/lib/payload-posts'; +import { getAllTags, getPostsByTag } from '@/lib/payload-posts'; import type { Metadata, ResolvingMetadata } from 'next'; import { notFound, redirect } from 'next/navigation'; @@ -57,7 +57,11 @@ const Header = ({ ); -const Pagination = ({ pageIndex, tag, totalPages }: { pageIndex: number; tag: string; totalPages: number }) => { +const Pagination = ({ + pageIndex, + tag, + totalPages, +}: { pageIndex: number; tag: string; totalPages: number }) => { const handlePageChange = async (page: number) => { 'use server'; redirect(`/tags/${tag}?page=${page}`); @@ -88,12 +92,14 @@ export default async function Page(props: { const pageIndex = searchParams.page ? Number.parseInt( Array.isArray(searchParams.page) - ? searchParams.page[0] ?? '' + ? (searchParams.page[0] ?? '') : searchParams.page, - 10 + 10, ) - 1 : 0; + if (Number.isNaN(pageIndex)) notFound(); + const { posts, totalDocs, totalPages } = await getPostsByTag(tag, { limit: postsPerPage, page: pageIndex + 1, @@ -106,7 +112,12 @@ export default async function Page(props: { return ( <> -
+
{posts.map((post) => { @@ -126,7 +137,9 @@ export default async function Page(props: { })}
- {totalPages > 1 && } + {totalPages > 1 && ( + + )} ); @@ -153,9 +166,9 @@ export async function generateMetadata( const pageIndex = searchParams.page ? Number.parseInt( Array.isArray(searchParams.page) - ? searchParams.page[0] ?? '' + ? (searchParams.page[0] ?? '') : searchParams.page, - 10 + 10, ) : 1; -- cgit v1.2.3