Skip to content

Commit 8e4e30c

Browse files
committed
refactor: 重构Context模块,拆分为独立文件
1 parent 503a002 commit 8e4e30c

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

src/context/AlgorithmContext.tsx

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
1-
import { createContext, useContext, useState, useCallback, useEffect, useRef, type ReactNode } from 'react';
1+
import { useState, useCallback, useEffect, useRef, type ReactNode } from 'react';
22
import type { AnimationStep, Language } from '../types';
33
import { AlgorithmEngine } from '../services/AlgorithmEngine';
44
import { gridPresets } from '../utils/generators';
5-
6-
interface AlgorithmContextType {
7-
gridData: number[][];
8-
steps: AnimationStep[];
9-
currentStep: number;
10-
currentStepData: AnimationStep | null;
11-
totalSteps: number;
12-
isPlaying: boolean;
13-
playbackSpeed: number;
14-
selectedLanguage: Language;
15-
setGridData: (grid: number[][]) => void;
16-
goToStep: (step: number) => void;
17-
nextStep: () => void;
18-
prevStep: () => void;
19-
togglePlay: () => void;
20-
reset: () => void;
21-
setPlaybackSpeed: (speed: number) => void;
22-
setSelectedLanguage: (lang: Language) => void;
23-
}
24-
25-
const AlgorithmContext = createContext<AlgorithmContextType | null>(null);
5+
import { AlgorithmContext } from './context';
266

277
export function AlgorithmProvider({ children }: { children: ReactNode }) {
288
const [gridData, setGridDataState] = useState<number[][]>(gridPresets[0].data);
@@ -46,6 +26,7 @@ export function AlgorithmProvider({ children }: { children: ReactNode }) {
4626
// 初始化
4727
useEffect(() => {
4828
generateSteps(gridData);
29+
// eslint-disable-next-line react-hooks/exhaustive-deps
4930
}, []);
5031

5132
// 设置网格数据
@@ -134,11 +115,3 @@ export function AlgorithmProvider({ children }: { children: ReactNode }) {
134115
</AlgorithmContext.Provider>
135116
);
136117
}
137-
138-
export function useAlgorithm() {
139-
const context = useContext(AlgorithmContext);
140-
if (!context) {
141-
throw new Error('useAlgorithm must be used within AlgorithmProvider');
142-
}
143-
return context;
144-
}

src/context/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createContext } from 'react';
2+
import type { AlgorithmContextType } from './types';
3+
4+
export const AlgorithmContext = createContext<AlgorithmContextType | null>(null);

src/context/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { AnimationStep, Language } from '../types';
2+
3+
export interface AlgorithmContextType {
4+
gridData: number[][];
5+
steps: AnimationStep[];
6+
currentStep: number;
7+
currentStepData: AnimationStep | null;
8+
totalSteps: number;
9+
isPlaying: boolean;
10+
playbackSpeed: number;
11+
selectedLanguage: Language;
12+
setGridData: (grid: number[][]) => void;
13+
goToStep: (step: number) => void;
14+
nextStep: () => void;
15+
prevStep: () => void;
16+
togglePlay: () => void;
17+
reset: () => void;
18+
setPlaybackSpeed: (speed: number) => void;
19+
setSelectedLanguage: (lang: Language) => void;
20+
}

src/context/useAlgorithm.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useContext } from 'react';
2+
import { AlgorithmContext } from './context';
3+
4+
export function useAlgorithm() {
5+
const context = useContext(AlgorithmContext);
6+
if (!context) {
7+
throw new Error('useAlgorithm must be used within AlgorithmProvider');
8+
}
9+
return context;
10+
}

0 commit comments

Comments
 (0)