function escapeHtml(text) {
return String(text)
.replace(/&/g, "&")
.replace(//g, ">");
}
function intakeItemRow(f, { showEmpty = true } = {}) {
if (!f.filled && !showEmpty) return "";
const status = f.filled ? "filled" : "empty";
const preview = f.value
? `${escapeHtml(f.value)}`
: `未填写`;
return `
${f.filled ? "✓" : "○"}
${escapeHtml(f.label)}
${preview}
`;
}
function intakeListSection(title, items, countLabel, { showEmpty = true } = {}) {
if (!items.length) return "";
const filled = items.filter((f) => f.filled).length;
const rows = items.map((f) => intakeItemRow(f, { showEmpty })).filter(Boolean);
if (!rows.length) return "";
return `
${escapeHtml(title)}
${filled}/${items.length} ${countLabel}
`;
}
function renderFilledSummary(fields) {
const filled = fields.filter((f) => f.filled && f.value);
if (!filled.length) return "";
const body = filled
.map(
(f) =>
`
${escapeHtml(f.label)}
${escapeHtml(f.value)}
`,
)
.join("");
return `
已填写内容 ${filled.length} 项
${body}
`;
}
function renderOptionalBlock(optional) {
if (!optional.length) return "";
const filled = optional.filter((f) => f.filled).length;
const open = filled > 0 ? " open" : "";
const rows = optional.map((f) => intakeItemRow(f, { showEmpty: true })).join("");
return `
可选项(选填) ${filled}/${optional.length} 已填
`;
}
/**
* @param {object | undefined} intake
* @param {{ variant?: "composer" | "feed" }} [options]
*/
export function renderIntakePanel(intake, options = {}) {
if (!intake?.fields?.length) return "";
const variant = options.variant ?? "composer";
const required = intake.fields.filter((f) => f.required);
const optional = intake.fields.filter((f) => !f.required);
const requiredSection = intakeListSection("必要项", required, "已填");
const filledSummary = renderFilledSummary(intake.fields);
const optionalBlock = renderOptionalBlock(optional);
const headBadge =
required.length > 0
? `${intake.requiredFilled}/${intake.requiredTotal} 必要项`
: "";
const className =
variant === "feed" ? "intake-panel intake-panel-feed" : "intake-panel";
return `
填空进度
${headBadge}
${requiredSection}
${filledSummary}
${optionalBlock}
`;
}