import React, { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent } from "@/components/ui/card"; export default function StoryGenerator() { const [childName, setChildName] = useState(""); const [theme, setTheme] = useState(""); const [storyLength, setStoryLength] = useState(""); const [story, setStory] = useState("Enter details to generate a story..."); const [imageUrl, setImageUrl] = useState(null); const [loading, setLoading] = useState(false); const generateStory = async () => { setLoading(true); try { const response = await fetch("/api/generate-story", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ childName, theme, storyLength }), }); if (!response.ok) { throw new Error("Failed to fetch story"); } const data = await response.json(); setStory(data.story); generateImage(data.story); } catch (error) { setStory("Error generating story. Please try again."); } setLoading(false); }; const generateImage = async (storyText) => { try { const response = await fetch("/api/generate-image", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ storyText }), }); if (!response.ok) { throw new Error("Failed to fetch image"); } const data = await response.json(); setImageUrl(data.imageUrl); } catch (error) { console.error("Error generating image:", error); } }; return (

AI-Powered Bedtime Stories

Create magical bedtime stories in seconds! Just enter a name and theme.

setChildName(e.target.value)} />
setTheme(e.target.value)} />
setStoryLength(e.target.value)} />
{story} {imageUrl && (

Generated Illustration

Story Illustration
)}
); }