CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Taro builds (acheng-campus-food) (push) Has been cancelled
CI / Taro builds (atelier-solopreneur) (push) Has been cancelled
CI / Taro builds (linjing-editorial) (push) Has been cancelled
CI / Browser regression (push) Has been cancelled
156 lines
5.7 KiB
TypeScript
156 lines
5.7 KiB
TypeScript
import { Button, Card, Input, Space, Switch, Typography } from "antd";
|
|
import { useState } from "react";
|
|
import { ErrorNote } from "../../../components/QueryState.js";
|
|
import { StatusTag } from "../../../components/StatusTag.js";
|
|
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;
|
|
}
|
|
|
|
function sameConfig(current: Record<string, unknown>, baseline: Record<string, unknown>): boolean {
|
|
const currentKeys = Object.keys(current ?? {}).sort();
|
|
const baselineKeys = Object.keys(baseline ?? {}).sort();
|
|
if (currentKeys.length !== baselineKeys.length) return false;
|
|
return currentKeys.every(
|
|
(key, index) =>
|
|
key === baselineKeys[index] && JSON.stringify(current[key]) === JSON.stringify(baseline[key]),
|
|
);
|
|
}
|
|
|
|
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 dirty = status !== capability.status || !sameConfig(config, capability.config ?? {});
|
|
|
|
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 className="admin-row-line">
|
|
{capability.displayName ?? "未命名能力"}
|
|
<CapabilityStatusTag status={status} />
|
|
<StatusTag tone="neutral" label={sourceLabels[capability.source]} />
|
|
</span>
|
|
}
|
|
extra={
|
|
<Space>
|
|
{dirty ? <StatusTag tone="warning" label="有未保存修改" /> : null}
|
|
<Button
|
|
type={dirty ? "primary" : "default"}
|
|
disabled={!dirty}
|
|
loading={mutation.isPending}
|
|
onClick={() => void save()}
|
|
>
|
|
保存草稿
|
|
</Button>
|
|
</Space>
|
|
}
|
|
>
|
|
{mutation.isError ? (
|
|
<div className="admin-offset-bottom">
|
|
<ErrorNote error={mutation.error} />
|
|
</div>
|
|
) : null}
|
|
{capability.description ? (
|
|
<p className="admin-section-description">{capability.description}</p>
|
|
) : null}
|
|
{capability.impact ? (
|
|
<p className="admin-section-description">影响:{capability.impact}</p>
|
|
) : null}
|
|
<div className="admin-field-stack">
|
|
<div>
|
|
<span className="admin-field-label">运行状态</span>
|
|
<div className="admin-offset-top-sm">
|
|
<CapabilityStatusControl
|
|
status={status}
|
|
onChange={setStatus}
|
|
disabled={mutation.isPending}
|
|
/>
|
|
</div>
|
|
<p className="admin-section-description admin-flush">保存后进入草稿,发布后生效。</p>
|
|
{status === "disabled" && capability.disablePolicy ? (
|
|
<p className="admin-section-description admin-flush admin-text-warning">
|
|
{disablePolicyLabels[capability.disablePolicy]}
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
{capability.configFields?.length ? (
|
|
<div>
|
|
<span className="admin-field-label">能力设置</span>
|
|
<div className="admin-field-stack admin-offset-top-sm">
|
|
{capability.configFields.map((field) => (
|
|
<div key={field.key}>
|
|
<span className="admin-field-label">{field.label}</span>
|
|
{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>
|
|
);
|
|
}
|