Files
persionalBrand/apps/admin/src/features/trust-content/TrustContentPage.tsx
T

102 lines
3.8 KiB
TypeScript

import { Button, Divider, Form, Typography } from "antd";
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
import { QueryState } from "../../components/QueryState.js";
import { TrustContentEditorModal } from "./components/TrustContentEditorModal.js";
import { TrustContentTable } from "./components/TrustContentTable.js";
import { TrustRequirementsList } from "./components/TrustRequirementsList.js";
import type { TrustTypeOption } from "./domain/trust-content-domain.js";
import {
createTrustPageLabelResolver,
groupTrustContentByType,
selectTrustTypeOptions,
} from "./domain/trust-content-domain.js";
import { useTrustContentEditor } from "./hooks/useTrustContentEditor.js";
import { useTrustContentQueries } from "./hooks/useTrustContentQueries.js";
export function TrustContentPage() {
const { query, workspace } = useTrustContentQueries();
const availableTypes = selectTrustTypeOptions(workspace.data?.templateContent?.trustContentTypes);
const pageLabel = createTrustPageLabelResolver(workspace.data?.templateContent?.navigation);
const contentByType = groupTrustContentByType(query.data ?? []);
const firstMissingRequiredType =
availableTypes.find((item) => item.required && !contentByType.has(item.type))?.type ??
availableTypes[0]?.type ??
"credential";
const {
begin,
closeEditor,
editing,
form: editorForm,
open,
remove,
save,
} = useTrustContentEditor({
availableTypes,
contentByType,
firstMissingRequiredType,
});
const selectedType = Form.useWatch("type", editorForm);
const selectedTypeMeta: TrustTypeOption | undefined = availableTypes.find(
(item) => item.type === selectedType,
);
const trustPageLabels = [
...new Set(availableTypes.flatMap((item) => item.pages ?? []).map(pageLabel)),
];
const trustPlacement = trustPageLabels.length
? `小程序的${trustPageLabels.join("、")}中`
: "小程序对应的信任展示区域中";
return (
<QueryState
loading={query.isLoading || workspace.isLoading}
error={query.error || workspace.error}
onRetry={() => void Promise.all([query.refetch(), workspace.refetch()])}
>
<AdminPageHeader
title="小程序内容:品牌介绍与信任"
description={`填写真实的经历、案例、资历、流程和服务说明。这些内容会显示在${trustPlacement};保存后需发布品牌,小程序才会更新。`}
extra={
<Button type="primary" onClick={() => begin()}>
添加品牌内容
</Button>
}
/>
<section aria-labelledby="trust-requirements-title">
<Typography.Title id="trust-requirements-title" level={5}>
当前模板需要配置的内容
</Typography.Title>
<Typography.Paragraph type="secondary">
标记为“发布必填”的项目需要填写完整后,才能发布小程序。
</Typography.Paragraph>
<TrustRequirementsList
availableTypes={availableTypes}
contentByType={contentByType}
items={query.data ?? []}
onEdit={begin}
/>
</section>
<Divider />
<TrustContentTable
availableTypes={availableTypes}
items={query.data ?? []}
removeError={remove.error}
removePending={remove.isPending}
onEdit={(item) => begin(item)}
onDelete={(id) => remove.mutate(id)}
/>
<TrustContentEditorModal
availableTypes={availableTypes}
editing={editing}
form={editorForm}
open={open}
pageLabel={pageLabel}
saveError={save.error}
savePending={save.isPending}
selectedTypeMeta={selectedTypeMeta}
onCancel={closeEditor}
onFinish={(values) => save.mutate(values)}
/>
</QueryState>
);
}