Files
writer-work-flow/apps/web/components/generation/WorldGenerator.tsx
Yaojia Wang 1afeb2cdb5 feat(ui): 强化 AI 工作中动效——构思占位/流式进度条/波动三点/转圈
- 动效基元:ai-dot 波动(三点竖跳)、ai-stream 不确定进度条、ai-breathe 呼吸微光(均带 reduced-motion 回退)
- ThinkingIndicator 升级为明显波动;新增 Spinner / StreamingBar / GenerationSkeleton 原子件;Button 加 loading 态
- 写作台:首 token 前正文区『AI 正在构思本章…』占位(补最大缺口,不再空白像卡死)+ 流式顶部进度条
- 整章重写流式进度条;续写/润色忙碌骨架 + 触发键转圈;生成器/角色/世界观改用 Button loading
2026-07-12 19:32:18 +02:00

168 lines
5.9 KiB
TypeScript
Raw Permalink 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, useState } from "react";
import { Copy, Globe2, RotateCcw, Sparkles } from "lucide-react";
import { useToast } from "@/components/Toast";
import { ThinkingIndicator } from "@/components/ThinkingIndicator";
import { Button } from "@/components/ui/Button";
import { EmptyState } from "@/components/ui/EmptyState";
import { Field } from "@/components/ui/Field";
import { SectionHeader } from "@/components/ui/SectionHeader";
import { StatusNote } from "@/components/ui/StatusNote";
import { TextArea } from "@/components/ui/TextArea";
import { worldEntityRules } from "@/lib/generation/cards";
import type { WorldEntityCardView } from "@/lib/api/types";
import { useWorldGen } from "@/lib/generation/useWorldGen";
import { cardClass } from "@/lib/ui/variants";
interface WorldGeneratorProps {
projectId: string;
}
// 世界观设计器UX §6.5):一句话需求 → 预览实体卡type/name/rules
// 本期世界观入库端点未建(仅预览);预览结果供作者参考/复制。
// 复制文案:名称 + 硬规则正文(供作者粘贴到「规则」或「设定库」)。
function entityClipboardText(entity: WorldEntityCardView): string {
const rules = worldEntityRules(entity);
const body = rules.length > 0 ? rules.join("\n") : "(无显式硬规则)";
return `${entity.name}${entity.type}\n${body}`;
}
export function WorldGenerator({ projectId }: WorldGeneratorProps) {
const gen = useWorldGen();
const toast = useToast();
const [brief, setBrief] = useState("");
const onGenerate = useCallback(async () => {
await gen.generate(projectId, brief);
}, [gen, projectId, brief]);
const onCopy = useCallback(
async (entity: WorldEntityCardView): Promise<void> => {
try {
await navigator.clipboard.writeText(entityClipboardText(entity));
toast("已复制到剪贴板。", "success");
} catch {
toast("复制失败,请手动选择文本。", "error");
}
},
[toast],
);
const generating = gen.status === "generating";
return (
<section className="flex flex-col gap-4" aria-label="世界观设计器">
<div className="rounded border border-line bg-panel p-4">
<SectionHeader title="世界观设计器" className="mb-3" />
<Field label="设定方向" className="mb-3">
<TextArea
value={brief}
onChange={(e) => setBrief(e.target.value)}
placeholder="如:东方修真世界,灵气复苏,宗门林立,强者寿元有限"
rows={3}
/>
</Field>
<Button
onClick={() => void onGenerate()}
loading={generating}
disabled={brief.trim().length === 0}
variant="primary"
>
<Sparkles className="h-4 w-4" aria-hidden="true" />
</Button>
</div>
{generating ? (
<div className="rounded border border-line bg-panel/50 p-4 text-cinnabar">
<ThinkingIndicator label="生成中" />
</div>
) : null}
{gen.status === "error" ? (
<StatusNote variant="danger">
<span className="flex flex-wrap items-center gap-3">
<span></span>
<Button
type="button"
variant="secondary"
size="sm"
onClick={() => void onGenerate()}
>
<RotateCcw className="h-4 w-4" aria-hidden="true" />
</Button>
</span>
</StatusNote>
) : null}
{gen.status === "done" && gen.entities.length === 0 ? (
<EmptyState
icon={Globe2}
title="本次未生成可用实体"
description="可换一个更具体的设定方向,或补充世界的硬规则与边界后再试。"
action={
<Button
type="button"
variant="primary"
size="sm"
onClick={() => void onGenerate()}
disabled={brief.trim().length === 0}
>
<Sparkles className="h-4 w-4" aria-hidden="true" />
</Button>
}
/>
) : null}
{gen.status === "done" && gen.entities.length > 0 ? (
<div className="flex flex-col gap-3">
<StatusNote variant="info">
</StatusNote>
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
{gen.entities.map((entity, i) => (
<article
key={`${entity.name}-${i}`}
className={cardClass("p-3 text-sm")}
>
<header className="mb-2 flex items-center gap-2">
<span className="font-serif text-base text-ink">
{entity.name}
</span>
<span className="rounded bg-bg px-2 py-0.5 text-xs text-ink-soft">
{entity.type}
</span>
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => void onCopy(entity)}
className="ml-auto"
aria-label={`复制 ${entity.name}`}
>
<Copy className="h-4 w-4" aria-hidden="true" />
</Button>
</header>
{worldEntityRules(entity).length > 0 ? (
<ul className="flex list-disc flex-col gap-1 pl-5 text-ink-soft">
{worldEntityRules(entity).map((rule, j) => (
<li key={j}>{rule}</li>
))}
</ul>
) : (
<p className="text-ink-soft"></p>
)}
</article>
))}
</div>
</div>
) : null}
</section>
);
}