<script setup lang="ts">
import { ref } from 'vue'
import { Image } from '@/components/ai-elements/image'
import { Loader } from '@/components/ai-elements/loader'
import { PromptInput, PromptInputSubmit, PromptInputTextarea } from '@/components/ai-elements/prompt-input'
const prompt = ref('A futuristic cityscape at sunset')
const imageData = ref<any>(null)
const isLoading = ref(false)
async function handleSubmit() {
if (!prompt.value.trim())
return
const currentPrompt = prompt.value.trim()
prompt.value = ''
setIsLoading(true)
try {
const response = await fetch('/api/image', {
method: 'POST',
body: JSON.stringify({ prompt: currentPrompt }),
headers: {
'Content-Type': 'application/json',
},
})
const data = await response.json()
imageData.value = data
}
catch (error) {
console.error('Error generating image:', error)
}
finally {
setIsLoading(false)
}
}
function setIsLoading(value: boolean) {
isLoading.value = value
}
</script>
<template>
<div class="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]">
<div class="flex flex-col h-full">
<div class="flex-1 overflow-y-auto p-4">
<div v-if="imageData" class="flex justify-center">
<Image
v-bind="imageData"
alt="Generated image"
class="h-[300px] aspect-square border rounded-lg"
/>
</div>
<Loader v-if="isLoading" />
</div>
<PromptInput
class="mt-4 w-full max-w-2xl mx-auto relative"
@submit="handleSubmit"
>
<PromptInputTextarea
v-model="prompt"
placeholder="Describe the image you want to generate..."
class="pr-12"
/>
<PromptInputSubmit
:status="isLoading ? 'submitted' : 'ready'"
:disabled="!prompt.trim()"
class="absolute bottom-1 right-1"
/>
</PromptInput>
</div>
</div>
</template>