|
| 1 | +import { useState } from 'react' |
| 2 | +import { |
| 3 | + useGenerateImage, |
| 4 | + fetchServerSentEvents, |
| 5 | + fetchHttpStream, |
| 6 | +} from '@tanstack/ai-react' |
| 7 | +import { generateImageFn } from '@/lib/server-functions' |
| 8 | +import type { ImageGenerationResult } from '@tanstack/ai' |
| 9 | +import type { Mode, Provider } from '@/lib/types' |
| 10 | + |
| 11 | +interface ImageGenUIProps { |
| 12 | + provider: Provider |
| 13 | + mode: Mode |
| 14 | + testId?: string |
| 15 | + aimockPort?: number |
| 16 | +} |
| 17 | + |
| 18 | +export function ImageGenUI({ |
| 19 | + provider, |
| 20 | + mode, |
| 21 | + testId, |
| 22 | + aimockPort, |
| 23 | +}: ImageGenUIProps) { |
| 24 | + const [prompt, setPrompt] = useState('') |
| 25 | + |
| 26 | + const connectionOptions = () => { |
| 27 | + const body = { provider, numberOfImages: 1, testId, aimockPort } |
| 28 | + |
| 29 | + if (mode === 'sse') { |
| 30 | + return { connection: fetchServerSentEvents('/api/image'), body } |
| 31 | + } |
| 32 | + if (mode === 'http-stream') { |
| 33 | + return { connection: fetchHttpStream('/api/image/stream'), body } |
| 34 | + } |
| 35 | + return { |
| 36 | + fetcher: async (input: { prompt: string }) => { |
| 37 | + return generateImageFn({ |
| 38 | + data: { |
| 39 | + prompt: input.prompt, |
| 40 | + provider, |
| 41 | + numberOfImages: 1, |
| 42 | + aimockPort, |
| 43 | + testId, |
| 44 | + }, |
| 45 | + }) as Promise<ImageGenerationResult> |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const { generate, result, isLoading, error, status } = |
| 51 | + useGenerateImage(connectionOptions()) |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="p-4 space-y-4"> |
| 55 | + <div className="flex gap-2"> |
| 56 | + <input |
| 57 | + data-testid="prompt-input" |
| 58 | + type="text" |
| 59 | + value={prompt} |
| 60 | + onChange={(e) => setPrompt(e.target.value)} |
| 61 | + placeholder="Describe the image..." |
| 62 | + className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm" |
| 63 | + /> |
| 64 | + <button |
| 65 | + data-testid="generate-button" |
| 66 | + onClick={() => generate({ prompt })} |
| 67 | + disabled={!prompt.trim() || isLoading} |
| 68 | + className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50" |
| 69 | + > |
| 70 | + Generate |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + <div data-testid="generation-status"> |
| 74 | + {status === 'idle' |
| 75 | + ? 'idle' |
| 76 | + : isLoading |
| 77 | + ? 'loading' |
| 78 | + : error |
| 79 | + ? 'error' |
| 80 | + : result |
| 81 | + ? 'complete' |
| 82 | + : 'idle'} |
| 83 | + </div> |
| 84 | + {error && ( |
| 85 | + <div data-testid="generation-error" className="text-red-400 text-sm"> |
| 86 | + {error.message} |
| 87 | + </div> |
| 88 | + )} |
| 89 | + {result && ( |
| 90 | + <div className="grid grid-cols-2 gap-4"> |
| 91 | + {result.images.map((img, i) => ( |
| 92 | + <img |
| 93 | + key={i} |
| 94 | + data-testid="generated-image" |
| 95 | + src={img.url || `data:image/png;base64,${img.b64Json}`} |
| 96 | + alt={`Generated ${i + 1}`} |
| 97 | + className="rounded border border-gray-700" |
| 98 | + /> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ) |
| 104 | +} |
0 commit comments