137 lines
4.5 KiB
TypeScript
137 lines
4.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { Alert, Button, Card, Form, Input, Select, Space, Table, Tag, message } from "antd";
|
|
import type { CapabilityInstallation } from "@personal-brand/api-client";
|
|
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
|
import { QueryState } from "../../components/QueryState.js";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
|
|
const statusLabels = { enabled: "已启用", disabled: "已停用", degraded: "降级" } as const;
|
|
|
|
export function CapabilitiesPage() {
|
|
const queryClient = useQueryClient();
|
|
const query = useQuery({
|
|
queryKey: ["admin", "capabilities"],
|
|
queryFn: () => createApiClient().listAdminCapabilities(),
|
|
});
|
|
const mutation = useMutation({
|
|
mutationFn: ({
|
|
key,
|
|
values,
|
|
}: {
|
|
key: string;
|
|
values: {
|
|
capabilityVersion: string;
|
|
status: CapabilityInstallation["status"];
|
|
configText?: string;
|
|
};
|
|
}) =>
|
|
createApiClient().updateAdminCapability(key, {
|
|
capabilityVersion: values.capabilityVersion,
|
|
status: values.status,
|
|
config: values.configText ? JSON.parse(values.configText) : {},
|
|
}),
|
|
onSuccess: () => {
|
|
message.success("能力配置已更新");
|
|
queryClient.invalidateQueries({ queryKey: ["admin", "capabilities"] });
|
|
},
|
|
onError: (error) => message.error(error instanceof Error ? error.message : "更新失败"),
|
|
});
|
|
if (query.isLoading) return <QueryState loading />;
|
|
if (query.isError) return <QueryState error={query.error} />;
|
|
const items = query.data ?? [];
|
|
return (
|
|
<div>
|
|
<AdminPageHeader title="能力插件" description="管理当前模板声明的业务能力及其运行状态。" />
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
message="能力状态会影响公开 Runtime Workspace;停用不会删除历史数据。"
|
|
style={{ marginBottom: 16 }}
|
|
/>
|
|
<Table
|
|
rowKey="capabilityKey"
|
|
dataSource={items}
|
|
locale={{ emptyText: "当前还没有品牌级能力覆盖,系统将使用模板默认能力。" }}
|
|
columns={[
|
|
{ title: "能力", dataIndex: "capabilityKey" },
|
|
{
|
|
title: "来源",
|
|
dataIndex: "source",
|
|
render: (value: CapabilityInstallation["source"]) => (
|
|
<Tag>{value === "brand" ? "品牌覆盖" : "模板默认"}</Tag>
|
|
),
|
|
},
|
|
{ title: "版本", dataIndex: "capabilityVersion" },
|
|
{
|
|
title: "状态",
|
|
dataIndex: "status",
|
|
render: (value: CapabilityInstallation["status"]) => (
|
|
<Tag
|
|
color={value === "enabled" ? "green" : value === "degraded" ? "orange" : "default"}
|
|
>
|
|
{statusLabels[value]}
|
|
</Tag>
|
|
),
|
|
},
|
|
{
|
|
title: "操作",
|
|
render: (_: unknown, record: CapabilityInstallation) => (
|
|
<CapabilityEditor
|
|
record={record}
|
|
loading={mutation.isPending}
|
|
onSubmit={(values) => mutation.mutate({ key: record.capabilityKey, values })}
|
|
/>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function CapabilityEditor({
|
|
record,
|
|
loading,
|
|
onSubmit,
|
|
}: {
|
|
record: CapabilityInstallation;
|
|
loading: boolean;
|
|
onSubmit: (values: {
|
|
capabilityVersion: string;
|
|
status: CapabilityInstallation["status"];
|
|
configText?: string;
|
|
}) => void;
|
|
}) {
|
|
const [form] = Form.useForm();
|
|
return (
|
|
<Card size="small" style={{ maxWidth: 420 }}>
|
|
<Form
|
|
form={form}
|
|
layout="inline"
|
|
initialValues={{
|
|
capabilityVersion: record.capabilityVersion,
|
|
status: record.status,
|
|
configText: record.config ? JSON.stringify(record.config) : "",
|
|
}}
|
|
onFinish={onSubmit}
|
|
>
|
|
<Form.Item name="capabilityVersion" rules={[{ required: true, message: "请输入版本" }]}>
|
|
<Input placeholder="版本" style={{ width: 100 }} />
|
|
</Form.Item>
|
|
<Form.Item name="status">
|
|
<Select
|
|
options={Object.entries(statusLabels).map(([value, label]) => ({ value, label }))}
|
|
style={{ width: 100 }}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item name="configText">
|
|
<Input placeholder="JSON 配置(可选)" style={{ width: 180 }} />
|
|
</Form.Item>
|
|
<Button type="primary" htmlType="submit" loading={loading}>
|
|
保存
|
|
</Button>
|
|
</Form>
|
|
</Card>
|
|
);
|
|
}
|