import { updateValByName } from "https://esm.town/v/nbbaier/updateValByName?v=14";
import { basicAuth } from "https://esm.town/v/pomdtr/basicAuth?v=62";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText";
import { chat } from "https://esm.town/v/stevekrouse/openai";
export default basicAuth(async (req) => {
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response(
`<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<link rel="icon" href="https://fav.farm/🔥" />
<title>Val Writer</title>
</head>
<body></body>
<script type="module">
import { App } from "${import.meta.url}";
import { createRoot } from "https://esm.sh/react-dom@18.2.0/client";
import { jsx } from "https://esm.sh/react@18.2.0/jsx-runtime";
createRoot(document.body).render(jsx(App, { }));
</script>
</html>`,
{
headers: {
"content-type": "text/html",
},
},
);
}
});
export function App() {
const example = examples[Math.floor(Math.random() * examples.length)];
const description = example.user;
let code = example.content;
let resp;
return (
<div className="flex p-6 flex-col space-y-4 max-w-2xl mx-auto">
<div>
<h1 className="text-3xl">Val Writer</h1>
<p>Compile your prompt into code</p>
</div>
<form className="flex space-x-2">
<textarea
name="description"
required
className="w-full border-2 rounded-lg p-2"
rows={5}
>
{description}
</textarea>
<button className="bg-purple-500 hover:bg-purple-700 text-white font-bold py-2 px-4 rounded disabled:hidden">
Write
</button>
</form>
<div>
{resp
? (
<iframe
width="100%"
height="400px"
src="https://www.val.town/embed/stevekrouse/valwriter_output"
title="Val Town"
frameborder="0"
allow="web-share"
allowfullscreen
>
</iframe>
)
: null}
<pre>
{resp ? JSON.stringify(resp, null, 2) : code}
</pre>
</div>
</div>
);
}
const examples = [
{
user: "website that shows the current time",
content: `/** @jsxImportSource npm:react */
export default function() {
return <h1>{new Date().toLocaleTimeString()}</h1>;
}`,
},
{
user: `A collaborative poem builder.
It stores each line of the poem in sqlite.