'use client'; import { useState, useEffect } from 'react'; import { Save, RefreshCw, Loader2, ChevronDown, ChevronUp } from 'lucide-react'; interface ConfigItem { id: string; key: string; value: Record; category: 'feature' | 'style' | 'seo' | 'general'; description: string | null; updatedAt: string; } const categoryLabels = { feature: '功能配置', style: '样式配置', seo: 'SEO 配置', general: '常规配置' }; const categoryColors = { feature: 'bg-blue-100 text-blue-800', style: 'bg-purple-100 text-purple-800', seo: 'bg-green-100 text-green-800', general: 'bg-gray-100 text-gray-800' }; export default function SettingsPage() { const [configs, setConfigs] = useState([]); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(null); const [expandedCategories, setExpandedCategories] = useState>(new Set(['feature', 'seo'])); const [editedValues, setEditedValues] = useState>>({}); useEffect(() => { fetchConfigs(); }, []); const fetchConfigs = async () => { try { setLoading(true); const res = await fetch('/api/admin/config'); const data = await res.json(); if (res.ok) { setConfigs(data.configs || []); } } catch (error) { console.error('获取配置失败:', error); } finally { setLoading(false); } }; const handleSave = async (configId: string) => { const editedValue = editedValues[configId]; if (!editedValue) return; try { setSaving(configId); const res = await fetch('/api/admin/config', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: configId, value: editedValue }) }); if (res.ok) { setEditedValues(prev => { const updated = { ...prev }; delete updated[configId]; return updated; }); await fetchConfigs(); } } catch (error) { console.error('保存配置失败:', error); } finally { setSaving(null); } }; const toggleCategory = (category: string) => { setExpandedCategories(prev => { const updated = new Set(prev); if (updated.has(category)) { updated.delete(category); } else { updated.add(category); } return updated; }); }; const handleValueChange = (configId: string, field: string, value: any) => { setEditedValues(prev => ({ ...prev, [configId]: { ...prev[configId], [field]: value } })); }; const getConfigValue = (config: ConfigItem, field: string) => { if (editedValues[config.id]?.[field] !== undefined) { return editedValues[config.id]![field]; } return config.value[field]; }; const hasChanges = (configId: string) => { return editedValues[configId] && Object.keys(editedValues[configId]).length > 0; }; const groupedConfigs = configs.reduce((acc, config) => { if (!acc[config.category]) { acc[config.category] = []; } acc[config.category]!.push(config); return acc; }, {} as Record); if (loading) { return (
); } return (

配置中心

管理网站功能和样式配置

{Object.entries(groupedConfigs).map(([category, categoryConfigs]) => (
{expandedCategories.has(category) && (
{categoryConfigs.map(config => (

{config.key}

{config.description && (

{config.description}

)}
{hasChanges(config.id) && ( )}
{Object.entries(config.value).map(([field, value]) => { const currentValue = getConfigValue(config, field); return (
{typeof value === 'boolean' ? ( ) : typeof value === 'string' ? ( handleValueChange(config.id, field, e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-[#C41E3A] focus:border-transparent" /> ) : typeof value === 'number' ? ( handleValueChange(config.id, field, Number(e.target.value))} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-[#C41E3A] focus:border-transparent" /> ) : Array.isArray(value) ? (