feat: 能力插件元数据与运行时消费闭环

This commit is contained in:
que01 committed 2026-10-12 11:20:43 +08:00
1 parent 076c6124c8
commit bb0709b8cc
26 files changed
+2188 -267

No files matched your search

@@ -0,0 +1,142 @@
import { Alert, Button, Card, Input, Switch, Tag, Typography } from "antd";
import { useState } from "react";
import { useCapabilityMutation } from "../hooks/useCapabilityQueries.js";
import type { AdminCapability, CapabilityStatus } from "../types.js";
import { CapabilityStatusControl, CapabilityStatusTag } from "./CapabilityStatusControl.js";
const sourceLabels = {
brand: "品牌覆盖",
template: "模板默认",
} as const;
const disablePolicyLabels = {
allowed: "该能力允许停用,保存后进入草稿。",
disabled: "该能力为模板必需项,保存后进入草稿。",
} as const;
const updatedTimeFormatter = new Intl.DateTimeFormat("zh-CN", {
dateStyle: "short",
timeStyle: "short",
});
function readTextValue(value: unknown): string {
return typeof value === "string" ? value : "";
}
function readSwitchValue(value: unknown): boolean {
return value === true;
}
export function CapabilityCard({ capability }: { capability: AdminCapability }) {
const [status, setStatus] = useState<CapabilityStatus>(capability.status);
const [config, setConfig] = useState<Record<string, unknown>>(capability.config);
const mutation = useCapabilityMutation();
const save = async () => {
await mutation.mutateAsync({
key: capability.capabilityKey,
status,
config,
});
};
const updateConfigValue = (key: string, value: unknown) =>
setConfig((current) => ({ ...current, [key]: value }));
return (
<Card
title={
<span>
{capability.displayName ?? "未命名能力"}
<Tag style={{ marginLeft: 8 }}>{sourceLabels[capability.source]}</Tag>
<CapabilityStatusTag status={status} />
</span>
}
extra={
<Button type="primary" loading={mutation.isPending} onClick={() => void save()}>
保存草稿
</Button>
}
>
{mutation.isError ? (
<Alert
type="error"
showIcon
message="保存失败,已保留当前输入"
description={
mutation.error instanceof Error ? mutation.error.message : "请检查网络或稍后重试"
}
style={{ marginBottom: 16 }}
/>
) : null}
{capability.description ? (
<Typography.Paragraph>{capability.description}</Typography.Paragraph>
) : null}
{capability.impact ? (
<Typography.Paragraph type="secondary">影响:{capability.impact}</Typography.Paragraph>
) : null}
<div style={{ display: "grid", gap: 12 }}>
<div>
<Typography.Text strong>运行状态</Typography.Text>
<div style={{ marginTop: 8 }}>
<CapabilityStatusControl
status={status}
onChange={setStatus}
disabled={mutation.isPending}
/>
</div>
<Typography.Paragraph type="secondary" style={{ marginBottom: 0 }}>
保存后进入草稿,发布后生效。
</Typography.Paragraph>
{status === "disabled" && capability.disablePolicy ? (
<Typography.Paragraph type="warning" style={{ marginBottom: 0 }}>
{disablePolicyLabels[capability.disablePolicy]}
</Typography.Paragraph>
) : null}
</div>
{capability.configFields?.length ? (
<div>
<Typography.Text strong>能力设置</Typography.Text>
<div style={{ display: "grid", gap: 12, marginTop: 8 }}>
{capability.configFields.map((field) => (
<div key={field.key}>
<Typography.Text>{field.label}</Typography.Text>
{field.input === "switch" ? (
<div>
<Switch
checked={readSwitchValue(config[field.key])}
disabled={mutation.isPending}
onChange={(checked) => updateConfigValue(field.key, checked)}
/>
</div>
) : field.input === "textarea" ? (
<Input.TextArea
rows={3}
value={readTextValue(config[field.key])}
disabled={mutation.isPending}
onChange={(event) => updateConfigValue(field.key, event.target.value)}
/>
) : (
<Input
value={readTextValue(config[field.key])}
disabled={mutation.isPending}
onChange={(event) => updateConfigValue(field.key, event.target.value)}
/>
)}
</div>
))}
</div>
</div>
) : null}
<Typography.Text type="secondary">
{capability.source === "template"
? "当前使用模板默认设置;修改后保存会创建品牌覆盖。"
: "当前已创建品牌覆盖;修改后保存会更新草稿。"}
{capability.updatedAt
? ` 最近更新:${updatedTimeFormatter.format(new Date(capability.updatedAt))}`
: ""}
</Typography.Text>
</div>
</Card>
);
}