Open History to see the last commit
1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { useGenerateVideo } from '@tanstack/ai-react'
4import type { UseGenerateVideoReturn } from '@tanstack/ai-react'
5import { fetchServerSentEvents } from '@tanstack/ai-client'
6import { resolveMediaPrompt } from '@tanstack/ai'
7import { generateVideoFn, generateVideoStreamFn } from '../lib/server-fns'
8
9// Persist each variant's lightweight resume snapshot across reloads. For a
10// long video run this keeps the job id around after a reload.
11
12function StreamingVideoGeneration() {
13 const [prompt, setPrompt] = useState('')
14
15 const hookReturn = useGenerateVideo({
16 threadId: 'video:streaming',
17 connection: fetchServerSentEvents('/api/generate/video'),
18 persistence: true,
19 })
20
21 return (
22 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
23 )
24}
25
26function DirectVideoGeneration() {
27 const [prompt, setPrompt] = useState('')
28
29 const hookReturn = useGenerateVideo({
30 threadId: 'video:direct',
31 fetcher: (input) =>
32 generateVideoFn({
33 data: { ...input, prompt: resolveMediaPrompt(input.prompt).text },
34 }),
35 persistence: true,
36 })
37
38 return (
39 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
40 )
41}
42
43function ServerFnVideoGeneration() {
44 const [prompt, setPrompt] = useState('')
45
46 const hookReturn = useGenerateVideo({
47 threadId: 'video:server-fn',
48 fetcher: (input) =>
49 generateVideoStreamFn({
50 data: { ...input, prompt: resolveMediaPrompt(input.prompt).text },
51 }),
52 persistence: true,
53 })
54
55 return (
56 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
57 )
58}
59
60function VideoGenerationUI({
61 prompt,
62 setPrompt,
63 generate,
64 result,
65 jobId,
66 videoStatus,
67 isLoading,
68 error,
69 stop,
70 reset,
71}: UseGenerateVideoReturn & {
72 prompt: string
73 setPrompt: (v: string) => void
74}) {
75 const handleGenerate = () => {
76 if (!prompt.trim()) return
77 generate({ prompt: prompt.trim() })
78 }
79
80 return (
81 <div className="space-y-6">
82 <div className="p-4 bg-yellow-500/10 border border-yellow-500/20 rounded-lg">
83 <p className="text-yellow-400 text-sm">
84 Video generation is experimental and requires an XAI_API_KEY with Grok
85 Imagine access. Generation can take several minutes.
86 </p>
87 </div>
88
89 <div className="space-y-3">
90 <label className="text-sm text-gray-400">Prompt</label>
91 <textarea
92 value={prompt}
93 onChange={(e) => setPrompt(e.target.value)}
94 placeholder="Describe the video you want to generate..."
95 className="w-full rounded-lg border border-orange-500/20 bg-gray-800/50 px-4 py-3 text-sm text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-orange-500/50 resize-none"
96 rows={3}
97 disabled={isLoading}
98 />
99 <div className="flex gap-3">
100 <button
101 onClick={handleGenerate}
102 disabled={!prompt.trim() || isLoading}
103 className="px-6 py-2 bg-orange-600 hover:bg-orange-700 disabled:bg-gray-700 disabled:text-gray-500 text-white rounded-lg text-sm font-medium transition-colors"
104 >
105 {isLoading ? 'Generating...' : 'Generate Video'}
106 </button>
107 {isLoading && (
108 <button
109 onClick={stop}
110 className="px-6 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm font-medium transition-colors"
111 >
112 Cancel
113 </button>
114 )}
115 {result && (
116 <button
117 onClick={reset}
118 className="px-6 py-2 bg-gray-700 hover:bg-gray-600 text-white rounded-lg text-sm font-medium transition-colors"
119 >
120 Clear
121 </button>
122 )}
123 </div>
124 </div>
125
126 {isLoading && (
127 <div className="p-4 bg-gray-800/50 border border-gray-700 rounded-lg space-y-3">
128 {jobId && (
129 <p className="text-sm text-gray-400">
130 Job: <span className="font-mono text-gray-300">{jobId}</span>
131 </p>
132 )}
133 {videoStatus && (
134 <>
135 <p className="text-sm text-gray-400">
136 Status: <span className="text-white">{videoStatus.status}</span>
137 </p>
138 {videoStatus.progress != null && (
139 <div className="w-full bg-gray-700 rounded-full h-2">
140 <div
141 className="bg-orange-500 h-2 rounded-full transition-all"
142 style={{ width: `${videoStatus.progress}%` }}
143 />
144 </div>
145 )}
146 </>
147 )}
148 {!jobId && !videoStatus && (
149 <p className="text-sm text-gray-400">Starting generation...</p>
150 )}
151 </div>
152 )}
153
154 {error && (
155 <div className="p-4 bg-red-500/10 border border-red-500/20 rounded-lg">
156 <p className="text-red-400 text-sm">{error.message}</p>
157 </div>
158 )}
159
160 {result && (
161 <div className="space-y-3">
162 <video
163 src={result.url}
164 controls
165 className="w-full rounded-lg border border-gray-700"
166 />
167 </div>
168 )}
169 </div>
170 )
171}
172
173function VideoGenerationPage() {
174 const [mode, setMode] = useState<'streaming' | 'direct' | 'server-fn'>(
175 'streaming',
176 )
177
178 return (
179 <div className="flex flex-col h-[calc(100vh-72px)] bg-gray-900 text-white">
180 <div className="border-b border-orange-500/20 bg-gray-800 px-6 py-4">
181 <div className="flex items-center justify-between">
182 <div>
183 <h2 className="text-xl font-semibold">Video Generation</h2>
184 <p className="text-sm text-gray-400 mt-1">
185 Generate videos using xAI Grok Imagine (experimental)
186 </p>
187 </div>
188 <div className="flex gap-1 bg-gray-900/50 rounded-lg p-1">
189 <button
190 onClick={() => setMode('streaming')}
191 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
192 mode === 'streaming'
193 ? 'bg-orange-600 text-white'
194 : 'text-gray-400 hover:text-white'
195 }`}
196 >
197 Streaming
198 </button>
199 <button
200 onClick={() => setMode('direct')}
201 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
202 mode === 'direct'
203 ? 'bg-orange-600 text-white'
204 : 'text-gray-400 hover:text-white'
205 }`}
206 >
207 Direct
208 </button>
209 <button
210 onClick={() => setMode('server-fn')}
211 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
212 mode === 'server-fn'
213 ? 'bg-orange-600 text-white'
214 : 'text-gray-400 hover:text-white'
215 }`}
216 >
217 Server Fn
218 </button>
219 </div>
220 </div>
221 </div>
222
223 <div className="flex-1 overflow-y-auto p-6">
224 <div className="max-w-2xl mx-auto">
225 {mode === 'streaming' ? (
226 <StreamingVideoGeneration key="streaming" />
227 ) : mode === 'direct' ? (
228 <DirectVideoGeneration key="direct" />
229 ) : (
230 <ServerFnVideoGeneration key="server-fn" />
231 )}
232 </div>
233 </div>
234 </div>
235 )
236}
237
238export const Route = createFileRoute('/generations/video')({
239 component: VideoGenerationPage,
240})