Open History to see the last commit
1import { useCallback, useEffect, useRef, useState } from 'react'
2import { ImageIcon, Loader2, Plus, Shuffle, X } from 'lucide-react'
3import { useGenerateImage } from '@tanstack/ai-react'
4import type { MediaPrompt } from '@tanstack/ai/client'
5
6import { generateImageFn } from '@/lib/server-functions'
7import { getRandomImagePrompt } from '@/lib/prompts'
8import { IMAGE_MODELS } from '@/lib/models'
9import type { ImageModel } from '@/lib/models'
10import { readMediaFile, toImagePart } from '@/lib/media'
11import type { AttachedMedia } from '@/lib/media'
12
13interface ImageGeneratorProps {
14 onImageGenerated?: (imageUrl: string) => void
15}
16
17/** How the parent asks one card to run: the built prompt, nothing else. */
18type ImageRunner = (prompt: MediaPrompt) => void
19
20function getImageSrc(image: { url?: string; b64Json?: string }): string {
21 if (image.url) return image.url
22 if (image.b64Json) return `data:image/png;base64,${image.b64Json}`
23 return ''
24}
25
26const falModels = IMAGE_MODELS.filter((m) => m.provider === 'fal')
27const geminiModels = IMAGE_MODELS.filter((m) => m.provider === 'gemini')
28const xaiModels = IMAGE_MODELS.filter((m) => m.provider === 'xai')
29const byteplusModels = IMAGE_MODELS.filter((m) => m.provider === 'byteplus')
30
31export default function ImageGenerator({
32 onImageGenerated,
33}: ImageGeneratorProps) {
34 const [prompt, setPrompt] = useState('')
35 const [selectedModel, setSelectedModel] = useState<string>('all')
36 const [images, setImages] = useState<Array<AttachedMedia>>([])
37 /** Whether anything has been generated for the current model selection. */
38 const [submitted, setSubmitted] = useState(false)
39 const [running, setRunning] = useState<Record<string, boolean>>({})
40 const fileInputRef = useRef<HTMLInputElement>(null)
41 // Generation is started from the click, not from an effect on the cards:
42 // a hook only exists inside a mounted component, so the cards mount with
43 // the picker and hand the parent a way to run them.
44 const runnersRef = useRef(new Map<string, ImageRunner>())
45
46 const currentModel = IMAGE_MODELS.find((m) => m.id === selectedModel)
47 const activeModels =
48 selectedModel === 'all'
49 ? IMAGE_MODELS
50 : IMAGE_MODELS.filter((model) => model.id === selectedModel)
51 // Each card owns its own generation, so the form's busy state is the union
52 // of what the cards report.
53 const isLoading = Object.values(running).some(Boolean)
54
55 // When images are attached, send an ordered parts array (text first, then one
56 // image part per attachment). Otherwise send the plain string. Only image-capable
57 // models accept image inputs — unsupported models surface a server error.
58 const buildPrompt = (): MediaPrompt => {
59 if (images.length === 0) return prompt
60 return [
61 { type: 'text', content: prompt },
62 ...images.map((image) => toImagePart(image, { role: 'reference' })),
63 ]
64 }
65
66 const handleImageSelect = async (e: React.ChangeEvent<HTMLInputElement>) => {
67 const files = Array.from(e.target.files ?? [])
68 if (fileInputRef.current) fileInputRef.current.value = ''
69 if (files.length === 0) return
70 const attached = await Promise.all(files.map((file) => readMediaFile(file)))
71 setImages((prev) => [...prev, ...attached])
72 }
73
74 const removeImage = (id: string) => {
75 setImages((prev) => prev.filter((image) => image.id !== id))
76 }
77
78 const handleRunningChange = useCallback((modelId: string, value: boolean) => {
79 setRunning((prev) =>
80 prev[modelId] === value ? prev : { ...prev, [modelId]: value },
81 )
82 }, [])
83
84 const registerRunner = useCallback(
85 (modelId: string, run: ImageRunner | null) => {
86 if (run) runnersRef.current.set(modelId, run)
87 else runnersRef.current.delete(modelId)
88 },
89 [],
90 )
91
92 const handleGenerate = () => {
93 if (!prompt.trim()) return
94 const built = buildPrompt()
95 setSubmitted(true)
96 for (const model of activeModels) {
97 runnersRef.current.get(model.id)?.(built)
98 }
99 }
100
101 return (
102 <div className="space-y-6">
103 <div className="space-y-4">
104 <div>
105 <label className="block text-sm font-medium text-gray-300 mb-2">
106 Model
107 </label>
108 <select
109 value={selectedModel}
110 onChange={(e) => {
111 setSelectedModel(e.target.value)
112 // The results below belong to the models that produced them.
113 setSubmitted(false)
114 setRunning({})
115 }}
116 disabled={isLoading}
117 className="w-full px-4 py-3 bg-gray-800 border border-gray-700 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent disabled:opacity-50"
118 >
119 <option value="all">All Models</option>
120 <optgroup label="fal.ai">
121 {falModels.map((model) => (
122 <option key={model.id} value={model.id}>
123 {model.name}
124 </option>
125 ))}
126 </optgroup>
127 <optgroup label="Gemini">
128 {geminiModels.map((model) => (
129 <option key={model.id} value={model.id}>
130 {model.name}
131 </option>
132 ))}
133 </optgroup>
134 <optgroup label="xAI (direct)">
135 {xaiModels.map((model) => (
136 <option key={model.id} value={model.id}>
137 {model.name}
138 </option>
139 ))}
140 </optgroup>
141 <optgroup label="BytePlus (direct)">
142 {byteplusModels.map((model) => (
143 <option key={model.id} value={model.id}>
144 {model.name}
145 </option>
146 ))}
147 </optgroup>
148 </select>
149 {currentModel && selectedModel !== 'all' && (
150 <p className="mt-1 text-xs text-gray-500">
151 {currentModel.description}
152 </p>
153 )}
154 </div>
155
156 <div>
157 <div className="flex items-center justify-between mb-2">
158 <label className="text-sm font-medium text-gray-300">Prompt</label>
159 <button
160 onClick={() => setPrompt(getRandomImagePrompt())}
161 disabled={isLoading}
162 className="flex items-center gap-1.5 px-3 py-1 text-xs font-medium text-purple-400 hover:text-purple-300 bg-purple-500/10 hover:bg-purple-500/20 rounded-md transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
163 >
164 <Shuffle className="w-3.5 h-3.5" />
165 Shuffle
166 </button>
167 </div>
168 <textarea
169 value={prompt}
170 onChange={(e) => setPrompt(e.target.value)}
171 placeholder="Describe the image you want to generate..."
172 className="w-full px-4 py-3 bg-gray-800 border border-gray-700 rounded-lg text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent resize-none"
173 rows={3}
174 disabled={isLoading}
175 />
176 </div>
177
178 <div>
179 <div className="flex items-center justify-between mb-2">
180 <label className="text-sm font-medium text-gray-300">
181 Reference Images
182 </label>
183 <span className="text-xs text-gray-500">
184 Sent as image prompt parts with role "reference" —
185 accepted by the Gemini multimodal models, xAI Imagine and Seedream
186 </span>
187 </div>
188 <div className="flex flex-wrap gap-2">
189 {images.map((image) => (
190 <div
191 key={image.id}
192 className="relative w-20 h-20 rounded-lg overflow-hidden border border-gray-700"
193 >
194 <img
195 src={image.dataUrl}
196 alt={image.name}
197 className="w-full h-full object-cover"
198 />
199 <button
200 onClick={() => removeImage(image.id)}
201 disabled={isLoading}
202 className="absolute top-1 right-1 p-0.5 bg-gray-900/80 hover:bg-gray-800 rounded-full text-white disabled:opacity-50"
203 aria-label={`Remove ${image.name}`}
204 >
205 <X className="w-3.5 h-3.5" />
206 </button>
207 </div>
208 ))}
209 <button
210 onClick={() => fileInputRef.current?.click()}
211 disabled={isLoading}
212 className="w-20 h-20 flex flex-col items-center justify-center gap-1 border-2 border-dashed border-gray-600 hover:border-gray-500 rounded-lg text-gray-400 hover:text-gray-300 transition-colors disabled:opacity-50"
213 >
214 <Plus className="w-5 h-5" />
215 <span className="text-xs">Add</span>
216 </button>
217 </div>
218 <input
219 ref={fileInputRef}
220 type="file"
221 accept="image/*"
222 multiple
223 onChange={handleImageSelect}
224 className="hidden"
225 />
226 </div>
227
228 <button
229 onClick={handleGenerate}
230 disabled={isLoading || !prompt.trim()}
231 className="w-full px-6 py-3 bg-purple-600 hover:bg-purple-700 disabled:bg-gray-700 disabled:cursor-not-allowed text-white font-medium rounded-lg transition-colors flex items-center justify-center gap-2"
232 >
233 {isLoading ? (
234 <>
235 <Loader2 className="w-5 h-5 animate-spin" />
236 Generating...
237 </>
238 ) : (
239 <>
240 <ImageIcon className="w-5 h-5" />
241 Generate Image
242 </>
243 )}
244 </button>
245 </div>
246
247 <div className="space-y-6">
248 {submitted && (
249 <h3 className="text-lg font-medium text-white">
250 {activeModels.length > 1 ? 'Generated Images' : 'Generated Image'}
251 </h3>
252 )}
253 {activeModels.map((model) => (
254 <ImageModelCard
255 key={model.id}
256 model={model}
257 visible={submitted}
258 showTitle={activeModels.length > 1}
259 onRegister={registerRunner}
260 onRunningChange={handleRunningChange}
261 onImageGenerated={onImageGenerated}
262 />
263 ))}
264 </div>
265 </div>
266 )
267}
268
269/**
270 * One model's generation. `useGenerateImage` owns the request lifecycle, so
271 * the card only decides what to render for each of its states — there is no
272 * per-model status bookkeeping left in the parent.
273 */
274function ImageModelCard({
275 model,
276 visible,
277 showTitle,
278 onRegister,
279 onRunningChange,
280 onImageGenerated,
281}: {
282 model: ImageModel
283 visible: boolean
284 showTitle: boolean
285 onRegister: (modelId: string, run: ImageRunner | null) => void
286 onRunningChange: (modelId: string, running: boolean) => void
287 onImageGenerated?: (imageUrl: string) => void
288}) {
289 const { generate, result, isLoading, error } = useGenerateImage({
290 threadId: `image:${model.id}`,
291 // The model is fixed for this card, so the server function's per-model
292 // switch is picked here rather than being sent as a request field.
293 // `options.signal` is the hook's abort signal — forwarding it lets an
294 // unmount or a `stop()` cancel the request rather than orphan it.
295 fetcher: (input, options) =>
296 generateImageFn({
297 data: { prompt: input.prompt, model: model.id },
298 signal: options?.signal,
299 }),
300 onResult: (generated) => {
301 const image = generated.images[0]
302 if (image) onImageGenerated?.(getImageSrc(image))
303 },
304 })
305
306 const run = useCallback(
307 (prompt: MediaPrompt) => {
308 // Deliberately not `reset()` first: reset stops the client, which clears
309 // the `isLoading` that makes a second `generate()` a no-op — that guard
310 // is what swallows a double-click. The stale result a failed re-run
311 // leaves behind is handled by the render gate below instead.
312 void generate({ prompt })
313 },
314 [generate],
315 )
316
317 useEffect(() => {
318 onRegister(model.id, run)
319 return () => onRegister(model.id, null)
320 }, [model.id, run, onRegister])
321
322 useEffect(() => {
323 onRunningChange(model.id, isLoading)
324 }, [model.id, isLoading, onRunningChange])
325
326 if (!visible) return null
327
328 const image = result?.images[0]
329
330 return (
331 <div className="space-y-2">
332 {showTitle && (
333 <h4 className="text-sm font-medium text-gray-300">{model.name}</h4>
334 )}
335 {isLoading && (
336 <div className="flex items-center gap-2 p-4 bg-gray-800 rounded-lg border border-gray-700">
337 <Loader2 className="w-5 h-5 animate-spin text-purple-400" />
338 <span className="text-gray-400">Generating...</span>
339 </div>
340 )}
341 {error && !isLoading && (
342 <div className="p-4 bg-red-500/10 border border-red-500/20 rounded-lg text-red-400">
343 {error.message}
344 </div>
345 )}
346 {/* `!error`: a failed re-run keeps the previous result on the hook, and
347 it must not render underneath the new error. */}
348 {image && !isLoading && !error && (
349 <>
350 <div className="rounded-lg overflow-hidden border border-gray-700">
351 <img
352 src={getImageSrc(image)}
353 alt={`Generated by ${model.name}`}
354 className="w-full h-auto"
355 />
356 </div>
357 {result?.usage?.unitsBilled != null && (
358 <p className="text-xs text-gray-500">
359 Billed {result.usage.unitsBilled}{' '}
360 {model.provider === 'fal' ? 'fal ' : ''}unit
361 {result.usage.unitsBilled === 1 ? '' : 's'} — multiply by the
362 endpoint unit price for USD cost
363 </p>
364 )}
365 </>
366 )}
367 </div>
368 )
369}