import React, { useState, useEffect } from 'react'; import { useStoreData } from '../../hooks/useStoreData'; import { Store } from '../../utils/store'; import { AI } from '../../utils/aiLogic'; import { Map, Ghost, Wand2, RefreshCw, MapPin, ChevronDown, Target, Activity, Package, Repeat, Edit3, Trash2, Check, ArrowRight } from 'lucide-react'; import { Button } from '../ui/Button'; import { GlassCard } from '../ui/GlassCard'; import { Select } from '../ui/Input'; export function PathView({ onSwitchToScript }) { const data = useStoreData(); const [selectedScriptId, setSelectedScriptId] = useState(''); const [loading, setLoading] = useState(false); const [isEditing, setIsEditing] = useState(false); const [expandedStep, setExpandedStep] = useState(null); const [editedPath, setEditedPath] = useState(null); // Initialize selectedScriptId when scripts are available useEffect(() => { if (data.generatedScripts.length > 0 && !selectedScriptId) { setSelectedScriptId(data.generatedScripts[0].id); } }, [data.generatedScripts, selectedScriptId]); // Find current path based on selected script const currentPath = data.paths?.find(p => p.scriptId === selectedScriptId); // Update local edited state when path changes useEffect(() => { if (currentPath) { setEditedPath(JSON.parse(JSON.stringify(currentPath))); // Deep copy } else { setEditedPath(null); } }, [currentPath]); const handleGenerate = async () => { if (currentPath && !window.confirm('重新生成将覆盖现有路径规划,确定吗?')) return; const script = data.generatedScripts.find(s => s.id === selectedScriptId); if (!script) return; setLoading(true); try { const newPath = await AI.generatePath(script, data.userProfile); Store.addPath(newPath); setIsEditing(false); } catch (e) { console.error(e); alert('规划失败,请重试'); } finally { setLoading(false); } }; const handleDelete = async () => { if (window.confirm('确定要删除这个路径规划吗?')) { Store.deletePath(currentPath.id); // Sync to backend try { const allPaths = Store.get().paths; const profileRes = await userApi.getCurrentUser(); if (profileRes.data) { await userApi.updateUserProfile({ id: profileRes.data.id, paths: JSON.stringify(allPaths) }); } } catch (err) { console.error("Failed to sync path deletion to backend", err); } setIsEditing(false); } }; const handleSaveEdit = async () => { if (editedPath) { Store.updatePath(editedPath.id, { steps: editedPath.steps }); // Sync to backend try { const allPaths = Store.get().paths; const profileRes = await userApi.getCurrentUser(); if (profileRes.data) { await userApi.updateUserProfile({ id: profileRes.data.id, paths: JSON.stringify(allPaths) }); } } catch (err) { console.error("Failed to sync path update to backend", err); } setIsEditing(false); } }; const handleStepEdit = (stepIndex, field, value) => { if (!editedPath) return; const newSteps = [...editedPath.steps]; newSteps[stepIndex] = { ...newSteps[stepIndex], [field]: value }; setEditedPath({ ...editedPath, steps: newSteps }); }; if (data.generatedScripts.length === 0) { return (

实现路径

将幻想落地为行动,AI为你定制专属计划。

你需要先有一个剧本,才能生成通往它的路径。

); } return (

实现路径

将幻想落地为行动,AI为你定制专属计划。

{!currentPath ? (

尚未生成路径,点击上方按钮开始规划。

) : ( <>
规划生成于 {new Date(currentPath.createdAt).toLocaleDateString()}
{(isEditing ? editedPath.steps : currentPath.steps).map((step, idx) => ( {/* Progress Line */}
{/* Header */}
setExpandedStep(expandedStep === idx ? null : idx)} >
{idx + 1}

{step.phase}

{step.time}
{/* Details (Collapsible) */}
{/* Core Content */}
核心策略
handleStepEdit(idx, 'content', e.currentTarget.textContent)} className={`p-3 rounded-lg text-sm text-gray-300 leading-relaxed border transition-colors ${ isEditing ? 'bg-white/10 border-primary/50 ring-2 ring-primary/20' : 'bg-white/5 border-white/5' }`} > {step.content}
{/* Actions */}
关键行动
handleStepEdit(idx, 'action', e.currentTarget.textContent)} className={`p-3 rounded-lg text-sm text-gray-300 transition-colors ${ isEditing ? 'bg-white/10 border border-primary/50 ring-2 ring-primary/20' : 'bg-blue-500/10 border border-blue-500/20' }`} > {step.action}
{/* Resources */}
所需资源
handleStepEdit(idx, 'resources', e.currentTarget.textContent)} className={`p-3 rounded-lg text-xs text-gray-400 leading-relaxed transition-colors ${ isEditing ? 'bg-white/10 border border-primary/50 ring-2 ring-primary/20' : 'bg-amber-500/10 border border-amber-500/20' }`} > {step.resources}
{/* Habits */}
养成习惯
handleStepEdit(idx, 'habit', e.currentTarget.textContent)} className={`p-3 rounded-lg text-xs text-gray-400 leading-relaxed transition-colors ${ isEditing ? 'bg-white/10 border border-primary/50 ring-2 ring-primary/20' : 'bg-green-500/10 border border-green-500/20' }`} > {step.habit}
))}
"路虽远,行则将至。"
)}
); }