import { useEffect, useRef, useState } from 'react'; import { Brain, ChevronRight } from 'lucide-react'; import { cn } from '@/lib/utils'; import type { StreamingDraftSection } from '../../types/ai-writing'; import { CollapseTransition } from './CollapseTransition'; interface DraftStreamingViewProps { title: string; sections: StreamingDraftSection[]; } function TypewriterCursor() { return ( ); } function SectionThinking({ thinkingText, isDone }: { thinkingText: string; isDone: boolean }) { const [open, setOpen] = useState(true); const preRef = useRef(null); useEffect(() => { if (open && preRef.current) { preRef.current.scrollTop = preRef.current.scrollHeight; } }, [thinkingText, open]); if (!thinkingText) return null; return ( setOpen((v) => !v)} className="w-full flex items-center gap-1.5 px-2.5 py-1.5 bg-amber-100/40 dark:bg-amber-900/30 hover:bg-amber-100/70 dark:hover:bg-amber-900/50 transition-colors" > 思考过程 {thinkingText} {!isDone && ( )} ); } /** 章节正文:把 `### 要点` 行渲染成小标题,其余按段落显示(流式纯文本,非完整 markdown 解析) */ function SectionBody({ text, showCursor }: { text: string; showCursor: boolean }) { const lines = text.split('\n'); return ( {lines.map((line, i) => { // 严格模式下作家声明素材不足的哨兵行,不展示给用户 if (line.includes('[[SECTION_BLOCKED]]')) return null; const heading = /^[ \t]*#{1,6}[ \t]+/.exec(line); if (heading) { return ( {line.slice(heading[0].length)} ); } if (!line.trim()) return null; // 跳过空行,避免大段空白 return ( {line} ); })} {showCursor && } ); } export function DraftStreamingView({ title, sections }: DraftStreamingViewProps) { const bottomRef = useRef(null); useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); }, [sections]); const activeSectionIdx = sections.reduce( (max, s) => (!s.isDone ? Math.max(max, s.sectionIndex) : max), -1, ); if (sections.length === 0) return null; return ( {title && ( {title} )} {sections.map((sec) => ( {sec.sectionTitle} {/* 该章节的思考过程(可折叠) */} {/* 正文内容(### 要点行渲染为小标题) */} ))} ); }
{thinkingText} {!isDone && ( )}
{line.slice(heading[0].length)}
{line}