371 lines
11 KiB
React
371 lines
11 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
|
||
*/
|
||
|
||
// 系统设置 →「WeyAi」→「版本说明」:每个 WeyAi 桌面版本的更新概要(Markdown)。
|
||
// 发布后,WeyAi 的「关于」页显示当前版本和历史版本的说明,发现新版本时的更新卡片
|
||
// 也会带上这里写的内容。客户端读公开接口 GET /api/weyai/desktop/release-notes,
|
||
// 未登录也能看到;草稿只在这里可见。root 和被下放了「WeyAi」页签的管理员都能用。
|
||
|
||
import React, { useCallback, useEffect, useState } from 'react';
|
||
import {
|
||
Banner,
|
||
Button,
|
||
DatePicker,
|
||
Empty,
|
||
Input,
|
||
Modal,
|
||
Popconfirm,
|
||
Space,
|
||
Switch,
|
||
Table,
|
||
TabPane,
|
||
Tabs,
|
||
Tag,
|
||
TextArea,
|
||
Typography,
|
||
} from '@/components/ui';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { API, showError, showSuccess } from '../../../helpers';
|
||
import MarkdownRenderer from '../../../components/common/markdown/MarkdownRenderer';
|
||
|
||
const { Text } = Typography;
|
||
|
||
const BASE = '/api/admin/weyai/release-notes';
|
||
|
||
const EMPTY_DRAFT = {
|
||
id: 0,
|
||
version: '',
|
||
title: '',
|
||
content: '',
|
||
prerelease: false,
|
||
released_at: 0,
|
||
};
|
||
|
||
const contentPlaceholder = (t) => `### ${t('新功能')}
|
||
-
|
||
|
||
### ${t('改进')}
|
||
-
|
||
|
||
### ${t('修复')}
|
||
- `;
|
||
|
||
const formatDate = (sec) =>
|
||
sec ? new Date(sec * 1000).toLocaleDateString() : '';
|
||
|
||
const SettingsWeyAiReleaseNotes = () => {
|
||
const { t } = useTranslation();
|
||
const [notes, setNotes] = useState([]);
|
||
const [loading, setLoading] = useState(false);
|
||
const [busyId, setBusyId] = useState(0);
|
||
const [draft, setDraft] = useState(null);
|
||
const [saving, setSaving] = useState(false);
|
||
|
||
const load = useCallback(async () => {
|
||
const res = await API.get(BASE, { skipErrorHandler: true });
|
||
if (!res?.data?.success) {
|
||
throw new Error(res?.data?.message || t('加载失败,请重试'));
|
||
}
|
||
setNotes(res.data.data || []);
|
||
}, [t]);
|
||
|
||
useEffect(() => {
|
||
setLoading(true);
|
||
load()
|
||
.catch((e) => showError(e.message))
|
||
.finally(() => setLoading(false));
|
||
}, [load]);
|
||
|
||
const call = async (id, request, successText) => {
|
||
setBusyId(id);
|
||
try {
|
||
const res = await request();
|
||
if (!res?.data?.success) {
|
||
showError(res?.data?.message || t('操作失败,请重试'));
|
||
return;
|
||
}
|
||
showSuccess(successText);
|
||
await load();
|
||
} catch (err) {
|
||
showError(err?.response?.data?.message || t('操作失败,请重试'));
|
||
} finally {
|
||
setBusyId(0);
|
||
}
|
||
};
|
||
|
||
const setStatus = (note, status) =>
|
||
call(
|
||
note.id,
|
||
() =>
|
||
API.post(
|
||
`${BASE}/${note.id}/status`,
|
||
{ status },
|
||
{ skipErrorHandler: true },
|
||
),
|
||
status === 'published' ? t('已发布') : t('已撤回为草稿'),
|
||
);
|
||
|
||
const remove = (note) =>
|
||
call(
|
||
note.id,
|
||
() => API.delete(`${BASE}/${note.id}`, { skipErrorHandler: true }),
|
||
t('已删除'),
|
||
);
|
||
|
||
const save = async () => {
|
||
if (!draft) return;
|
||
setSaving(true);
|
||
try {
|
||
const body = {
|
||
version: draft.version,
|
||
title: draft.title,
|
||
content: draft.content,
|
||
prerelease: draft.prerelease,
|
||
released_at: draft.released_at,
|
||
};
|
||
const res = draft.id
|
||
? await API.put(`${BASE}/${draft.id}`, body, { skipErrorHandler: true })
|
||
: await API.post(BASE, body, { skipErrorHandler: true });
|
||
if (!res?.data?.success) {
|
||
showError(res?.data?.message || t('保存失败,请重试'));
|
||
return;
|
||
}
|
||
showSuccess(
|
||
draft.id ? t('保存成功') : t('已保存为草稿,确认无误后点「发布」'),
|
||
);
|
||
setDraft(null);
|
||
await load();
|
||
} catch (err) {
|
||
showError(err?.response?.data?.message || t('保存失败,请重试'));
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
};
|
||
|
||
const columns = [
|
||
{
|
||
title: t('版本'),
|
||
width: 160,
|
||
render: (_, note) => (
|
||
<Space>
|
||
<Text strong>v{note.version}</Text>
|
||
{note.prerelease && (
|
||
<Tag size='small' color='orange'>
|
||
{t('预发布')}
|
||
</Tag>
|
||
)}
|
||
</Space>
|
||
),
|
||
},
|
||
{
|
||
title: t('标题'),
|
||
render: (_, note) => note.title || <Text type='tertiary'>—</Text>,
|
||
},
|
||
{
|
||
title: t('状态'),
|
||
width: 100,
|
||
render: (_, note) =>
|
||
note.status === 'published' ? (
|
||
<Tag color='green'>{t('已发布')}</Tag>
|
||
) : (
|
||
<Tag color='grey'>{t('草稿')}</Tag>
|
||
),
|
||
},
|
||
{
|
||
title: t('发布日期'),
|
||
width: 130,
|
||
render: (_, note) =>
|
||
formatDate(note.released_at) || <Text type='tertiary'>—</Text>,
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
width: 240,
|
||
render: (_, note) => (
|
||
<Space>
|
||
<Button
|
||
onClick={() => setDraft({ ...note })}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('编辑')}
|
||
</Button>
|
||
{note.status === 'published' ? (
|
||
<Button
|
||
loading={busyId === note.id}
|
||
onClick={() => setStatus(note, 'draft')}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('撤回')}
|
||
</Button>
|
||
) : (
|
||
<Button
|
||
loading={busyId === note.id}
|
||
onClick={() => setStatus(note, 'published')}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('发布')}
|
||
</Button>
|
||
)}
|
||
<Popconfirm
|
||
title={t('确定删除这条版本说明吗?')}
|
||
content={t('删除后客户端不再显示这个版本的说明')}
|
||
onConfirm={() => remove(note)}
|
||
>
|
||
<Button
|
||
disabled={busyId === note.id}
|
||
type='link'
|
||
size='small'
|
||
danger
|
||
>
|
||
{t('删除')}
|
||
</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div>
|
||
<Banner
|
||
type='info'
|
||
closeIcon={null}
|
||
className='mb-4'
|
||
description={t(
|
||
'每个版本写一条更新概要,支持 Markdown。发布后 WeyAi「关于」页会显示当前版本和历史版本的说明,发现新版本时的更新提示也会带上对应内容;草稿不会下发。',
|
||
)}
|
||
/>
|
||
<div className='mb-3'>
|
||
<Button theme='solid' onClick={() => setDraft({ ...EMPTY_DRAFT })}>
|
||
{t('新建版本说明')}
|
||
</Button>
|
||
</div>
|
||
<Table
|
||
rowKey='id'
|
||
loading={loading}
|
||
columns={columns}
|
||
dataSource={notes}
|
||
pagination={false}
|
||
empty={<Empty description={t('还没有版本说明')} />}
|
||
/>
|
||
|
||
<Modal
|
||
title={draft?.id ? t('编辑版本说明') : t('新建版本说明')}
|
||
visible={Boolean(draft)}
|
||
width={760}
|
||
onCancel={() => setDraft(null)}
|
||
onOk={save}
|
||
okText={t('保存')}
|
||
confirmLoading={saving}
|
||
maskClosable={false}
|
||
>
|
||
{draft && (
|
||
<div className='flex flex-col gap-3'>
|
||
<div className='flex gap-3 flex-wrap items-center'>
|
||
<Input
|
||
style={{ width: 200 }}
|
||
prefix='v'
|
||
placeholder='2.3.0'
|
||
value={draft.version}
|
||
onChange={(version) =>
|
||
setDraft({
|
||
...draft,
|
||
version,
|
||
// 1.2.3-beta.1 → 1.2.3: the pre-release mark came from the suffix, drop it
|
||
prerelease:
|
||
draft.version.includes('-') && !version.includes('-')
|
||
? false
|
||
: draft.prerelease,
|
||
})
|
||
}
|
||
/>
|
||
<DatePicker
|
||
type='date'
|
||
placeholder={t('发布日期(留空则在发布时自动填写)')}
|
||
style={{ width: 280 }}
|
||
// A published note keeps its date; clearing it would be ignored on save.
|
||
showClear={draft.status !== 'published'}
|
||
value={
|
||
draft.released_at
|
||
? new Date(draft.released_at * 1000)
|
||
: undefined
|
||
}
|
||
onChange={(date) =>
|
||
setDraft({
|
||
...draft,
|
||
// Local noon: the same calendar day in every time zone within ±12 h.
|
||
released_at: date
|
||
? Math.floor(new Date(date).setHours(12, 0, 0, 0) / 1000)
|
||
: 0,
|
||
})
|
||
}
|
||
/>
|
||
<Space>
|
||
<Switch
|
||
checked={draft.prerelease || draft.version.includes('-')}
|
||
disabled={draft.version.includes('-')}
|
||
onChange={(prerelease) => setDraft({ ...draft, prerelease })}
|
||
/>
|
||
<Text>{t('预发布版本')}</Text>
|
||
</Space>
|
||
</div>
|
||
<Input
|
||
placeholder={t('标题(可选),例如:对话显示每轮 token 用量')}
|
||
maxLength={100}
|
||
value={draft.title}
|
||
onChange={(title) => setDraft({ ...draft, title })}
|
||
/>
|
||
<Tabs type='button' size='small'>
|
||
<TabPane tab={t('编辑')} itemKey='edit'>
|
||
<TextArea
|
||
className='mt-2'
|
||
autosize={{ minRows: 12, maxRows: 24 }}
|
||
maxCount={20000}
|
||
placeholder={contentPlaceholder(t)}
|
||
value={draft.content}
|
||
onChange={(content) => setDraft({ ...draft, content })}
|
||
/>
|
||
</TabPane>
|
||
<TabPane tab={t('预览')} itemKey='preview'>
|
||
<div
|
||
className='mt-2 p-3 rounded'
|
||
style={{
|
||
minHeight: 200,
|
||
border: '1px solid var(--ui-color-border)',
|
||
}}
|
||
>
|
||
{draft.content.trim() ? (
|
||
<MarkdownRenderer content={draft.content} />
|
||
) : (
|
||
<Text type='tertiary'>{t('还没有内容')}</Text>
|
||
)}
|
||
</div>
|
||
</TabPane>
|
||
</Tabs>
|
||
</div>
|
||
)}
|
||
</Modal>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default SettingsWeyAiReleaseNotes;
|