Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

VoiceDesignUI.tsx

158 lines4.6 KB

1import { useState } from 'react'
2import type { Provider } from '@/lib/types'
3​
4interface VoiceDesignUIProps {
5 provider: Provider
6 testId?: string
7 aimockPort?: number
8}
9​
10interface DesignedVoice {
11 voiceId: string
12 audio?: string
13 contentType?: string
14 duration?: number
15 saved?: boolean
17​
18interface VoiceApiResult {
19 model: string
20 previewText?: string
21 voices: Array<DesignedVoice>
23​
24/**
25 * Minimal voice-design harness page. `generateVoice()` is Promise-based, so a
26 * single fetch to `/api/voice` is the whole flow — no connection-adapter/mode
27 * variants, the same shape as the embedding page.
28 *
29 * The "save to library" checkbox drives the two-step half of the ElevenLabs
30 * adapter: with it ticked the first candidate comes back with a library voice
31 * id and `saved` set, which is what `data-saved` reports.
32 */
33export function VoiceDesignUI({
34 provider,
35 testId,
36 aimockPort,
37}: VoiceDesignUIProps) {
38 const [prompt, setPrompt] = useState('')
39 const [name, setName] = useState('')
40 const [save, setSave] = useState(false)
41 const [isLoading, setIsLoading] = useState(false)
42 const [error, setError] = useState<string | null>(null)
43 const [result, setResult] = useState<VoiceApiResult | null>(null)
44​
45 const handleGenerate = async () => {
46 setIsLoading(true)
47 setError(null)
48 setResult(null)
49 try {
50 const res = await fetch('/api/voice', {
51 method: 'POST',
52 headers: { 'Content-Type': 'application/json' },
53 body: JSON.stringify({
54 provider,
55 prompt,
56 ...(save ? { name: name || 'E2E Voice' } : {}),
57 testId,
58 aimockPort,
59 }),
60 })
61 const data: unknown = await res.json()
62 if (!res.ok) {
63 const message =
64 data && typeof data === 'object' && 'error' in data
65 ? String(data.error)
66 : `HTTP ${res.status}`
67 throw new Error(message)
68 }
69 setResult(data as VoiceApiResult)
70 } catch (err) {
71 setError(err instanceof Error ? err.message : 'An error occurred')
72 } finally {
73 setIsLoading(false)
74 }
75 }
76​
77 return (
78 <div className="p-4 space-y-4">
79 <div className="flex gap-2">
80 <textarea
81 data-testid="prompt-input"
82 value={prompt}
83 onChange={(e) => setPrompt(e.target.value)}
84 placeholder="Describe the voice..."
85 rows={3}
86 className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
87 />
88 <button
89 data-testid="generate-button"
90 onClick={handleGenerate}
91 disabled={!prompt.trim() || isLoading}
92 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
93 >
94 Design
95 </button>
96 </div>
97 <label className="flex items-center gap-2 text-sm text-gray-300">
98 <input
99 data-testid="save-voice-toggle"
100 type="checkbox"
101 checked={save}
102 onChange={(e) => setSave(e.target.checked)}
103 />
104 Save to voice library
105 </label>
106 {save && (
107 <input
108 data-testid="voice-name-input"
109 value={name}
110 onChange={(e) => setName(e.target.value)}
111 placeholder="Voice name"
112 className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
113 />
114 )}
115 <div data-testid="generation-status">
116 {isLoading ? 'loading' : error ? 'error' : result ? 'complete' : 'idle'}
117 </div>
118 {error && (
119 <div data-testid="generation-error" className="text-red-400 text-sm">
120 {error}
121 </div>
122 )}
123 {result && (
124 <div className="space-y-2">
125 <div data-testid="voice-model" className="text-gray-400 text-sm">
126 {result.model}
127 </div>
128 {result.previewText && (
129 <div
130 data-testid="voice-preview-text"
131 className="text-gray-400 text-sm"
132 >
133 {result.previewText}
134 </div>
135 )}
136 {result.voices.map((voice) => (
137 <div
138 key={voice.voiceId}
139 data-testid="designed-voice"
140 data-voice-id={voice.voiceId}
141 data-saved={voice.saved ? 'true' : 'false'}
142 className="text-gray-200 text-sm"
143 >
144 {voice.voiceId}
145 {voice.audio && (
146 <audio
147 data-testid="voice-preview-audio"
148 controls
149 src={`data:${voice.contentType ?? 'audio/mpeg'};base64,${voice.audio}`}
150 />
151 )}
152 </div>
153 ))}
154 </div>
155 )}
156 </div>
157 )