"use client"; import { useMemo, useState } from "react"; import { ListTree, Sparkles } from "lucide-react"; import { AppShell } from "@/components/AppShell"; import { Button } from "@/components/ui/Button"; import { EmptyState } from "@/components/ui/EmptyState"; import { PageHeader } from "@/components/ui/PageHeader"; import { Select } from "@/components/ui/Select"; import { StatusNote } from "@/components/ui/StatusNote"; import { TextInput } from "@/components/ui/TextInput"; import type { OutlineChapterView, ProjectResponse } from "@/lib/api/types"; import { distinctVolumes, filterByViewVolume, groupByVolume, type ViewVolume, } from "@/lib/outline/outline"; import { useOutline } from "@/lib/outline/useOutline"; import { OutlineChapterRow } from "./OutlineChapterRow"; interface OutlineEditorProps { project: ProjectResponse; initialChapters: OutlineChapterView[]; } // 大纲编辑器主体(UX §6.7)。RSC 种入已存大纲;Client 承载生成(POST .../outline)。 // 无凭据 → 503 LLM_UNAVAILABLE 经 error 检出、引导去设置。 export function OutlineEditor({ project, initialChapters, }: OutlineEditorProps) { const { chapters, status, error, generate } = useOutline(initialChapters); // 生成目标卷与「正在看的卷」共用一个心智:选具体卷即把生成目标设为该卷。 const [volume, setVolume] = useState(1); const [viewVolume, setViewVolume] = useState("all"); // 「重新排大纲」破坏性覆盖的两段式确认(仅目标卷已有章节时出现)。 const [confirming, setConfirming] = useState(false); const availableVolumes = useMemo(() => distinctVolumes(chapters), [chapters]); const volumes = useMemo( () => groupByVolume(filterByViewVolume(chapters, viewVolume)), [chapters, viewVolume], ); const generating = status === "generating"; // 目标卷已有章节 → 生成是破坏性替换,文案/确认都要变。 const targetHasChapters = useMemo( () => filterByViewVolume(chapters, volume).length > 0, [chapters, volume], ); // 选具体卷时把生成目标对齐到所看卷,消除「看卷 A、却生成卷 B」的易混。 const onViewVolumeChange = (next: ViewVolume): void => { setViewVolume(next); setConfirming(false); if (next !== "all") setVolume(next); }; const onVolumeChange = (next: number): void => { setVolume(Math.max(1, next || 1)); setConfirming(false); }; const runGenerate = (): void => { setConfirming(false); void generate(project.id, volume); }; // 安全卷直接生成;破坏性卷先进入两段式确认。 const onGenerateClick = (): void => { if (targetHasChapters) { setConfirming(true); return; } runGenerate(); }; const generateLabel = generating ? "排大纲中…" : `${targetHasChapters ? "重新排大纲" : "AI 排大纲"} · 卷 ${volume}`; return (
{/* 卷/视图/生成控件独立成全宽工具栏行(窄屏自动换行,不用脆弱的 ml-auto)。 */}
{availableVolumes.length > 0 ? ( ) : null} {availableVolumes.length > 0 ? (
{confirming ? (

重新排大纲会替换卷 {volume} 的全部条目(其它卷不受影响)。

) : null} {error ? ( {error.text} {error.actionHref ? ( <> {" "} {error.actionLabel} ) : null} ) : null}
{volumes.length === 0 ? ( 0 && viewVolume !== "all" ? `卷 ${viewVolume} 暂无大纲` : "暂无大纲" } description={ chapters.length > 0 && viewVolume !== "all" ? "切到「全部」查看其它卷,或为当前卷生成章节节拍。" : "点击「AI 排大纲」生成逐章节拍与伏笔窗口,再回到写作台逐章推进。" } action={ } /> ) : ( volumes.map((group) => (

卷 {group.volume}

    {group.chapters.map((ch) => ( ))}
)) )}
); }