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, '
【$1】
' ); }; const selectedScript = getSelectedScript(); return (
{/* 左侧面板 */}
{/* 角色设定卡片 */}

角色设定

{registrationData.nickname || '-'}
{registrationData.zodiac || '-'}
{registrationData.mbti || '-'}
{registrationData.hobbies?.join(', ') || '-'}
{/* 创作需求表单 */}

创作需求

{isLoading ? ( <> 编撰中... ) : ( '生成爽文人生' )}
{/* 历史卷轴列表 */}
历史卷轴
{scripts.length > 0 ? ( scripts.map((script) => (
setSelectedScriptId(script.id)} className={` p-3 glass-card text-left cursor-pointer hover:bg-white/5 border-white/5 transition-all ${script.id === selectedScriptId ? 'border-orange-200/30 bg-orange-200/5' : ''} `} >
{script.theme}
{script.style} | {script.length} {script.date}
)) ) : (

暂无卷轴

)}
{/* 右侧剧本展示区 */}
{selectedScript ? (

{selectedScript.theme}

{selectedScript.style}篇 · {selectedScript.length}卷

) : (

请在左侧设定需求,开启你的爽文人生

)}
); }; export default ScriptView;