Files
writer-work-flow/apps/web/components/toolbox/TemplateFiller.tsx
Yaojia Wang 1a402f5ccc feat(web): F1/F2/F3 前端——拆书入库为规则 / 续写式链选择 / 模板库
- gen:api 纳入 /templates、chain continue_volume、teardown rules ingest 端点
- F1:GeneratorRunner 识别拆书为单对象入库(整 preview 作一条 teardown),
  ingestTable(BookTeardownResult)=rules,buildIngestRequest 组装 teardown 体,
  按 isSingleObjectIngest 隐藏勾选框、改文案「入库为规则」
- F2:ChainStarter 加链类型单选(draft_volume 从头写 / continue_volume 续写),
  chain_key 作 path 参传给 run(CHAIN_KINDS 对齐后端 SUPPORTED_CHAINS)
- F3:模板库页 app/templates + 全局 nav 入口 + TemplatesManager(列/建/删,乐观更新回滚);
  GeneratorRunner 加 TemplateFiller「从模板填入」(body 填进 brief/text,纯前端)
- vitest 覆盖 templates/ingest teardown/templateFillTarget/CHAIN_KINDS 纯逻辑
2026-06-23 20:29:48 +02:00

103 lines
3.3 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.

"use client";
import { useCallback, useEffect, useState } from "react";
import { useToast } from "@/components/Toast";
import { api } from "@/lib/api/client";
import type { TemplateResponse } from "@/lib/api/types";
interface TemplateFillerProps {
// 可填入的目标输入字段名(如 brief / text空 → 不渲染(无可填字段)。
targetField: string | null;
// 选定模板 → 把其 body 填进目标字段(纯前端,不改生成器后端)。
onFill: (field: string, body: string) => void;
}
// 「从模板填入」F3纯前端进场懒加载模板库选一个把 body 填进生成器的 brief/原文输入。
// 与生成器后端解耦——只读 /templates 并回写本地表单值。无模板/无目标字段则不渲染。
export function TemplateFiller({ targetField, onFill }: TemplateFillerProps) {
const toast = useToast();
const [templates, setTemplates] = useState<TemplateResponse[] | null>(null);
const [open, setOpen] = useState(false);
const load = useCallback(async (): Promise<void> => {
try {
const { data, error } = await api.GET("/templates", {});
if (error || !data) {
toast("加载模板失败。", "error");
setTemplates([]);
return;
}
setTemplates(data);
} catch {
toast("加载模板请求异常,请检查网络。", "error");
setTemplates([]);
}
}, [toast]);
useEffect(() => {
if (open && templates === null) void load();
}, [open, templates, load]);
if (!targetField) return null;
const field = targetField;
if (!open) {
return (
<button
type="button"
onClick={() => setOpen(true)}
className="self-start rounded border border-line px-3 py-1 text-sm text-ink-soft hover:text-ink"
>
</button>
);
}
return (
<div
className="flex flex-col gap-2 rounded border border-line bg-bg p-3"
aria-label="从模板填入"
>
<div className="flex items-center justify-between">
<span className="text-sm text-ink-soft">{field}</span>
<button
type="button"
onClick={() => setOpen(false)}
className="text-xs text-ink-soft hover:text-ink"
>
</button>
</div>
{templates === null ? (
<p className="text-sm text-ink-soft"></p>
) : templates.length === 0 ? (
<p className="text-sm text-ink-soft"></p>
) : (
<ul className="flex flex-col gap-1">
{templates.map((t) => (
<li key={t.id}>
<button
type="button"
onClick={() => {
onFill(field, t.body);
setOpen(false);
toast(`已填入模板「${t.title}」。`, "success");
}}
className="w-full rounded border border-line px-3 py-2 text-left text-sm text-ink hover:border-cinnabar"
>
<span className="font-serif">{t.title}</span>
{t.tool_key ? (
<span className="ml-2 text-xs text-ink-soft">
{t.tool_key}
</span>
) : null}
</button>
</li>
))}
</ul>
)}
</div>
);
}