Spaces:
Running
Running
File size: 6,174 Bytes
21d7fc3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
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" />
);
} |