|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useRef } from "react"; |
| 4 | +import { apiFetch } from "@/lib/api"; |
| 5 | + |
| 6 | +const VOICES = [ |
| 7 | + { id: "21m00Tcm4TlvDq8ikWAM", name: "Rachel", gender: "Female" }, |
| 8 | + { id: "EXAVITQu4vr4xnSDxMaL", name: "Sarah", gender: "Female" }, |
| 9 | + { id: "pFZP5JQG7iQjIQuC4Bku", name: "Lily", gender: "Female" }, |
| 10 | + { id: "nPczCjzI2devNBz1zQrb", name: "Brian", gender: "Male" }, |
| 11 | + { id: "JBFqnCBsd6RMkjVDRZzb", name: "George", gender: "Male" }, |
| 12 | +] as const; |
| 13 | + |
| 14 | +interface VoiceSelectorProps { |
| 15 | + value: string; |
| 16 | + onChange: (voiceId: string) => void; |
| 17 | +} |
| 18 | + |
| 19 | +export default function VoiceSelector({ value, onChange }: VoiceSelectorProps) { |
| 20 | + const [playingId, setPlayingId] = useState<string | null>(null); |
| 21 | + const [loadingId, setLoadingId] = useState<string | null>(null); |
| 22 | + const audioRef = useRef<HTMLAudioElement | null>(null); |
| 23 | + const cacheRef = useRef<Record<string, string>>({}); |
| 24 | + |
| 25 | + async function handlePlay(voiceId: string) { |
| 26 | + // Stop current playback |
| 27 | + if (audioRef.current) { |
| 28 | + audioRef.current.pause(); |
| 29 | + audioRef.current = null; |
| 30 | + } |
| 31 | + |
| 32 | + if (playingId === voiceId) { |
| 33 | + setPlayingId(null); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + setLoadingId(voiceId); |
| 38 | + |
| 39 | + try { |
| 40 | + let blobUrl = cacheRef.current[voiceId]; |
| 41 | + |
| 42 | + if (!blobUrl) { |
| 43 | + const res = await apiFetch("/api/voices/preview", { |
| 44 | + method: "POST", |
| 45 | + headers: { "Content-Type": "application/json" }, |
| 46 | + body: JSON.stringify({ voiceId }), |
| 47 | + }); |
| 48 | + |
| 49 | + if (!res.ok) throw new Error("Failed to load preview"); |
| 50 | + |
| 51 | + const blob = await res.blob(); |
| 52 | + blobUrl = URL.createObjectURL(blob); |
| 53 | + cacheRef.current[voiceId] = blobUrl; |
| 54 | + } |
| 55 | + |
| 56 | + const audio = new Audio(blobUrl); |
| 57 | + audio.onended = () => setPlayingId(null); |
| 58 | + audioRef.current = audio; |
| 59 | + await audio.play(); |
| 60 | + setPlayingId(voiceId); |
| 61 | + } catch (err) { |
| 62 | + console.error("Preview failed:", err); |
| 63 | + } finally { |
| 64 | + setLoadingId(null); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <div> |
| 70 | + <label className="block text-sm font-medium text-gray-700 mb-2"> |
| 71 | + Voice |
| 72 | + </label> |
| 73 | + <div className="grid grid-cols-1 gap-2"> |
| 74 | + {VOICES.map((voice) => { |
| 75 | + const isSelected = value === voice.id; |
| 76 | + const isPlaying = playingId === voice.id; |
| 77 | + const isLoading = loadingId === voice.id; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div |
| 81 | + key={voice.id} |
| 82 | + onClick={() => onChange(voice.id)} |
| 83 | + className={`flex items-center justify-between rounded-lg border-2 px-3 py-2.5 cursor-pointer transition-colors ${ |
| 84 | + isSelected |
| 85 | + ? "border-indigo-500 bg-indigo-50" |
| 86 | + : "border-gray-200 hover:border-gray-300 bg-white" |
| 87 | + }`} |
| 88 | + > |
| 89 | + <div className="flex items-center gap-3"> |
| 90 | + <div |
| 91 | + className={`flex h-8 w-8 items-center justify-center rounded-full text-xs font-medium ${ |
| 92 | + isSelected |
| 93 | + ? "bg-indigo-100 text-indigo-700" |
| 94 | + : "bg-gray-100 text-gray-600" |
| 95 | + }`} |
| 96 | + > |
| 97 | + {voice.gender === "Female" ? ( |
| 98 | + <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor"> |
| 99 | + <path strokeLinecap="round" strokeLinejoin="round" d="M15.75 6a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0ZM4.501 20.118a7.5 7.5 0 0 1 14.998 0" /> |
| 100 | + </svg> |
| 101 | + ) : ( |
| 102 | + <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor"> |
| 103 | + <path strokeLinecap="round" strokeLinejoin="round" d="M15.75 6a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0ZM4.501 20.118a7.5 7.5 0 0 1 14.998 0" /> |
| 104 | + </svg> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + <div> |
| 108 | + <p className={`text-sm font-medium ${isSelected ? "text-indigo-900" : "text-gray-900"}`}> |
| 109 | + {voice.name} |
| 110 | + </p> |
| 111 | + <p className={`text-xs ${isSelected ? "text-indigo-600" : "text-gray-500"}`}> |
| 112 | + {voice.gender} |
| 113 | + </p> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <button |
| 118 | + type="button" |
| 119 | + onClick={(e) => { |
| 120 | + e.stopPropagation(); |
| 121 | + handlePlay(voice.id); |
| 122 | + }} |
| 123 | + disabled={isLoading} |
| 124 | + className={`flex h-8 w-8 items-center justify-center rounded-full transition-colors ${ |
| 125 | + isPlaying |
| 126 | + ? "bg-indigo-600 text-white" |
| 127 | + : isSelected |
| 128 | + ? "bg-indigo-200 text-indigo-700 hover:bg-indigo-300" |
| 129 | + : "bg-gray-100 text-gray-600 hover:bg-gray-200" |
| 130 | + } disabled:opacity-50`} |
| 131 | + title={isPlaying ? "Stop" : "Preview voice"} |
| 132 | + > |
| 133 | + {isLoading ? ( |
| 134 | + <div className="h-3.5 w-3.5 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 135 | + ) : isPlaying ? ( |
| 136 | + <svg className="h-3.5 w-3.5" fill="currentColor" viewBox="0 0 24 24"> |
| 137 | + <rect x="6" y="4" width="4" height="16" /> |
| 138 | + <rect x="14" y="4" width="4" height="16" /> |
| 139 | + </svg> |
| 140 | + ) : ( |
| 141 | + <svg className="h-3.5 w-3.5" fill="currentColor" viewBox="0 0 24 24"> |
| 142 | + <path d="M8 5v14l11-7z" /> |
| 143 | + </svg> |
| 144 | + )} |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + ); |
| 148 | + })} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +} |
0 commit comments