|
| 1 | +import { useRef, useCallback } from 'react'; |
| 2 | +import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs'; |
| 3 | +import { CircuitEnv } from './circuit-env'; |
| 4 | +import { Scene3D } from './Scene3D'; |
| 5 | +import { RewardChart } from './RewardChart'; |
| 6 | +import { CodePanel } from './CodePanel'; |
| 7 | +import { Controls } from './Controls'; |
| 8 | +import { useDemoStore } from './store'; |
| 9 | + |
| 10 | +export default function App() { |
| 11 | + const envRef = useRef<IgnitionEnvTFJS | null>(null); |
| 12 | + const circuitRef = useRef<CircuitEnv>(new CircuitEnv()); |
| 13 | + const episodeRewardRef = useRef(0); |
| 14 | + const origStepRef = useRef<typeof CircuitEnv.prototype.step | null>(null); |
| 15 | + const origResetRef = useRef<typeof CircuitEnv.prototype.reset | null>(null); |
| 16 | + |
| 17 | + const { setMode, updateCar, recordEpisode, resetStats } = useDemoStore(); |
| 18 | + |
| 19 | + const syncState = useCallback(() => { |
| 20 | + const c = circuitRef.current; |
| 21 | + updateCar({ carX: c.carX, carY: c.carY, carAngle: c.carAngle, stepCount: c.stepCount, laps: c.laps }); |
| 22 | + }, [updateCar]); |
| 23 | + |
| 24 | + const createEnv = useCallback(() => { |
| 25 | + const circuit = circuitRef.current; |
| 26 | + if (!origStepRef.current) { |
| 27 | + origStepRef.current = circuit.step.bind(circuit); |
| 28 | + origResetRef.current = circuit.reset.bind(circuit); |
| 29 | + circuit.step = (action: number | number[]) => { |
| 30 | + origStepRef.current!(action); |
| 31 | + episodeRewardRef.current += circuit.reward(); |
| 32 | + syncState(); |
| 33 | + }; |
| 34 | + circuit.reset = () => { |
| 35 | + recordEpisode(episodeRewardRef.current); |
| 36 | + episodeRewardRef.current = 0; |
| 37 | + origResetRef.current!(); |
| 38 | + syncState(); |
| 39 | + }; |
| 40 | + } |
| 41 | + const env = new IgnitionEnvTFJS(circuit); |
| 42 | + envRef.current = env; |
| 43 | + syncState(); |
| 44 | + return env; |
| 45 | + }, [syncState, recordEpisode]); |
| 46 | + |
| 47 | + const handleStart = useCallback(() => { |
| 48 | + if (!envRef.current) createEnv(); |
| 49 | + envRef.current!.train(useDemoStore.getState().algorithm); |
| 50 | + setMode('training'); |
| 51 | + }, [createEnv, setMode]); |
| 52 | + |
| 53 | + const handleInfer = useCallback(() => { |
| 54 | + if (!envRef.current?.agent) return; |
| 55 | + envRef.current.stop(); |
| 56 | + envRef.current.infer(); |
| 57 | + setMode('inference'); |
| 58 | + }, [setMode]); |
| 59 | + |
| 60 | + const handleStop = useCallback(() => { |
| 61 | + envRef.current?.stop(); |
| 62 | + setMode('stopped'); |
| 63 | + }, [setMode]); |
| 64 | + |
| 65 | + const handleReset = useCallback(() => { |
| 66 | + envRef.current?.stop(); |
| 67 | + envRef.current?.agent?.dispose?.(); |
| 68 | + envRef.current = null; |
| 69 | + origResetRef.current?.(); |
| 70 | + episodeRewardRef.current = 0; |
| 71 | + resetStats(); |
| 72 | + setMode('stopped'); |
| 73 | + syncState(); |
| 74 | + }, [resetStats, setMode, syncState]); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div style={{ minHeight: '100vh', background: '#0a0a1a', color: '#e2e8f0', fontFamily: 'system-ui, sans-serif' }}> |
| 78 | + {/* Header */} |
| 79 | + <header style={{ textAlign: 'center', padding: '20px 0 4px' }}> |
| 80 | + <h1 style={{ margin: 0, fontSize: 28, fontWeight: 800, letterSpacing: '-0.5px' }}> |
| 81 | + Ignition<span style={{ color: '#6366f1' }}>AI</span> — Car Circuit |
| 82 | + </h1> |
| 83 | + <p style={{ margin: '4px 0 0', color: '#888', fontSize: 14 }}> |
| 84 | + Train RL agents in the browser. Deploy anywhere. |
| 85 | + </p> |
| 86 | + </header> |
| 87 | + |
| 88 | + {/* 3D Scene — hero area */} |
| 89 | + <div style={{ height: '50vh', minHeight: 350, maxHeight: 500, margin: '0 auto', maxWidth: 1000 }}> |
| 90 | + <Scene3D /> |
| 91 | + </div> |
| 92 | + |
| 93 | + {/* Controls */} |
| 94 | + <Controls onStart={handleStart} onStop={handleStop} onReset={handleReset} onInfer={handleInfer} /> |
| 95 | + |
| 96 | + {/* Bottom: code + chart */} |
| 97 | + <div style={{ display: 'flex', gap: 24, padding: '8px 32px 24px', maxWidth: 1000, margin: '0 auto' }}> |
| 98 | + <div style={{ flex: '0 0 380px' }}> |
| 99 | + <CodePanel /> |
| 100 | + </div> |
| 101 | + <div style={{ flex: 1, minWidth: 250 }}> |
| 102 | + <RewardChart /> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
0 commit comments