import { DeleteOutlined, PlusOutlined, ApiOutlined } from "@ant-design/icons" import { Button, Form, Input, message, Modal, Popconfirm, Select, Space, Table, Tag } from "antd" import { useEffect, useState } from "react" import { CMS_TYPES, checkCmsAuth, getCmsAccounts, removeCmsAccount, saveCmsAccount, type CmsAccount } from "~sync/extension/engine" export interface CmsManagerProps { /** 账号列表变化时通知父组件 */ onChange?: (accounts: CmsAccount[]) => void } interface CmsFormValues { type: CmsAccount["type"] name: string url: string username: string password: string } /** * 自建站(WordPress / Typecho / MetaWeblog)账号管理 */ export default function CmsManager({ onChange }: CmsManagerProps) { const [accounts, setAccounts] = useState([]) const [status, setStatus] = useState< Record >({}) const [open, setOpen] = useState(false) const [saving, setSaving] = useState(false) const [form] = Form.useForm() async function refresh() { const list = await getCmsAccounts() setAccounts(list) onChange?.(list) } useEffect(() => { refresh() }, []) async function handleAdd() { try { const values = await form.validateFields() setSaving(true) const account = await saveCmsAccount( { type: values.type, name: values.name || `${values.type} 站点`, url: values.url.trim(), username: values.username }, values.password ) message.success("账号已保存") setOpen(false) form.resetFields() await refresh() await check(account) } catch (error) { if ((error as { errorFields?: unknown }).errorFields) return message.error((error as Error).message) } finally { setSaving(false) } } async function check(account: CmsAccount) { setStatus((prev) => ({ ...prev, [account.id]: { checking: true } })) const result = await checkCmsAuth(account) setStatus((prev) => ({ ...prev, [account.id]: { checking: false, ok: result.isAuthenticated, error: result.error } })) } async function handleRemove(id: string) { await removeCmsAccount(id) message.success("账号已删除") await refresh() } return (
支持 WordPress、Typecho 及兼容 MetaWeblog 的博客,同步为草稿 {accounts.length > 0 && ( size="small" rowKey="id" pagination={false} dataSource={accounts} columns={[ { title: "名称", dataIndex: "name", width: 160 }, { title: "类型", dataIndex: "type", width: 130, render: (type: string) => CMS_TYPES.find((t) => t.value === type)?.label || type }, { title: "站点", dataIndex: "url", ellipsis: true }, { title: "用户名", dataIndex: "username", width: 120 }, { title: "状态", width: 120, render: (_, account) => { const item = status[account.id] if (!item || item.checking) return 未检测 return item.ok ? ( 连接正常 ) : ( 连接失败 ) } }, { title: "操作", width: 140, render: (_, account) => ( handleRemove(account.id)}>
) }