"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 { ArrowUpOnSquareIcon } from "@heroicons/react/24/outline"; import * as Select from "@radix-ui/react-select"; import * as Switch from "@radix-ui/react-switch"; import { AnimatePresence, motion } from "framer-motion"; import { FormEvent, useEffect, useState } from "react"; import LoadingDots from "../../components/loading-dots"; function removeCodeFormatting(code: string): string { return code.replace(/```(?:typescript|javascript|tsx)?\n([\s\S]*?)```/g, '$1').trim(); } export default function Home() { let [status, setStatus] = useState< "initial" | "creating" | "created" | "updating" | "updated" >("initial"); let [prompt, setPrompt] = useState(""); let 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", } ]; let [model, setModel] = useState(models[0].value); let [modification, setModification] = useState(""); let [generatedCode, setGeneratedCode] = useState(""); let [initialAppConfig, setInitialAppConfig] = useState({ model: "", shadcn: true, }); let [ref, scrollTo] = useScrollTo(); let [messages, setMessages] = useState<{ role: string; content: string }[]>( [], ); let loading = status === "creating" || status === "updating"; async function createApp(e: FormEvent) { e.preventDefault(); if (status !== "initial") { scrollTo({ delay: 0.5 }); } setStatus("creating"); setGeneratedCode(""); let res = await fetch("/api/generateCode", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ model, messages: [{ role: "user", content: prompt }], }), }); if (!res.ok) { throw new Error(res.statusText); } if (!res.body) { throw new Error("No response body"); } const reader = res.body.getReader(); let receivedData = ""; while (true) { const { done, value } = await reader.read(); if (done) { break; } receivedData += new TextDecoder().decode(value); const cleanedData = removeCodeFormatting(receivedData); setGeneratedCode(cleanedData); } setMessages([{ role: "user", content: prompt }]); setInitialAppConfig({ model }); setStatus("created"); } useEffect(() => { let el = document.querySelector(".cm-scroller"); if (el && loading) { let end = el.scrollHeight - el.clientHeight; el.scrollTo({ top: end }); } }, [loading, generatedCode]); return (
Powered by Gemini API

Turn your idea
into an app