feat: 能力插件元数据与运行时消费闭环
This commit is contained in:
1 parent
076c6124c8
commit
bb0709b8cc
26 files changed
+2188
-267
No files matched your search
@@ -1,148 +1,34 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Alert, Button, Card, Form, Input, Select, Table, Tag, message } from "antd";
|
||||
import type { CapabilityInstallation } from "@personal-brand/api-client";
|
||||
import { Card, Col, Empty, Row, Typography } from "antd";
|
||||
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;
|
||||
import { CapabilityCard } from "./components/CapabilityCard.js";
|
||||
import { useCapabilityQuery } from "./hooks/useCapabilityQueries.js";
|
||||
|
||||
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) as Record<string, unknown>) : {},
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("能力配置已更新");
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin", "capabilities"] });
|
||||
},
|
||||
onError: (error) => message.error(error instanceof Error ? error.message : "更新失败"),
|
||||
});
|
||||
const sampleMutation = useMutation({
|
||||
mutationFn: () => createApiClient().importAdminSampleContent(),
|
||||
onSuccess: (result) => message.success(`已导入 ${result.counts.offerings} 项示例业务`),
|
||||
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 }}
|
||||
/>
|
||||
<Button
|
||||
onClick={() => sampleMutation.mutate()}
|
||||
loading={sampleMutation.isPending}
|
||||
style={{ marginBottom: 16 }}
|
||||
>
|
||||
导入门窗示例内容
|
||||
</Button>
|
||||
<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>
|
||||
);
|
||||
}
|
||||
const query = useCapabilityQuery();
|
||||
|
||||
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>
|
||||
<QueryState loading={query.isLoading} error={query.error} onRetry={() => void query.refetch()}>
|
||||
<AdminPageHeader
|
||||
title="能力管理"
|
||||
description="以经营者视角调整模板能力的开关与展示设置;保存后进入草稿,发布后生效。"
|
||||
/>
|
||||
{query.data?.length ? (
|
||||
<Row gutter={[16, 16]}>
|
||||
{query.data.map((capability) => (
|
||||
<Col key={capability.capabilityKey} xs={24} lg={12}>
|
||||
<CapabilityCard capability={capability} />
|
||||
</Col>
|
||||
))}
|
||||
</Row>
|
||||
) : (
|
||||
<Card>
|
||||
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="当前模板没有可配置能力。" />
|
||||
</Card>
|
||||
)}
|
||||
<Typography.Text type="secondary" style={{ display: "block", marginTop: 16 }}>
|
||||
技术信息仅用于排查问题,不作为运营判断依据。
|
||||
</Typography.Text>
|
||||
</QueryState>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { Segmented, Space, Switch, Tag, Typography } from "antd";
|
||||
import type { CapabilityStatus } from "../types.js";
|
||||
|
||||
const statusLabels: Record<CapabilityStatus, string> = {
|
||||
enabled: "启用",
|
||||
disabled: "停用",
|
||||
degraded: "降级",
|
||||
};
|
||||
|
||||
const statusColors: Record<CapabilityStatus, string> = {
|
||||
enabled: "green",
|
||||
disabled: "default",
|
||||
degraded: "orange",
|
||||
};
|
||||
|
||||
export function CapabilityStatusTag({ status }: { status: CapabilityStatus }) {
|
||||
return <Tag color={statusColors[status]}>{statusLabels[status]}</Tag>;
|
||||
}
|
||||
|
||||
export function CapabilityStatusControl({
|
||||
status,
|
||||
onChange,
|
||||
disabled,
|
||||
}: {
|
||||
status: CapabilityStatus;
|
||||
onChange: (status: CapabilityStatus) => void;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
const isDisabled = disabled === true;
|
||||
if (status === "degraded")
|
||||
return (
|
||||
<Segmented
|
||||
value={status}
|
||||
disabled={isDisabled}
|
||||
options={[
|
||||
{ value: "degraded", label: "降级" },
|
||||
{ value: "enabled", label: "启用" },
|
||||
{ value: "disabled", label: "停用" },
|
||||
]}
|
||||
onChange={(value) => onChange(value as CapabilityStatus)}
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<Space>
|
||||
<Switch
|
||||
checked={status === "enabled"}
|
||||
disabled={isDisabled}
|
||||
onChange={(checked) => onChange(checked ? "enabled" : "disabled")}
|
||||
/>
|
||||
<Typography.Text type={status === "enabled" ? "success" : "secondary"}>
|
||||
{statusLabels[status]}
|
||||
</Typography.Text>
|
||||
</Space>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { message } from "antd";
|
||||
import { createApiClient } from "../../../lib/api.js";
|
||||
import { readAdminCapability } from "../types.js";
|
||||
import type { CapabilityValues } from "../types.js";
|
||||
|
||||
export const capabilitiesQueryKey = ["admin", "capabilities"] as const;
|
||||
|
||||
export function useCapabilityQuery() {
|
||||
return useQuery({
|
||||
queryKey: capabilitiesQueryKey,
|
||||
queryFn: async () => {
|
||||
const response = await createApiClient().listAdminCapabilities();
|
||||
return response.map(readAdminCapability);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useCapabilityMutation() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ key, ...values }: { key: string } & CapabilityValues) =>
|
||||
createApiClient().updateAdminCapability(key, {
|
||||
...values,
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("能力已保存,进入草稿;发布后生效");
|
||||
void queryClient.invalidateQueries({ queryKey: capabilitiesQueryKey });
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { CapabilityInstallation } from "@personal-brand/api-client";
|
||||
|
||||
export type CapabilityStatus = CapabilityInstallation["status"];
|
||||
|
||||
export type CapabilitySource = NonNullable<CapabilityInstallation["source"]>;
|
||||
|
||||
export interface CapabilityConfigField {
|
||||
key: string;
|
||||
label: string;
|
||||
input: string;
|
||||
required: boolean;
|
||||
}
|
||||
|
||||
export interface AdminCapability {
|
||||
capabilityKey: string;
|
||||
capabilityVersion: string;
|
||||
status: CapabilityStatus;
|
||||
config: Record<string, unknown>;
|
||||
source: CapabilitySource;
|
||||
updatedAt?: string | null;
|
||||
displayName?: string | undefined;
|
||||
description?: string | undefined;
|
||||
impact?: string | undefined;
|
||||
optional?: boolean | undefined;
|
||||
disablePolicy?: "allowed" | "disabled" | undefined;
|
||||
configFields?: CapabilityConfigField[] | undefined;
|
||||
}
|
||||
|
||||
export interface CapabilityValues {
|
||||
status: CapabilityStatus;
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function isCapabilitySource(value: unknown): value is CapabilitySource {
|
||||
return value === "brand" || value === "template";
|
||||
}
|
||||
|
||||
export function readAdminCapability(value: CapabilityInstallation): AdminCapability {
|
||||
const { config, source, ...rest } = value;
|
||||
return {
|
||||
...rest,
|
||||
config: config ?? {},
|
||||
source: isCapabilitySource(source) ? source : "template",
|
||||
displayName: readOptionalString(value.displayName),
|
||||
description: readOptionalString(value.description),
|
||||
impact: readOptionalString(value.impact),
|
||||
optional: typeof value.optional === "boolean" ? value.optional : undefined,
|
||||
disablePolicy:
|
||||
value.disablePolicy === "allowed" || value.disablePolicy === "disabled"
|
||||
? value.disablePolicy
|
||||
: undefined,
|
||||
configFields: Array.isArray(value.configFields)
|
||||
? value.configFields.filter(isCapabilityConfigField)
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function readOptionalString(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() ? value : undefined;
|
||||
}
|
||||
|
||||
function isCapabilityConfigField(value: {
|
||||
key: string;
|
||||
label: string;
|
||||
input: string;
|
||||
required: boolean;
|
||||
}): value is CapabilityConfigField {
|
||||
const field = value;
|
||||
return (
|
||||
typeof field.key === "string" &&
|
||||
field.key.length > 0 &&
|
||||
typeof field.label === "string" &&
|
||||
field.label.length > 0 &&
|
||||
(field.input === "text" || field.input === "textarea" || field.input === "switch")
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user