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为你定制专属计划。
你需要先有一个剧本,才能生成通往它的路径。
将幻想落地为行动,AI为你定制专属计划。
尚未生成路径,点击上方按钮开始规划。