1- import { createContext , useContext , useState , useCallback , useEffect , useRef , type ReactNode } from 'react' ;
1+ import { useState , useCallback , useEffect , useRef , type ReactNode } from 'react' ;
22import type { AnimationStep , Language } from '../types' ;
33import { AlgorithmEngine } from '../services/AlgorithmEngine' ;
44import { 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
277export 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- }
0 commit comments