Spaces:
Running
Running
import { useState } from 'react'; | |
import Script from 'next/script'; | |
import { Inter } from 'next/font/google'; | |
import SketchRenderer from '../components/SketchRenderer'; | |
import GenerationForm from '../components/GenerationForm'; | |
import CodePanel from '../components/CodePanel'; | |
import { initialSketch, examples, exampleReasonings } from '../sketches'; | |
const inter = Inter({ | |
subsets: ['latin'], | |
variable: '--font-inter', | |
}); | |
export default function Home() { | |
const [word, setWord] = useState(''); | |
const [instructions, setInstructions] = useState(''); | |
const [isGenerating, setIsGenerating] = useState(false); | |
const [generatedCode, setGeneratedCode] = useState(initialSketch); | |
const [showCodePanel, setShowCodePanel] = useState(false); | |
const [error, setError] = useState(null); | |
const [activeExample, setActiveExample] = useState(null); | |
const [showExamples, setShowExamples] = useState(false); | |
const [modelResponse, setModelResponse] = useState(''); | |
const [showSystemPrompt, setShowSystemPrompt] = useState(false); | |
const [isCapturingGif, setIsCapturingGif] = useState(false); | |
const handleExampleClick = (example) => { | |
setActiveExample(example); | |
setGeneratedCode(examples[example]); | |
setShowCodePanel(true); | |
setModelResponse(exampleReasonings[example]); | |
}; | |
const handleSubmit = async (e) => { | |
e.preventDefault(); | |
setIsGenerating(true); | |
setError(null); | |
setActiveExample(null); | |
try { | |
const response = await fetch('/api/generate', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ word, instructions }), | |
}); | |
const data = await response.json(); | |
if (!response.ok) { | |
throw new Error(data.message || `API error: ${response.status}`); | |
} | |
if (!data || data.error) { | |
throw new Error(data.error || 'No data received from API'); | |
} | |
if (!data.code || !data.code.includes('function setup()')) { | |
throw new Error('Generated code is not in the correct format'); | |
} | |
setGeneratedCode(data.code); | |
setModelResponse(data.fullResponse || ''); | |
setShowCodePanel(true); | |
} catch (error) { | |
console.error('Generation error:', error); | |
setError(error.message); | |
} finally { | |
setIsGenerating(false); | |
} | |
}; | |
const handleReplay = () => { | |
setGeneratedCode(prevCode => prevCode + ' '); | |
requestAnimationFrame(() => { | |
setGeneratedCode(prevCode => prevCode.slice(0, -1)); | |
}); | |
}; | |
const handleGifCapture = async (startCapture) => { | |
if (startCapture) { | |
setIsCapturingGif(true); | |
try { | |
const iframe = document.getElementById('sketch-iframe'); | |
if (iframe) { | |
iframe.contentWindow.postMessage({ type: 'startGifCapture' }, '*'); | |
} | |
} catch (error) { | |
console.error('GIF capture error:', error); | |
setIsCapturingGif(false); | |
} | |
} else { | |
setIsCapturingGif(false); | |
} | |
}; | |
return ( | |
<div className={`min-h-screen bg-white text-black flex flex-col items-center p-4 sm:p-8 ${inter.variable}`}> | |
<Script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js" strategy="beforeInteractive" /> | |
); | |
} |