feat(settings): 后台可开关全站评论

关闭后禁止新评论与回复,已有内容仍可浏览;设置经 WS 同步到前台。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-16 07:01:46 +08:00
parent bcb4f6a603
commit 3e135542d3
5 changed files with 73 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import {
Code2,
Globe,
Loader2,
MessageSquare,
Save,
Sparkles,
UserPlus,
@@ -49,6 +50,7 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
const [siteName, setSiteName] = useState(initial.site_name);
const [siteDescription, setSiteDescription] = useState(initial.site_description);
const [allowRegister, setAllowRegister] = useState(initial.allow_register);
const [allowComments, setAllowComments] = useState(initial.allow_comments !== false);
const [codeAutoFold, setCodeAutoFold] = useState(
initial.code_block_auto_fold !== false
);
@@ -62,6 +64,7 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
const [savedDesc, setSavedDesc] = useState(initial.site_description);
const [saving, setSaving] = useState(false);
const [togglingReg, setTogglingReg] = useState(false);
const [togglingComments, setTogglingComments] = useState(false);
const [togglingFold, setTogglingFold] = useState(false);
const [savingFoldLines, setSavingFoldLines] = useState(false);
const [uiAnimations, setUiAnimations] = useState(initial.ui_animations !== false);
@@ -116,6 +119,20 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
}
};
const toggleComments = async () => {
if (togglingComments) return;
const next = !allowComments;
setTogglingComments(true);
try {
const res = await apiUpdateSiteSettings({ allow_comments: next });
setAllowComments(res.allow_comments);
toast(res.allow_comments ? "已开放评论" : "已关闭评论", "ok");
} catch (e) {
toast(e instanceof Error ? e.message : "保存失败");
} finally {
setTogglingComments(false);
}
};
const toggleCodeFold = async () => {
if (togglingFold) return;
@@ -201,7 +218,7 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
站点设置
</h1>
<p className="meta mt-1.5 text-[12.5px]">
配置站点名称、简介与注册开关;主题色请到「外观」页调整
配置站点名称、简介与注册/评论开关;主题色请到「外观」页调整
</p>
</div>
@@ -265,6 +282,23 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
/>
</section>
<section className="j13-module p-5 sm:p-6 mb-5 flex items-start justify-between gap-4 flex-wrap">
<div className="min-w-0 flex-1">
<h2 className="text-[15px] font-semibold flex items-center gap-1.5" style={{ color: "var(--ink)" }}>
<MessageSquare size={15} /> 开放评论
</h2>
<p className="meta text-[12.5px] mt-1 leading-relaxed">
关闭后全站禁止发表新评论与回复,已有评论仍可浏览。适用于 ICP/IPC
备案审核等合规需要;前台仅提示「暂停评论」,不会展示备案事由。
</p>
</div>
<Switch
checked={allowComments}
disabled={togglingComments}
onChange={() => void toggleComments()}
label="开放评论"
/>
</section>
<section className="j13-module p-5 sm:p-6 mb-5">
<div className="flex items-start justify-between gap-4 flex-wrap">

View File

@@ -547,6 +547,8 @@ export interface PublicSettings {
site_name: string;
site_description: string;
allow_register: boolean;
/** 全站是否开放评论/回复;缺省 true */
allow_comments: boolean;
post_cooldown_hours: number;
/** 超过 N 行的代码块是否默认折叠;缺省 true */
code_block_auto_fold: boolean;
@@ -568,6 +570,7 @@ const DEFAULT_PUBLIC_SETTINGS: PublicSettings = {
site_name: "姜十三论坛",
site_description: "姜十三论坛 - 技术分享与讨论社区",
allow_register: true,
allow_comments: true,
post_cooldown_hours: 24,
code_block_auto_fold: true,
code_block_fold_lines: 5,
@@ -587,6 +590,7 @@ function normalizePublicSettings(data: Partial<PublicSettings> | null | undefine
? String(data.site_description).trim()
: DEFAULT_PUBLIC_SETTINGS.site_description,
allow_register: data?.allow_register !== false,
allow_comments: data?.allow_comments !== false,
post_cooldown_hours:
typeof data?.post_cooldown_hours === "number" && data.post_cooldown_hours >= 0
? data.post_cooldown_hours
@@ -1530,6 +1534,7 @@ export type UpdateSiteSettingsBody = {
site_name?: string;
site_description?: string;
allow_register?: boolean;
allow_comments?: boolean;
post_cooldown_hours?: number;
code_block_auto_fold?: boolean;
code_block_fold_lines?: number;

View File

@@ -44,6 +44,7 @@ export interface RtSettingsData {
site_name?: string;
site_description?: string;
allow_register?: boolean;
allow_comments?: boolean;
post_cooldown_hours?: number;
code_block_auto_fold?: boolean;
code_block_fold_lines?: number;