"use client"; import { Code2, Globe, Loader2, MessageSquare, Save, Sparkles, UserPlus, } from "lucide-react"; import { useState } from "react"; import { AdminPageHeader, AdminSection, AdminSettingRow, AdminSwitch, } from "@/components/admin"; import { apiUpdateSiteSettings, type PublicSettings } from "@/lib/api"; import { toast } from "@/lib/toast"; 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 ); const [foldLines, setFoldLines] = useState( Number.isFinite(initial.code_block_fold_lines) ? initial.code_block_fold_lines : 5 ); const [savedFoldLines, setSavedFoldLines] = useState( Number.isFinite(initial.code_block_fold_lines) ? initial.code_block_fold_lines : 5 ); const [savedName, setSavedName] = useState(initial.site_name); 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); const [animCodeFold, setAnimCodeFold] = useState(initial.anim_code_fold !== false); const [animSmoothScroll, setAnimSmoothScroll] = useState( initial.anim_smooth_scroll !== false ); const [animChrome, setAnimChrome] = useState(initial.anim_chrome !== false); const [togglingMotion, setTogglingMotion] = useState(false); const dirty = siteName.trim() !== savedName || siteDescription.trim() !== savedDesc; const foldLinesDirty = foldLines !== savedFoldLines; const saveIdentity = async () => { if (saving) return; const name = siteName.trim(); if (!name) { toast("站点名称不能为空"); return; } setSaving(true); try { const res = await apiUpdateSiteSettings({ site_name: name, site_description: siteDescription.trim(), }); setSiteName(res.site_name); setSiteDescription(res.site_description); setSavedName(res.site_name); setSavedDesc(res.site_description); toast("站点信息已保存", "ok"); } catch (e) { toast(e instanceof Error ? e.message : "保存失败"); } finally { setSaving(false); } }; const toggleRegister = async () => { if (togglingReg) return; const next = !allowRegister; setTogglingReg(true); try { const res = await apiUpdateSiteSettings({ allow_register: next }); setAllowRegister(res.allow_register); toast(res.allow_register ? "已开放注册" : "已关闭注册", "ok"); } catch (e) { toast(e instanceof Error ? e.message : "保存失败"); } finally { setTogglingReg(false); } }; 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; const next = !codeAutoFold; setTogglingFold(true); try { const res = await apiUpdateSiteSettings({ code_block_auto_fold: next }); setCodeAutoFold(res.code_block_auto_fold); toast( res.code_block_auto_fold ? `已开启代码块自动折叠(超过 ${res.code_block_fold_lines} 行)` : "已关闭代码块自动折叠", "ok" ); } catch (e) { toast(e instanceof Error ? e.message : "保存失败"); } finally { setTogglingFold(false); } }; const saveFoldLines = async () => { if (savingFoldLines) return; const raw = Math.round(Number(foldLines)); const n = Number.isFinite(raw) ? Math.max(0, Math.min(100, raw)) : 5; setSavingFoldLines(true); try { const res = await apiUpdateSiteSettings({ code_block_fold_lines: n }); setFoldLines(res.code_block_fold_lines); setSavedFoldLines(res.code_block_fold_lines); toast( res.code_block_fold_lines === 0 ? "折叠预览已设为 0 行(无预览)" : `折叠阈值已设为超过 ${res.code_block_fold_lines} 行`, "ok" ); } catch (e) { toast(e instanceof Error ? e.message : "保存失败"); } finally { setSavingFoldLines(false); } }; const toggleMotionField = async ( key: "ui_animations" | "anim_code_fold" | "anim_smooth_scroll" | "anim_chrome", next: boolean ) => { if (togglingMotion) return; setTogglingMotion(true); try { const res = await apiUpdateSiteSettings({ [key]: next }); setUiAnimations(res.ui_animations); setAnimCodeFold(res.anim_code_fold); setAnimSmoothScroll(res.anim_smooth_scroll); setAnimChrome(res.anim_chrome); const labels: Record = { ui_animations: ["已开启界面动画", "已关闭界面动画(全站减少动态效果)"], anim_code_fold: ["已开启代码折叠动画", "已关闭代码折叠动画"], anim_smooth_scroll: ["已开启平滑滚动", "已关闭平滑滚动"], anim_chrome: ["已开启装饰入场动画", "已关闭装饰入场动画"], }; toast(next ? labels[key][0] : labels[key][1], "ok"); } catch (e) { toast(e instanceof Error ? e.message : "保存失败"); } finally { setTogglingMotion(false); } }; return (
setSiteName(e.target.value)} placeholder="姜十三论坛" />