Skip to content

Commit 85ea6ec

Browse files
committed
fix: simplify state update with single initialize action
1 parent 9955993 commit 85ea6ec

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/hooks/useAnimation.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,40 @@ export function useAnimation(
4747
}
4848

4949
const { timeline } = solution;
50-
const totalSteps = timeline.length; // 总步数等于时间线长度
50+
console.log(`生成时间线,长度: ${timeline.length}`); // 添加日志
5151

52-
// 准备数据结构(这部分可能需要在 reducer 或 action 中处理,以确保状态一致性)
52+
// 准备数据结构
5353
const nodes = [];
5454
const links = [];
5555

56+
// 初始化楼梯节点(当使用DP或矩阵时)
5657
if (state.currentAlgorithm !== 'formula') {
5758
for (let i = 0; i <= n; i++) {
5859
nodes.push({
5960
id: i,
6061
x: 50 + i * 80,
6162
y: 300 - Math.min(i * 40, 200),
62-
value: i <= 1 ? 1 : 0 // 初始值
63+
value: i <= 1 ? 1 : 0
6364
});
65+
66+
// 添加连接(从每个节点到前两个节点)
6467
if (i >= 2) {
6568
links.push({ source: i - 1, target: i });
6669
links.push({ source: i - 2, target: i });
6770
}
6871
}
6972
}
7073

71-
// 构建初始状态更新对象 (Dispatch actions instead of direct object)
74+
// 使用单一action更新所有状态
7275
dispatch({
73-
type: 'animation/initialize', // Consider an initialization action
74-
payload: {
75-
timeline,
76-
totalSteps,
77-
staircase: { nodes, links },
78-
matrix: state.currentAlgorithm === 'matrix' ? [[1, 1], [1, 0]] : [],
79-
formula: ''
80-
}
76+
type: 'animation/initialize',
77+
payload: {
78+
timeline: timeline,
79+
staircase: { nodes, links },
80+
matrix: state.currentAlgorithm === 'matrix' ? [[1, 1], [1, 0]] : [],
81+
formula: ''
82+
}
8183
});
82-
dispatch(setCurrentStep(0)); // 确保初始步骤为0
8384

8485
}, [state.currentAlgorithm, n, dispatch]);
8586

0 commit comments

Comments
 (0)