/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useEffect, useRef, useState } from 'react'; import { Banner, Button, Card, Form, Spin, Tabs, Typography, } from '@/components/ui'; import { useTranslation } from 'react-i18next'; import { showError, showSuccess, showWarning } from '../../helpers'; import { TABS, emptySettings, settingsFromConfig, serializeSettings, validateField, validateSettings, } from './model'; import { getWebsiteSettings, saveWebsiteSettings } from './settingsApi'; import WebsiteField from './WebsiteField'; import QrCodeUpload from './QrCodeUpload'; import InsightsPanel from './InsightsPanel'; import './WebsiteSettings.css'; export default function WebsiteSettings() { const { t } = useTranslation(); const [activeTab, setActiveTab] = useState('home'); const [values, setValues] = useState(emptySettings); const [errors, setErrors] = useState({}); const [saving, setSaving] = useState(false); const [uploading, setUploading] = useState(false); const [resetVersion, setResetVersion] = useState(0); const [dirty, setDirty] = useState(false); const [loading, setLoading] = useState(true); const [loadError, setLoadError] = useState(''); const [loadVersion, setLoadVersion] = useState(0); const originalConfig = useRef({}); const savePending = useRef(false); useEffect(() => { let active = true; setLoading(true); setLoadError(''); getWebsiteSettings() .then((config) => { if (!active) return; originalConfig.current = config ?? {}; setValues(settingsFromConfig(config)); setDirty(false); setErrors({}); }) .catch((error) => { if (active) setLoadError(error.message || '官网配置读取失败,请稍后重试'); }) .finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, [loadVersion]); const disabled = saving || loading || Boolean(loadError); useEffect(() => { if (!dirty) return; const warn = (event) => { event.preventDefault(); event.returnValue = ''; }; window.addEventListener('beforeunload', warn); return () => window.removeEventListener('beforeunload', warn); }, [dirty]); const tab = TABS.find((item) => item.key === activeTab); const change = (name, value) => { setDirty(true); setValues((previous) => ({ ...previous, [name]: value })); setErrors((previous) => previous[name] ? { ...previous, [name]: validateField(name, value) } : previous, ); }; const clear = () => { setDirty(true); setResetVersion((version) => version + 1); setValues(emptySettings()); setErrors({}); setUploading(false); }; const save = async () => { if (savePending.current || disabled || uploading) return; const nextErrors = validateSettings(values); setErrors(nextErrors); if (Object.keys(nextErrors).length) { // Keep field errors for every tab; report each hidden invalid tab only once. for (const item of TABS) { if ( item.key !== activeTab && item.fields.some((name) => nextErrors[name]) ) { showWarning(t(`请完善${item.label}的配置`)); } } return; } savePending.current = true; setSaving(true); try { const configurationJson = serializeSettings( values, originalConfig.current, ); const result = await saveWebsiteSettings(configurationJson); originalConfig.current = result.config ?? JSON.parse(configurationJson); setValues(settingsFromConfig(originalConfig.current)); setDirty(false); showSuccess(t('官网设置已保存')); } catch (error) { showError(t(error.message || '保存失败,请稍后重试')); } finally { setSaving(false); savePending.current = false; } }; return (
{t('官网设置')} {t('配置官网展示内容与商务联系信息')} {loadError ? ( ) : null} {TABS.map((item) => ( ))}
{t(tab.description)} {activeTab === 'insights' ? ( change('insights', value)} disabled={disabled} error={errors.insights} /> ) : (
{tab.fields.map((name) => (
change(name, value)} onBlur={() => setErrors((previous) => ({ ...previous, [name]: validateField(name, values[name]), })) } > {name === 'wechatQrCode' && ( change(name, url)} busy={disabled} onBusyChange={setUploading} resetVersion={resetVersion} invalid={Boolean(errors[name])} /> )} {activeTab === 'home' && name === 'teamMembers' && ( {t('研发人员占比')}: {values.researchRatio || t('未配置')} )}
))}
)}
); }