import { useState, useEffect } from 'react'; import { UserCog, PenTool, Sparkles, BookOpen, Loader2 } from 'lucide-react'; import { GlassCard, GlassButton, GlassInput, GlassSelect } from '../components/ui'; import useStore from '../store/useStore'; import { generateEpicScript } from '../services/ai'; import { scriptStyles, scriptLengths } from '../utils/constants'; /** * ScriptView 组件 * 爽文剧本视图,包含角色设定、创作需求和剧本展示 * @param {Object} props * @param {Function} props.onOpenProfile - 打开用户资料模态框回调 */ const ScriptView = ({ onOpenProfile }) => { const { registrationData, lifeEvents, scripts, selectedScriptId, addScript, setSelectedScriptId, getSelectedScript, loadScripts } = useStore(); // 加载剧本列表 useEffect(() => { loadScripts().catch(() => { // 后端不可用时忽略错误 }); }, [loadScripts]); // 表单状态 const [theme, setTheme] = useState(''); const [style, setStyle] = useState(scriptStyles[0].value); const [length, setLength] = useState(scriptLengths[0].value); const [isLoading, setIsLoading] = useState(false); /** * 处理剧本生成 */ const handleGenerate = async () => { if (!theme) { alert('请输入主题'); return; } setIsLoading(true); try { const content = await generateEpicScript( { theme, style, length, character: registrationData }, lifeEvents ); addScript({ theme, style, length, content }); setTheme(''); } catch (error) { console.error('Failed to generate script:', error); } finally { setIsLoading(false); } }; /** * 格式化剧本内容,高亮【标题】 */ const formatScriptContent = (content) => { if (!content) return ''; return content.replace( /【([^】]+)】/g, '
暂无卷轴
)}{selectedScript.style}篇 · {selectedScript.length}卷
请在左侧设定需求,开启你的爽文人生