import { useEffect, useRef } from 'react'; import ReplayButton from './ReplayButton'; import GifDownloadButton from './GifDownloadButton'; import { loadingSketch } from '../sketches'; export default function SketchRenderer({ generatedCode, isGenerating, error, showCodePanel, onReplay, onGifCapture, isCapturingGif, isInitialSketch }) { const sketchContainer = useRef(null); const currentSketch = useRef(null); useEffect(() => { // Add message listener for GIF capture completion const handleGifComplete = (event) => { if (event.data.type === 'gifCaptureComplete') { onGifCapture(false); // Signal completion } }; window.addEventListener('message', handleGifComplete); return () => { window.removeEventListener('message', handleGifComplete); }; }, [onGifCapture]); useEffect(() => { if (generatedCode && window.p5 && sketchContainer.current) { // Cleanup previous sketch if it exists const cleanup = () => { if (currentSketch.current) { currentSketch.current.remove(); currentSketch.current = null; } }; cleanup(); try { const formattedCodeResponse = `