"use client"; import CodeViewer from "@/components/code-viewer"; import { useScrollTo } from "@/hooks/use-scroll-to"; import { CheckIcon } from "@heroicons/react/16/solid"; import { ArrowLongRightIcon, ChevronDownIcon } from "@heroicons/react/20/solid"; import { AnimatePresence, motion } from "framer-motion"; import { FormEvent, useEffect, useState } from "react"; import LoadingDots from "../../components/loading-dots"; import * as Select from "@radix-ui/react-select"; function removeCodeFormatting(code: string): string { return code.replace(/```(?:typescript|javascript|tsx)?\n([\s\S]*?)```/g, "$1").trim(); } export default function Home() { const [status, setStatus] = useState<"initial" | "creating" | "created">("initial"); const [prompt, setPrompt] = useState(""); const [model, setModel] = useState("gemini-2.0-flash-exp"); const [generatedCode, setGeneratedCode] = useState(""); const [ref, scrollTo] = useScrollTo(); const models = [ { label: "gemini-2.0-flash-exp", value: "gemini-2.0-flash-exp" }, { label: "gemini-1.5-pro", value: "gemini-1.5-pro" }, { label: "gemini-1.5-flash", value: "gemini-1.5-flash" }, ]; const loading = status === "creating"; async function createApp(e: FormEvent) { e.preventDefault(); setStatus("creating"); setGeneratedCode(""); try { const res = await fetch("/api/generateCode", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model, messages: [{ role: "user", content: prompt }], }), }); if (!res.ok) { const errorBody = await res.text(); throw new Error(`HTTP Error ${res.status}: ${res.statusText}. ${errorBody}`); } const reader = res.body?.getReader(); if (!reader) throw new Error("The response does not contain a body."); let receivedData = ""; while (true) { const { done, value } = await reader.read(); if (done) break; receivedData += new TextDecoder().decode(value); } const cleanedData = removeCodeFormatting(receivedData); setGeneratedCode(cleanedData); setStatus("created"); } catch (error: any) { console.error("Error during code generation:", error.message); setStatus("initial"); } } useEffect(() => { const el = document.querySelector(".cm-scroller"); if (el && loading) { const end = el.scrollHeight - el.clientHeight; el.scrollTo({ top: end }); } }, [loading, generatedCode]); return (
Powered by Gemini API

Turn your idea
into an app