import { useState } from "react"; import ImageUpload from "./components/ImageUpload"; import CodePreview from "./components/CodePreview"; import Preview from "./components/Preview"; import { CodeGenerationParams, generateCode } from "./generateCode"; import Spinner from "./components/Spinner"; import classNames from "classnames"; import { FaCode, FaDesktop, FaDownload, FaMobile, FaUndo, } from "react-icons/fa"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs"; import CodeMirror from "./components/CodeMirror"; function App() { const [appState, setAppState] = useState<"INITIAL" | "CODING" | "CODE_READY">( "INITIAL" ); const [generatedCode, setGeneratedCode] = useState(""); const [referenceImages, setReferenceImages] = useState([]); const [executionConsole, setExecutionConsole] = useState([]); const [updateInstruction, setUpdateInstruction] = useState(""); const [history, setHistory] = useState([]); const downloadCode = () => { // Create a blob from the generated code const blob = new Blob([generatedCode], { type: "text/html" }); const url = URL.createObjectURL(blob); // Create an anchor element and set properties for download const a = document.createElement("a"); a.href = url; a.download = "index.html"; // Set the file name for download document.body.appendChild(a); // Append to the document a.click(); // Programmatically click the anchor to trigger download // Clean up by removing the anchor and revoking the Blob URL document.body.removeChild(a); URL.revokeObjectURL(url); }; const reset = () => { setAppState("INITIAL"); setGeneratedCode(""); setReferenceImages([]); setExecutionConsole([]); setHistory([]); }; function doGenerateCode(params: CodeGenerationParams) { setExecutionConsole([]); setAppState("CODING"); generateCode( params, (token) => setGeneratedCode((prev) => prev + token), (code) => setGeneratedCode(code), (line) => setExecutionConsole((prev) => [...prev, line]), () => setAppState("CODE_READY") ); } // Initial version creation function doCreate(referenceImages: string[]) { setReferenceImages(referenceImages); doGenerateCode({ generationType: "create", image: referenceImages[0], }); } // Subsequent updates function doUpdate() { const updatedHistory = [...history, generatedCode, updateInstruction]; doGenerateCode({ generationType: "update", image: referenceImages[0], history: updatedHistory, }); setHistory(updatedHistory); setGeneratedCode(""); setUpdateInstruction(""); } return (

Screenshot to Code

{appState === "INITIAL" && (

Drag & drop a screenshot to get started.

)} {(appState === "CODING" || appState === "CODE_READY") && ( <> {/* Show code preview only when coding */} {appState === "CODING" && (
{executionConsole.slice(-1)[0]}
)} {appState === "CODE_READY" && (