feat: 统一业务内容双入口编辑预览

This commit is contained in:
que01 committed 2026-10-11 12:55:13 +08:00
1 parent bfbcee45bb
commit 91322fa9a0
5 files changed
+338 -77

No files matched your search

@@ -16,13 +16,21 @@
overflow: hidden;
}
.service-offering-editor__preview > * {
flex-shrink: 0;
}
.service-offering-editor__view {
align-self: flex-start;
margin-bottom: 10px;
}
.service-offering-editor__preview > .component-template-preview-shell {
flex: 1;
flex: 1 1 0;
min-height: 0;
}
.service-offering-editor__preview .component-template-preview {
min-height: 0;
}
@@ -31,13 +39,14 @@
display: flex;
flex-direction: column;
max-height: 72vh;
overflow-y: auto;
padding-right: 4px;
}
.service-offering-editor__form-scroll {
flex: 1;
min-height: 0;
overflow-y: auto;
padding-right: 4px;
}
.service-offering-editor__preview .ant-alert {
@@ -21,7 +21,6 @@ import type {
TemplateAdminSection,
} from "@personal-brand/api-client";
import { useMemo, useState } from "react";
import { useEffect, useRef } from "react";
import { createApiClient } from "../../lib/api.js";
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
@@ -44,13 +43,12 @@ export function ServicesPage() {
const [offeringForm] = Form.useForm();
const [offeringOpen, setOfferingOpen] = useState(false);
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
const [offeringDraftVersion, setOfferingDraftVersion] = useState(0);
const [previewPage, setPreviewPage] = useState<"offerings" | "detail">("detail");
const listFieldsRef = useRef<HTMLDivElement | null>(null);
const detailFieldsRef = useRef<HTMLDivElement | null>(null);
const formScrollRef = useRef<HTMLDivElement | null>(null);
const offerings = useQuery({
queryKey: ["admin-offerings"],
queryFn: () => client.listAdminOfferings(),
refetchOnMount: "always",
});
const workspace = useQuery({
queryKey: ["admin-workspace-offering-fields"],
@@ -59,6 +57,7 @@ export function ServicesPage() {
const previewWorkspace = useQuery({
queryKey: ["admin-preview-workspace-services"],
queryFn: () => client.getAdminPreviewWorkspace(),
refetchOnMount: "always",
});
const templateFields: TemplateAdminField[] = (
workspace.data?.templateContent?.admin.sections ?? []
@@ -134,14 +133,10 @@ export function ServicesPage() {
),
[templateFields, listFields, detailFields],
);
useEffect(() => {
const target = previewPage === "offerings" ? listFieldsRef.current : detailFieldsRef.current;
if (!target || !formScrollRef.current) return;
formScrollRef.current.scrollTo({
top: target.offsetTop - formScrollRef.current.offsetTop - 12,
behavior: "smooth",
});
}, [previewPage, editingOffering?.id]);
const activeTemplateFields =
previewPage === "offerings"
? [...listFields, ...otherFields.filter((field) => field.page === "offerings")]
: [...detailFields, ...otherFields.filter((field) => field.page === "detail")];
const previewWorkspaceDraft = useMemo<RuntimeWorkspace | undefined>(() => {
if (!previewWorkspace.data || !editingOffering) return undefined;
const values = offeringForm.getFieldsValue(true) as Record<string, unknown>;
@@ -184,19 +179,34 @@ export function ServicesPage() {
...(config.presentation && typeof config.presentation === "object"
? (config.presentation as Record<string, unknown>)
: {}),
...(typeof config.categoryLabel === "string"
? { categoryLabel: config.categoryLabel }
: {}),
...(typeof config.area === "string" ? { area: config.area } : {}),
...(typeof config.series === "string" ? { series: config.series } : {}),
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
heroImage:
typeof config.detailHero === "string"
? config.detailHero
: typeof offeringValues.coverUrl === "string" && offeringValues.coverUrl.trim()
? offeringValues.coverUrl
: (item.coverUrl ?? null),
details: {
...item.presentation.details,
...(config.details && typeof config.details === "object"
? {
...item.presentation.details,
...(config.details as Record<string, unknown>),
}
: {}),
},
},
}
: item,
),
brand: previewWorkspace.data.brand,
};
}, [previewWorkspace.data, editingOffering, templateFields]);
}, [previewWorkspace.data, editingOffering, templateFields, offeringDraftVersion]);
const saveOffering = useMutation({
mutationFn: (values: Record<string, unknown>) => {
const fields = Object.fromEntries(
@@ -217,7 +227,10 @@ export function ServicesPage() {
setOfferingOpen(false);
setEditingOffering(null);
offeringForm.resetFields();
offeringForm.setFieldsValue({});
void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] });
void queryClient.invalidateQueries({ queryKey: ["admin-workspace-offering-fields"] });
void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace-services"] });
},
});
const deleteOffering = useMutation({
@@ -412,6 +425,7 @@ export function ServicesPage() {
<ComponentTemplatePreview
workspace={previewWorkspaceDraft}
page={previewPage}
offeringId={editingOffering.id}
targets={previewTargets[previewPage] ?? []}
selection={{
targetKey:
@@ -428,9 +442,10 @@ export function ServicesPage() {
className="service-offering-editor__form"
form={offeringForm}
layout="vertical"
onValuesChange={() => setOfferingDraftVersion((version) => version + 1)}
onFinish={(values) => saveOffering.mutate(values as Record<string, unknown>)}
>
<div ref={formScrollRef} className="service-offering-editor__form-scroll">
<div className="service-offering-editor__form-scroll">
<Typography.Title level={5}>基础信息</Typography.Title>
<Form.Item name="kind" label="类型" rules={[{ required: true }]}>
<Select
@@ -479,62 +494,41 @@ export function ServicesPage() {
]}
/>
</Form.Item>
{activeTemplateFields.length ? (
<div>
<Typography.Title level={5}>
{previewPage === "offerings" ? "列表展示字段" : "详情展示字段"}
</Typography.Title>
<Typography.Paragraph type="secondary">
这里只编辑左侧预览对应的字段;它们与小程序页面编辑的是同一份业务数据。
</Typography.Paragraph>
<RecordContentFields
section={{
...templateFieldSection,
fields: activeTemplateFields,
}}
/>
</div>
) : null}
{editingOffering ? (
<Button
htmlType="button"
onClick={() =>
navigate("/admin/brand", {
state: {
targetKey:
previewPage === "offerings"
? visualOfferingTarget.key
: (detailTarget?.key ?? visualOfferingTarget.key),
recordId: editingOffering.id,
},
})
}
>
在小程序页面编辑
</Button>
) : null}
</div>
{templateFields.length ? (
<>
<Typography.Paragraph type="secondary">
以下模板字段与左侧预览及小程序页面编辑的是同一份业务数据。
</Typography.Paragraph>
{listFields.length ? (
<div ref={listFieldsRef}>
<Typography.Title level={5}>列表展示</Typography.Title>
<RecordContentFields
section={{
...templateFieldSection,
fields: listFields,
}}
/>
</div>
) : null}
{detailFields.length ? (
<div ref={detailFieldsRef}>
<Typography.Title level={5}>详情展示</Typography.Title>
<RecordContentFields
section={{
...templateFieldSection,
fields: detailFields,
}}
/>
</div>
) : null}
{otherFields.length ? (
<>
<Typography.Title level={5}>其他模板内容</Typography.Title>
<RecordContentFields
section={{
...templateFieldSection,
fields: otherFields,
}}
/>
</>
) : null}
{editingOffering ? (
<Button
htmlType="button"
onClick={() =>
navigate("/admin/brand", {
state: {
targetKey: visualOfferingTarget.key,
recordId: editingOffering.id,
},
})
}
>
在小程序页面编辑
</Button>
) : null}
</>
) : null}
</Form>
</div>
</Modal>