293 lines
9.4 KiB
React
293 lines
9.4 KiB
React
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
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,
|
|
metricPreview,
|
|
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 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;
|
|
};
|
|
}, []);
|
|
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);
|
|
|
|
// Home team card on the official site: "50+ · 研发占比60%" (ratio is edited in the about tab).
|
|
const teamPreview = () => {
|
|
const team = metricPreview('teamMembers', values.teamMembers);
|
|
if (!team) return '';
|
|
const ratio = metricPreview('researchRatio', values.researchRatio);
|
|
return t('{{team}} · 研发占比 {{ratio}}', {
|
|
team,
|
|
ratio: ratio || t('未配置'),
|
|
});
|
|
};
|
|
|
|
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 (
|
|
<div className='website-settings-page' aria-busy={loading || saving}>
|
|
<Spin
|
|
spinning={loading || saving}
|
|
size='large'
|
|
style={{
|
|
minHeight: 200,
|
|
width: '100%',
|
|
flex: 1,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
}}
|
|
childStyle={{ flex: 1, display: 'flex', flexDirection: 'column' }}
|
|
data-testid='website-settings-loading'
|
|
>
|
|
<Card
|
|
className={`website-settings-card${loadError ? ' website-settings-card--error' : ''}`}
|
|
>
|
|
{loadError ? (
|
|
<Banner
|
|
type='danger'
|
|
closeIcon={null}
|
|
description={t(loadError)}
|
|
className='website-settings-load-error'
|
|
style={{ marginBlock: '0 8px' }}
|
|
/>
|
|
) : null}
|
|
<Tabs type='line' activeKey={activeTab} onChange={setActiveTab}>
|
|
{TABS.map((item) => (
|
|
<Tabs.TabPane
|
|
key={item.key}
|
|
itemKey={item.key}
|
|
tab={t(item.label)}
|
|
/>
|
|
))}
|
|
</Tabs>
|
|
<Form
|
|
onSubmit={save}
|
|
className={activeTab === 'insights' ? 'py-5' : 'max-w-4xl py-5'}
|
|
autoComplete='off'
|
|
>
|
|
<Typography.Paragraph
|
|
type='tertiary'
|
|
className='website-settings-tab-description'
|
|
>
|
|
{t(tab.description)}
|
|
</Typography.Paragraph>
|
|
{activeTab === 'insights' ? (
|
|
<InsightsPanel
|
|
key={resetVersion}
|
|
value={values.insights}
|
|
onChange={(value) => change('insights', value)}
|
|
disabled={disabled}
|
|
error={errors.insights}
|
|
/>
|
|
) : (
|
|
<div className='grid grid-cols-1 gap-x-7 md:grid-cols-2'>
|
|
{tab.fields.map((name) => (
|
|
<div
|
|
key={name}
|
|
className={name === 'address' ? 'md:col-span-2' : ''}
|
|
>
|
|
<WebsiteField
|
|
name={name}
|
|
preview={
|
|
activeTab === 'home' && name === 'teamMembers'
|
|
? teamPreview()
|
|
: undefined
|
|
}
|
|
value={values[name]}
|
|
error={errors[name]}
|
|
disabled={disabled}
|
|
onChange={(value) => change(name, value)}
|
|
onBlur={() =>
|
|
setErrors((previous) => ({
|
|
...previous,
|
|
[name]: validateField(name, values[name]),
|
|
}))
|
|
}
|
|
>
|
|
{name === 'wechatQrCode' && (
|
|
<QrCodeUpload
|
|
value={values.wechatQrCode}
|
|
onChange={(url) => change(name, url)}
|
|
busy={disabled}
|
|
onBusyChange={setUploading}
|
|
resetVersion={resetVersion}
|
|
invalid={Boolean(errors[name])}
|
|
/>
|
|
)}
|
|
</WebsiteField>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
<Banner
|
|
type='info'
|
|
closeIcon={null}
|
|
className='website-settings-notice !rounded-lg'
|
|
style={{
|
|
marginTop: 10,
|
|
marginBottom: 2,
|
|
marginBlock:
|
|
activeTab === 'insights'
|
|
? '0 var(--website-insights-footer-gap)'
|
|
: undefined,
|
|
paddingTop: 2,
|
|
paddingBottom: 2,
|
|
}}
|
|
description={t('四个页签共用一份配置,保存时将统一校验并提交。')}
|
|
/>
|
|
<div
|
|
className='flex justify-center gap-2'
|
|
style={{ marginTop: activeTab === 'insights' ? 0 : 18 }}
|
|
>
|
|
<Button onClick={clear} disabled={disabled}>
|
|
{t('清空')}
|
|
</Button>
|
|
<Button
|
|
theme='solid'
|
|
htmlType='submit'
|
|
loading={saving}
|
|
disabled={disabled || uploading}
|
|
>
|
|
{t('保存')}
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</Card>
|
|
</Spin>
|
|
</div>
|
|
);
|
|
}
|