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
361 lines
13 KiB
TypeScript
361 lines
13 KiB
TypeScript
import { App, Badge, Button, Descriptions, Drawer, Select, Space, Table, Input } from "antd";
|
||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||
import type { AdminBooking, InquirySubmission } from "@personal-brand/api-client";
|
||
import { createApiClient } from "../../lib/api.js";
|
||
import { useState } from "react";
|
||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||
import { EmptyState } from "../../components/EmptyState.js";
|
||
import { AdminSection } from "../../components/AdminSection.js";
|
||
import { ErrorNote } from "../../components/QueryState.js";
|
||
import { ListSkeleton, TableSkeleton } from "../../components/TableSkeleton.js";
|
||
import { StatusTag } from "../../components/StatusTag.js";
|
||
import type { StatusTone } from "../../components/StatusTag.js";
|
||
|
||
const statuses = ["received", "contacting", "confirmed", "completed", "cancelled"] as const;
|
||
const labels: Record<string, string> = {
|
||
received: "待处理",
|
||
contacting: "沟通中",
|
||
confirmed: "已确认",
|
||
completed: "已完成",
|
||
cancelled: "已取消",
|
||
};
|
||
const inquiryStatuses = [
|
||
"new",
|
||
"reviewing",
|
||
"replied",
|
||
"qualified",
|
||
"converted",
|
||
"closed",
|
||
] as const;
|
||
const inquiryLabels: Record<string, string> = {
|
||
new: "新提交",
|
||
reviewing: "评估中",
|
||
replied: "已回复",
|
||
qualified: "已确认需求",
|
||
converted: "已转化",
|
||
closed: "已关闭",
|
||
};
|
||
|
||
const statusTones: Record<string, StatusTone> = {
|
||
received: "info",
|
||
new: "info",
|
||
contacting: "warning",
|
||
reviewing: "warning",
|
||
confirmed: "success",
|
||
replied: "success",
|
||
qualified: "success",
|
||
converted: "success",
|
||
completed: "success",
|
||
cancelled: "error",
|
||
closed: "neutral",
|
||
};
|
||
|
||
export function InquiriesPage() {
|
||
const { message } = App.useApp();
|
||
const client = createApiClient();
|
||
const queryClient = useQueryClient();
|
||
const [status, setStatus] = useState<string>();
|
||
const [selectedId, setSelectedId] = useState<string>();
|
||
const [selectedSubmission, setSelectedSubmission] = useState<InquirySubmission>();
|
||
const [submissionStatus, setSubmissionStatus] = useState<InquirySubmission["status"]>();
|
||
const query = useQuery({
|
||
queryKey: ["admin-bookings", status],
|
||
queryFn: () => client.listAdminBookings(status ? { status: status as never } : {}),
|
||
});
|
||
const update = useMutation({
|
||
mutationFn: ({ id, value }: { id: string; value: never }) =>
|
||
client.updateAdminBookingStatus(id, value),
|
||
onSuccess: () => {
|
||
message.success("咨询状态已更新");
|
||
void queryClient.invalidateQueries({ queryKey: ["admin-bookings"] });
|
||
},
|
||
});
|
||
const read = useMutation({
|
||
mutationFn: (id: string) => client.markAdminBookingRead(id),
|
||
onSuccess: () => void queryClient.invalidateQueries({ queryKey: ["admin-bookings"] }),
|
||
});
|
||
const detail = useQuery({
|
||
queryKey: ["admin-booking", selectedId],
|
||
queryFn: () => client.getAdminBooking(selectedId!),
|
||
enabled: Boolean(selectedId),
|
||
});
|
||
const submissions = useQuery({
|
||
queryKey: ["admin-inquiry-submissions"],
|
||
queryFn: () => client.listAdminInquirySubmissions(),
|
||
});
|
||
const updateSubmission = useMutation({
|
||
mutationFn: ({
|
||
id,
|
||
status,
|
||
adminNote,
|
||
}: {
|
||
id: string;
|
||
status: InquirySubmission["status"];
|
||
adminNote?: string;
|
||
}) =>
|
||
client.updateAdminInquirySubmission(id, {
|
||
status,
|
||
...(adminNote === undefined ? {} : { adminNote }),
|
||
}),
|
||
onSuccess: () => {
|
||
message.success("咨询跟进已更新");
|
||
void queryClient.invalidateQueries({ queryKey: ["admin-inquiry-submissions"] });
|
||
},
|
||
onError: (error) => message.error(error instanceof Error ? error.message : "更新失败,请重试"),
|
||
});
|
||
return (
|
||
<>
|
||
<AdminPageHeader title="咨询记录" description="查看访客提交的咨询和预约,并跟进处理进度。" />
|
||
<AdminSection
|
||
title="预约咨询"
|
||
extra={
|
||
<Space>
|
||
<Select
|
||
allowClear
|
||
placeholder="全部状态"
|
||
className="admin-field-w-md"
|
||
value={status}
|
||
onChange={setStatus}
|
||
options={statuses.map((value) => ({ value, label: labels[value] }))}
|
||
/>
|
||
<Badge
|
||
count={
|
||
query.data?.filter((item) => !(item as AdminBooking & { isRead?: boolean }).isRead)
|
||
.length ?? 0
|
||
}
|
||
showZero
|
||
/>
|
||
</Space>
|
||
}
|
||
>
|
||
{query.isLoading && !query.data ? (
|
||
<TableSkeleton />
|
||
) : (
|
||
<Table
|
||
rowKey="id"
|
||
locale={{
|
||
emptyText: (
|
||
<EmptyState
|
||
compact
|
||
title="还没有预约咨询"
|
||
description="访客在小程序提交预约后,会出现在这里。"
|
||
/>
|
||
),
|
||
}}
|
||
dataSource={query.data ?? []}
|
||
columns={[
|
||
{
|
||
title: "编号",
|
||
dataIndex: "bookingNo",
|
||
render: (value: string, item: AdminBooking) => (
|
||
<Button type="link" onClick={() => setSelectedId(item.id)}>
|
||
{value}
|
||
</Button>
|
||
),
|
||
},
|
||
{ title: "联系人", dataIndex: "customerName" },
|
||
{
|
||
title: "提交时间",
|
||
dataIndex: "createdAt",
|
||
render: (value: string) => new Date(value).toLocaleString("zh-CN"),
|
||
},
|
||
{
|
||
title: "状态",
|
||
dataIndex: "status",
|
||
render: (value: string) => (
|
||
<StatusTag
|
||
tone={statusTones[value] ?? "neutral"}
|
||
label={labels[value] ?? value}
|
||
/>
|
||
),
|
||
},
|
||
{
|
||
title: "操作",
|
||
render: (_: unknown, item: AdminBooking) => (
|
||
<Space>
|
||
<Select
|
||
size="small"
|
||
value={item.status}
|
||
onChange={(value) => update.mutate({ id: item.id, value: value as never })}
|
||
options={statuses.map((value) => ({ value, label: labels[value] }))}
|
||
/>
|
||
<Button type="link" size="small" onClick={() => read.mutate(item.id)}>
|
||
标记已读
|
||
</Button>
|
||
</Space>
|
||
),
|
||
},
|
||
]}
|
||
/>
|
||
)}
|
||
<Drawer
|
||
title="咨询详情"
|
||
open={Boolean(selectedId)}
|
||
onClose={() => setSelectedId(undefined)}
|
||
loading={detail.isLoading}
|
||
width={520}
|
||
>
|
||
{detail.data && (
|
||
<Descriptions className="admin-descriptions" column={1} size="small">
|
||
<Descriptions.Item label="编号">{detail.data.bookingNo}</Descriptions.Item>
|
||
<Descriptions.Item label="联系人">{detail.data.customerName}</Descriptions.Item>
|
||
<Descriptions.Item label="状态">
|
||
{labels[detail.data.status] ?? detail.data.status}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="提交时间">
|
||
{new Date(detail.data.createdAt).toLocaleString("zh-CN")}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="管理员备注">
|
||
{detail.data.adminNote || "暂无"}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
)}
|
||
</Drawer>
|
||
</AdminSection>
|
||
<AdminSection
|
||
title={`动态咨询提交${submissions.data ? `(${submissions.data.length})` : ""}`}
|
||
description="按咨询表单提交的记录,字段随表单配置变化。"
|
||
extra={
|
||
<Button onClick={() => void submissions.refetch()} loading={submissions.isFetching}>
|
||
刷新提交记录
|
||
</Button>
|
||
}
|
||
>
|
||
{submissions.isError ? (
|
||
<ErrorNote error={submissions.error} onRetry={() => void submissions.refetch()} />
|
||
) : null}
|
||
<Space
|
||
className={`admin-offset-bottom-sm ${submissions.isError ? "admin-offset-top" : ""}`}
|
||
>
|
||
<Select
|
||
allowClear
|
||
placeholder="动态咨询状态"
|
||
className="admin-field-w-md"
|
||
value={submissionStatus}
|
||
onChange={setSubmissionStatus}
|
||
options={inquiryStatuses.map((value) => ({ value, label: inquiryLabels[value] }))}
|
||
/>
|
||
</Space>
|
||
{submissions.isLoading && !submissions.data ? (
|
||
<ListSkeleton rows={3} />
|
||
) : (
|
||
<Table<InquirySubmission>
|
||
rowKey="id"
|
||
dataSource={(submissions.data ?? []).filter(
|
||
(item) => !submissionStatus || item.status === submissionStatus,
|
||
)}
|
||
locale={{
|
||
emptyText: (
|
||
<EmptyState
|
||
compact
|
||
title="暂无动态咨询提交"
|
||
description="按咨询表单提交的内容会汇总在这里。"
|
||
/>
|
||
),
|
||
}}
|
||
columns={[
|
||
{ title: "表单", dataIndex: ["inquiryForm", "name"] },
|
||
{
|
||
title: "来源业务",
|
||
dataIndex: ["offering", "name"],
|
||
render: (value: string | null | undefined) => value || "品牌咨询",
|
||
},
|
||
{
|
||
title: "状态",
|
||
dataIndex: "status",
|
||
render: (value: InquirySubmission["status"]) => (
|
||
<StatusTag
|
||
tone={statusTones[value] ?? "neutral"}
|
||
label={inquiryLabels[value] ?? value}
|
||
/>
|
||
),
|
||
},
|
||
{ title: "版本", dataIndex: "formVersion", render: (value) => `v${value}` },
|
||
{
|
||
title: "提交时间",
|
||
dataIndex: "createdAt",
|
||
render: (value: string) => new Date(value).toLocaleString("zh-CN"),
|
||
},
|
||
{
|
||
title: "内容",
|
||
dataIndex: "values",
|
||
render: (value: Record<string, unknown>) => {
|
||
const entries = Object.entries(value ?? {});
|
||
return entries.length
|
||
? entries.map(([key, item]) => `${key}: ${String(item)}`).join(";")
|
||
: "暂无内容";
|
||
},
|
||
},
|
||
{
|
||
title: "跟进",
|
||
render: (_: unknown, item: InquirySubmission) => (
|
||
<Space>
|
||
<Button type="link" size="small" onClick={() => setSelectedSubmission(item)}>
|
||
查看
|
||
</Button>
|
||
<Select
|
||
size="small"
|
||
value={item.status}
|
||
options={inquiryStatuses.map((value) => ({
|
||
value,
|
||
label: inquiryLabels[value],
|
||
}))}
|
||
onChange={(value) => updateSubmission.mutate({ id: item.id, status: value })}
|
||
/>
|
||
<Input
|
||
size="small"
|
||
placeholder="备注"
|
||
defaultValue={item.adminNote ?? ""}
|
||
onPressEnter={(event) =>
|
||
updateSubmission.mutate({
|
||
id: item.id,
|
||
status: item.status,
|
||
adminNote: event.currentTarget.value,
|
||
})
|
||
}
|
||
/>
|
||
</Space>
|
||
),
|
||
},
|
||
]}
|
||
/>
|
||
)}
|
||
<Drawer
|
||
title="动态咨询详情"
|
||
open={Boolean(selectedSubmission)}
|
||
onClose={() => setSelectedSubmission(undefined)}
|
||
width={520}
|
||
>
|
||
{selectedSubmission && (
|
||
<Descriptions className="admin-descriptions" column={1} size="small">
|
||
<Descriptions.Item label="表单">
|
||
{selectedSubmission.inquiryForm?.name || "品牌咨询"}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="来源业务">
|
||
{selectedSubmission.offering?.name || "品牌咨询"}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="状态">
|
||
{inquiryLabels[selectedSubmission.status] ?? selectedSubmission.status}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="表单版本">
|
||
v{selectedSubmission.formVersion}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="提交时间">
|
||
{new Date(selectedSubmission.createdAt).toLocaleString("zh-CN")}
|
||
</Descriptions.Item>
|
||
{Object.entries(selectedSubmission.values ?? {}).map(([key, value]) => (
|
||
<Descriptions.Item label={key} key={key}>
|
||
{String(value)}
|
||
</Descriptions.Item>
|
||
))}
|
||
<Descriptions.Item label="管理员备注">
|
||
{selectedSubmission.adminNote || "暂无"}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
)}
|
||
</Drawer>
|
||
</AdminSection>
|
||
</>
|
||
);
|
||
}
|