Files
persionalBrand/apps/admin/src/features/brand/BrandPage.tsx
T

254 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
Alert,
Button,
Col,
Form,
Input,
Row,
Select,
Segmented,
Space,
Tag,
Typography,
message,
} from "antd";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { Link, useLocation } from "react-router-dom";
import type { BrandProfile } from "@personal-brand/api-client";
import { createApiClient } from "../../lib/api.js";
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
import { MutationError, QueryState } from "../../components/QueryState.js";
import { PresentationEditor } from "./PresentationEditor.js";
import type { PreviewSelection } from "./preview-selection.js";
import "./BrandPage.css";
import { useEditorDraft } from "./use-editor-draft.js";
export function BrandPage() {
const location = useLocation();
const initialSelection = location.state as PreviewSelection | undefined;
const [panel, setPanel] = useState("pages");
const [dirty, setDirty] = useState(false);
useEditorDraft(dirty);
const client = createApiClient();
const queryClient = useQueryClient();
const [form] = Form.useForm<BrandProfile>();
const brand = useQuery({ queryKey: ["admin-brand"], queryFn: () => client.getAdminBrand() });
const workspace = useQuery({
queryKey: ["admin-workspace"],
queryFn: () => client.getAdminWorkspace(),
});
const previewWorkspace = useQuery({
queryKey: ["admin-preview-workspace"],
queryFn: () => client.getAdminPreviewWorkspace(),
});
const template = useQuery({
queryKey: ["template", brand.data?.templateKey],
queryFn: () => client.getPublicTemplate(brand.data!.templateKey),
enabled: Boolean(brand.data?.templateKey),
});
const save = useMutation({
mutationFn: (values: Partial<BrandProfile>) => client.updateAdminBrand(values),
onSuccess: () => {
setDirty(false);
message.success("品牌表达已保存");
void queryClient.invalidateQueries({ queryKey: ["admin-brand"] });
void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] });
},
});
return (
<QueryState loading={brand.isLoading} error={brand.error} onRetry={() => void brand.refetch()}>
{!brand.data ? null : (
<>
<AdminPageHeader
title="小程序工作台"
description={
template.data?.description
? `先让访客理解你是谁、服务谁,以及为什么值得信任。${template.data.description}`
: "先让访客理解你是谁、服务谁,以及为什么值得信任。"
}
extra={<Tag color="blue">{template.data?.name ?? "当前模板"}</Tag>}
/>
<div className="brand-page__toolbar">
<Segmented
aria-label="工作台视图"
value={panel}
onChange={(value) => setPanel(String(value))}
options={[
{ value: "pages", label: "页面编辑" },
{ value: "brand", label: "品牌资料" },
]}
/>
<Space>
<Tag color={dirty ? "orange" : "default"}>
{dirty ? "品牌资料有未保存修改" : "草稿工作区"}
</Tag>
<Link to="/admin/publication">检查与发布 →</Link>
</Space>
</div>
<div className="brand-page__content" hidden={panel !== "brand"}>
<div className="brand-page__settings">
<Form
form={form}
layout="vertical"
initialValues={brand.data}
onValuesChange={() => setDirty(true)}
onFinish={(values) => save.mutate(values)}
>
<section className="brand-settings-section" aria-labelledby="brand-identity-title">
<div className="brand-settings-section__header">
<div>
<Typography.Title id="brand-identity-title" level={4}>
我是谁
</Typography.Title>
<Typography.Text type="secondary">公开展示的品牌身份和定位。</Typography.Text>
</div>
<Tag>品牌基础信息</Tag>
</div>
<Form.Item
name="name"
label="品牌名称"
help="可以是个人姓名、工作室名称或品牌名。"
rules={[{ required: true, message: "请输入品牌名称" }]}
>
<Input size="large" placeholder="例如:品牌主理人|品牌顾问" />
</Form.Item>
<Form.Item
name="tagline"
label="一句话定位"
help="用一句话说明你为谁提供什么价值。"
rules={[{ required: true, message: "请输入一句话定位" }]}
>
<Input size="large" placeholder="例如:帮助独立创作者建立可信的个人品牌" />
</Form.Item>
<Form.Item name="description" label="品牌简介" help="介绍背景、方法和合作风格。">
<Input.TextArea
rows={5}
showCount
maxLength={500}
placeholder="让第一次认识你的人快速建立基本信任。"
/>
</Form.Item>
</section>
<section className="brand-settings-section" aria-labelledby="brand-contact-title">
<div className="brand-settings-section__header">
<div>
<Typography.Title id="brand-contact-title" level={4}>
如何联系与合作
</Typography.Title>
<Typography.Text type="secondary">
让访客知道联系渠道、服务范围和响应预期。
</Typography.Text>
</div>
</div>
<Row gutter={16}>
<Col xs={24} md={12}>
<Form.Item name="hours" label="可联系时间">
<Input placeholder="例如:工作日 10:00–18:00" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name="address" label="所在位置">
<Input placeholder="城市或服务范围" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name="contactPhone" label="联系电话">
<Input />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name="contactWechat" label="微信号">
<Input />
</Form.Item>
</Col>
</Row>
<Form.Item
name="deliveryNote"
label="合作预期"
help="说明响应时间、合作方式或重要边界。"
>
<Input.TextArea rows={3} placeholder="例如:收到咨询后 1 个工作日内回复。" />
</Form.Item>
</section>
<section className="brand-settings-section" aria-labelledby="brand-visual-title">
<div className="brand-settings-section__header">
<div>
<Typography.Title id="brand-visual-title" level={4}>
模板与品牌视觉
</Typography.Title>
<Typography.Text type="secondary">
模板负责业务表达,品牌信息仍由你统一维护。
</Typography.Text>
</div>
</div>
<Form.Item
name="templateKey"
label="业务表达模板"
help="模板只改变业务表达,不改变个人品牌主线。"
>
<Select
disabled
options={[
{ value: "personal", label: "个人品牌" },
{ value: "food", label: "餐饮模板" },
]}
/>
</Form.Item>
<Row gutter={16}>
<Col xs={24} md={12}>
<Form.Item
name="avatarUrl"
label="头像图片地址"
help="点击上传或粘贴一张可公开访问的图片地址。"
>
<ImageUrlField scope="brand" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item
name="coverUrl"
label="品牌封面图片地址"
help="点击上传,或粘贴一张横向图片地址。"
>
<ImageUrlField scope="brand" />
</Form.Item>
</Col>
</Row>
</section>
<Space direction="vertical" style={{ width: "100%", marginTop: 24 }}>
<MutationError error={save.error} />
<Button type="primary" htmlType="submit" loading={save.isPending} size="large">
保存品牌表达
</Button>
</Space>
</Form>
</div>
{template.data ? (
<Alert
className="brand-page__template-note"
type="info"
showIcon
message={`当前使用:${template.data.name}`}
description={`适用场景:${template.data.serviceCategories.join("、") || "通用品牌展示"}`}
/>
) : null}
</div>
<div hidden={panel !== "pages"}>
<PresentationEditor
templateContent={workspace.data?.templateContent}
previewWorkspace={previewWorkspace.data}
config={brand.data.config}
brand={brand.data}
loading={workspace.isLoading}
error={workspace.error}
{...(initialSelection ? { initialSelection } : {})}
/>
</div>
</>
)}
</QueryState>
);
}