import { useState } from "react"; import { videoVoicesApi } from "../../api/endpoints"; import type { VideoVoicesResponse } from "../../api/types"; import { ConfirmButton, errorMessage } from "../ui"; import { Premium, PlayButton, useAudio } from "./parts"; const cap = (s?: string | null) => (s ? s[0].toUpperCase() + s.slice(1) : ""); /** The built-in voices to add to "My voices", and this workspace's custom voices. */ export function VoiceLibrary({ voices, premium, onChange, onLocked, full = false, part, customKind }: { voices: VideoVoicesResponse; premium: boolean; onChange: () => void; onLocked: () => void; /** My voices is at its cap: nothing more can be added until one is removed. */ full?: boolean; /** Just one part, with no heading of its own (the caller titles it): the custom voices or the built-in library. */ part?: "custom" | "library"; /** With part "custom": only cloned or only designed custom voices. */ customKind?: "clone" | "designed"; }) { const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const { playing, play } = useAudio(); const shown = voices.library; const custom = voices.custom.filter((c) => !customKind || (customKind === "clone") === (c.source === "clone")); async function act(key: string, fn: () => Promise) { setBusy(key); setError(null); try { await fn(); onChange(); } catch (e) { setError(errorMessage(e)); } finally { setBusy(null); } } async function playCustom(id: number, url: string | null) { if (url) return play(`c${id}`, url); const res = await videoVoicesApi.customPreview(id).catch(() => null); if (res?.preview_url) play(`c${id}`, res.preview_url); else setError("The preview is not ready yet. Try again in a minute."); } return (
{part !== "library" && custom.length > 0 && (
{!part &&

Your custom voices

}
{custom.map((c) => (
playCustom(c.id, c.preview_url)} />
{c.name} {c.source === "clone" ? "Cloned" : "Designed"}
{c.saved ? ( ) : ( )} act(`d${c.id}`, () => videoVoicesApi.removeCustom(c.id))}>Delete
))}
)} {part === "custom" && error &&

{error}

} {part !== "custom" && (
{error &&

{error}

}
{shown.map((v) => (
play(v.voice_id, v.preview_url)} />
{v.name} {[cap(v.gender), cap(v.accent), cap(v.age)].filter(Boolean).join(" • ")} {v.description ? ` - ${v.description}` : ""}
{v.premium && } {v.saved ? ( ) : ( )}
))} {shown.length === 0 &&

No voices in the library right now.

}
)}
); }