Add configurable homepage social links
This commit is contained in:
@@ -2,9 +2,11 @@ import { cookies } from "next/headers";
|
||||
import { AdminPostList } from "@/components/AdminPostList";
|
||||
import { AdminUserManager } from "@/components/AdminUserManager";
|
||||
import { CreatePostForm } from "@/components/CreatePostForm";
|
||||
import { SocialLinksManager } from "@/components/SocialLinksManager";
|
||||
import { cookieName, isAdminSession, verifySession } from "@/lib/auth";
|
||||
import { getDb } from "@/lib/mongo";
|
||||
import { buildOwnedPostFilter, buildPinnedSort, serializePost } from "@/lib/posts";
|
||||
import { getDefaultSocialLinks, getSiteSettings } from "@/lib/site-settings";
|
||||
import { findUserById, getEffectiveDailyPostLimit, getShanghaiDayRange } from "@/lib/users";
|
||||
import { Post } from "@/types/post";
|
||||
|
||||
@@ -203,13 +205,15 @@ export default async function AdminPage() {
|
||||
const adminView = isAdminSession(session);
|
||||
const roleLabel = ROLE_LABELS[(session?.role as ManagedUser["role"]) || "user"];
|
||||
|
||||
const [recentPosts, favoritePosts, availableTags, publishQuota, managedUsers] = await Promise.all([
|
||||
fetchRecentPosts(session),
|
||||
fetchFavoritePosts(session),
|
||||
fetchAvailableTags(session),
|
||||
fetchPublishQuota(session),
|
||||
adminView ? fetchManagedUsers() : Promise.resolve([] as ManagedUser[])
|
||||
]);
|
||||
const [recentPosts, favoritePosts, availableTags, publishQuota, managedUsers, siteSettings] =
|
||||
await Promise.all([
|
||||
fetchRecentPosts(session),
|
||||
fetchFavoritePosts(session),
|
||||
fetchAvailableTags(session),
|
||||
fetchPublishQuota(session),
|
||||
adminView ? fetchManagedUsers() : Promise.resolve([] as ManagedUser[]),
|
||||
adminView ? getSiteSettings() : Promise.resolve({ socialLinks: getDefaultSocialLinks() })
|
||||
]);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
@@ -218,7 +222,7 @@ export default async function AdminPage() {
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-semibold text-slate-900">内容后台</h1>
|
||||
<p className="text-sm text-slate-500">
|
||||
登录用户可以发布、编辑自己的内容和管理自己的收藏;管理员额外拥有置顶、删帖、删用户和调整用户等级/额度的全部权限。
|
||||
登录用户可以发布、编辑自己的内容和管理自己的收藏;管理员额外拥有置顶、删帖、删用户、调整用户等级/额度,以及维护首页社交链接的全部权限。
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
@@ -235,6 +239,8 @@ export default async function AdminPage() {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{adminView ? <SocialLinksManager initialLinks={siteSettings.socialLinks} /> : null}
|
||||
|
||||
<CreatePostForm
|
||||
availableTags={availableTags}
|
||||
publishLimit={publishQuota.limit}
|
||||
|
||||
53
app/api/admin/settings/route.ts
Normal file
53
app/api/admin/settings/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { cookieName, isAdminSession, verifySession } from "@/lib/auth";
|
||||
import { getSiteSettings, updateSiteSettings } from "@/lib/site-settings";
|
||||
|
||||
const socialLinkSchema = z.object({
|
||||
id: z.string().trim().min(1).max(80),
|
||||
label: z.string().trim().min(1).max(30),
|
||||
url: z.string().trim().url(),
|
||||
iconUrl: z.string().trim().url().optional().or(z.literal(""))
|
||||
});
|
||||
|
||||
const siteSettingsSchema = z.object({
|
||||
socialLinks: z.array(socialLinkSchema).max(20)
|
||||
});
|
||||
|
||||
async function requireAdmin(req: NextRequest) {
|
||||
const token = req.cookies.get(cookieName)?.value;
|
||||
const session = await verifySession(token);
|
||||
if (!isAdminSession(session)) {
|
||||
return null;
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireAdmin(req);
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "未授权" }, { status: 401 });
|
||||
}
|
||||
|
||||
const settings = await getSiteSettings();
|
||||
return NextResponse.json(settings);
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest) {
|
||||
const session = await requireAdmin(req);
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "未授权" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = siteSettingsSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
await updateSiteSettings({
|
||||
socialLinks: parsed.data.socialLinks
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, socialLinks: parsed.data.socialLinks });
|
||||
}
|
||||
15
app/page.tsx
15
app/page.tsx
@@ -1,7 +1,9 @@
|
||||
import { PostCard } from "@/components/PostCard";
|
||||
import { SocialLinksBar } from "@/components/SocialLinksBar";
|
||||
import { getDb } from "@/lib/mongo";
|
||||
import { buildPinnedSort, serializePost } from "@/lib/posts";
|
||||
import { buildSearchFilter } from "@/lib/search";
|
||||
import { getSiteSettings } from "@/lib/site-settings";
|
||||
import { Post } from "@/types/post";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
@@ -53,7 +55,11 @@ export default async function HomePage({
|
||||
const tag = searchParams?.tag?.trim();
|
||||
const q = searchParams?.q?.trim();
|
||||
const page = Number.parseInt(searchParams?.page || "1", 10) || 1;
|
||||
const { posts, total, totalPages, page: currentPage } = await fetchPosts({ tag, q, page });
|
||||
|
||||
const [{ posts, total, totalPages, page: currentPage }, siteSettings] = await Promise.all([
|
||||
fetchPosts({ tag, q, page }),
|
||||
getSiteSettings()
|
||||
]);
|
||||
|
||||
const buildHref = (targetPage: number) => {
|
||||
const params = new URLSearchParams();
|
||||
@@ -85,8 +91,13 @@ export default async function HomePage({
|
||||
<p className="mt-2 text-sm text-white/80">
|
||||
未登录用户可以浏览全部内容;登录用户可以发布并修改自己的内容;置顶内容会优先展示。
|
||||
</p>
|
||||
|
||||
<div className="mt-4">
|
||||
<SocialLinksBar links={siteSettings.socialLinks} title="社交链接" />
|
||||
</div>
|
||||
|
||||
{tag ? (
|
||||
<div className="mt-3 inline-flex items-center gap-2 rounded-full bg-white/15 px-3 py-1 text-xs font-medium">
|
||||
<div className="mt-4 inline-flex items-center gap-2 rounded-full bg-white/15 px-3 py-1 text-xs font-medium">
|
||||
<span>当前标签 #{tag}</span>
|
||||
<a
|
||||
href={clearTagHref}
|
||||
|
||||
Reference in New Issue
Block a user