import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [prompt, setPrompt] = useState("");
const [faviconUrl, setFaviconUrl] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError("");
setFaviconUrl("");
try {
const response = await fetch(`/generate?prompt=${encodeURIComponent(prompt)}`);
if (!response.ok) {
throw new Error(`Failed to generate image: ${response.statusText}`);
}
const data = await response.json();
setFaviconUrl(data.imageUrl);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
<div>
<h1>Favicon Generator</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Enter your favicon prompt"
required
/>
<button type="submit" disabled={loading}>
{loading ? "Generating..." : "Generate Favicon"}
</button>
</form>
{error && <p style={{ color: "red" }}>{error}</p>}
{faviconUrl && (
<div>
<h2>Your Favicon:</h2>
<img src={faviconUrl} alt="Generated Favicon" style={{ width: "32px", height: "32px" }} />
<p>
<a href={faviconUrl} download="favicon.png">
Download Favicon
</a>
</p>
</div>
)}
<p>
<a href={import.meta.url.replace("esm.town", "val.town")}>View Source</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {
client();
}
async function generateImage(prompt: string) {
const FAL_KEY = Deno.env.get("FAL_KEY");
if (!FAL_KEY) {
throw new Error("FAL_KEY environment variable is not set");
}
const response = await fetch("https://fal.run/fal-ai/flux/schnell", {
method: "POST",
headers: {
"Authorization": `Key ${FAL_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: prompt,
image_size: "square",
num_inference_steps: 2,
guidance_scale: 1,
num_images: 1,
}),
});
if (!response.ok) {
const errorText = await response.text();
console.error("fal.ai API error:", response.status, response.statusText, errorText);
throw new Error(`Failed to generate image: ${response.status} ${response.statusText}`);