From 365eabead1b6b17eaf637f9b7a876c3bd99d4575 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 24 Mar 2026 02:53:01 +0800 Subject: [PATCH] fix: stop canvas reload effect loop --- app/canvas/[id]/page.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/canvas/[id]/page.tsx b/app/canvas/[id]/page.tsx index f9ce052df..17f899be9 100644 --- a/app/canvas/[id]/page.tsx +++ b/app/canvas/[id]/page.tsx @@ -413,13 +413,20 @@ export default function CanvasEditorPage({ params }: PageProps) { workflowMaterials, workflowSelections, ]); + const currentWorkflowPersistedStateRef = useRef( + buildCurrentWorkflowPersistedState() + ); + + useEffect(() => { + currentWorkflowPersistedStateRef.current = buildCurrentWorkflowPersistedState(); + }, [buildCurrentWorkflowPersistedState]); // 加载画布数据 useLayoutEffect(() => { console.log('[Canvas] Loading project:', id); const loadProject = async () => { - const preservedWorkflowState = buildCurrentWorkflowPersistedState(); + const preservedWorkflowState = currentWorkflowPersistedStateRef.current; const shouldPreserveCurrentWorkflowState = requestedAgentMode && lastLoadedCanvasIdRef.current === id && @@ -532,7 +539,7 @@ export default function CanvasEditorPage({ params }: PageProps) { }; loadProject(); - }, [buildCurrentWorkflowPersistedState, id, requestedAgentMode]); + }, [id, requestedAgentMode]); useEffect(() => { const updateViewportWidth = () => setViewportWidth(window.innerWidth); @@ -596,6 +603,11 @@ export default function CanvasEditorPage({ params }: PageProps) { workflowMaterials, workflowSelections, ]); + const persistedAgentProjectStateRef = useRef(buildPersistedAgentProjectState()); + + useEffect(() => { + persistedAgentProjectStateRef.current = buildPersistedAgentProjectState(); + }, [buildPersistedAgentProjectState]); useEffect(() => { if (isAgentMode) return; @@ -724,7 +736,7 @@ export default function CanvasEditorPage({ params }: PageProps) { try { const result = await agentProjectService.createProject({ title: buildDefaultAgentProjectTitle(), - projectState: buildPersistedAgentProjectState(), + projectState: persistedAgentProjectStateRef.current, }); if (!result.success || !result.data) { throw new Error(result.error || "创建生图项目失败"); @@ -751,7 +763,6 @@ export default function CanvasEditorPage({ params }: PageProps) { }; }, [ agentProjectId, - buildPersistedAgentProjectState, isAgentWorkspaceEnabled, requestedAgentMode, updateCanvasSearchParams, @@ -773,7 +784,7 @@ export default function CanvasEditorPage({ params }: PageProps) { const persistedState = normalizeAgentGenerationProjectPersistedState( result.data.projectState ); - const currentWorkflowState = buildCurrentWorkflowPersistedState(); + const currentWorkflowState = currentWorkflowPersistedStateRef.current; const persistedWorkflow = hasMeaningfulWorkflowState(persistedState.workflowState) ? persistedState.workflowState : hasMeaningfulWorkflowState(currentWorkflowState) @@ -814,7 +825,7 @@ export default function CanvasEditorPage({ params }: PageProps) { return () => { cancelled = true; }; - }, [agentProjectId, buildCurrentWorkflowPersistedState, requestedAgentMode]); + }, [agentProjectId, requestedAgentMode]); useEffect(() => { if (!isAgentMode || !agentProjectId || isAgentProjectLoading) return;