ClassyGitshadowzzh

VoicesView.tsx

VoicesView.tsx
import { useMemo, useState } from "react"
import { ArrowUpRight, AudioLines, Heart, Mic2, Pencil, Plus, Search, Trash2 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Badge } from "@/components/ui/badge"
import { AudioPlayer } from "@/components/AudioPlayer"
import { EmptyState } from "@/components/EmptyState"
import { cn } from "@/lib/utils"
import { kindLabels, taskLabels } from "@/lib/format"
import type { Voice, VoiceKind } from "@/types"
export function VoicesView({ voices, loading, busy, onCreate, onEdit, onUse, onFavorite, onDelete }: { voices: Voice[]; loading: boolean; busy: boolean; onCreate: () => void; onEdit: (voice: Voice) => void; onUse: (voice: Voice) => void; onFavorite: (voice: Voice) => void; onDelete: (voice: Voice) => void }) {
  const [query, setQuery] = useState("")
  const [filter, setFilter] = useState<"all" | "favorite" | VoiceKind>("all")
  const items = useMemo(() => voices.filter(voice => (filter === "all" || filter === "favorite" && voice.favorite || voice.kind === filter) && [voice.name, voice.description, ...voice.tags].some(value => value.toLowerCase().includes(query.toLowerCase()))), [voices, query, filter])
  return <div className="space-y-7">
    <div className="flex flex-wrap items-start justify-between gap-4"><div><p className="eyebrow">你的专属声音收藏</p><h1 className="mt-2 text-2xl font-semibold tracking-tight sm:text-3xl">每一种声音,都有自己的个性<span className="text-primary">。</span></h1><p className="mt-3 text-sm text-muted-foreground">管理克隆音色、内置声音和设计预设,让灵感随时开口。</p></div><Button onClick={onCreate}><Plus />创建声音</Button></div>
    <div className="flex flex-wrap items-center justify-between gap-4"><div className="flex max-w-full gap-1 overflow-x-auto rounded-lg border bg-muted/40 p-1">{([{ value: "all", label: "全部声音" }, { value: "favorite", label: "我的收藏" }, ...Object.entries(kindLabels).map(([value, label]) => ({ value, label }))]).map(item => <button key={item.value} type="button" onClick={() => setFilter(item.value as typeof filter)} aria-pressed={filter === item.value} className={cn("shrink-0 rounded-md px-3 py-1.5 text-xs transition-colors", filter === item.value ? "bg-background font-medium shadow-xs" : "text-muted-foreground hover:text-foreground")}>{item.label}</button>)}</div><div className="relative w-full sm:w-64"><Search className="absolute left-3 top-2.5 size-4 text-muted-foreground" /><Input aria-label="搜索声音名称、备注或标签" placeholder="搜索声音、标签…" value={query} onChange={event => setQuery(event.target.value)} className="pl-9" /></div></div>
    <div className="flex items-center justify-between"><span className="text-xs text-muted-foreground">{loading ? "正在读取声音库…" : `共 ${items.length} 个声音`}</span><span className="text-[11px] text-muted-foreground">声音与参数保存在本地服务</span></div>
    {items.length ? <div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">{items.map(voice => <article key={voice.id} className="group flex min-w-0 flex-col rounded-2xl border bg-card p-5 shadow-xs transition-shadow hover:shadow-md">
      <div className="flex items-start justify-between gap-3"><div className={cn("flex size-12 items-center justify-center rounded-2xl", voice.kind === "clone" ? "bg-primary/10 text-primary" : voice.kind === "design" ? "bg-rose-500/10 text-rose-500" : "bg-emerald-500/10 text-emerald-600")}><Mic2 className="size-5" /></div><Button variant="ghost" size="icon-sm" aria-label={voice.favorite ? "取消收藏声音" : "收藏声音"} aria-pressed={voice.favorite} disabled={busy} onClick={() => onFavorite(voice)}><Heart className={cn("size-4 text-muted-foreground", voice.favorite && "fill-primary text-primary")} /></Button></div>
      <div className="mt-4 flex items-center gap-2"><h2 className="truncate font-semibold">{voice.name}</h2><Badge variant="secondary" className="shrink-0 text-[10px] font-normal">{kindLabels[voice.kind]}</Badge></div><p className="mt-2 line-clamp-2 min-h-10 text-xs leading-5 text-muted-foreground">{voice.description || "尚未添加声音备注"}</p>
      <div className="mb-4 mt-3 flex min-h-5 flex-wrap gap-1.5">{voice.tags.map(tag => <span key={tag} className="rounded-md bg-muted px-2 py-0.5 text-[10px] text-muted-foreground">#{tag}</span>)}</div>
      <div className="mt-auto">{voice.audio_url ? <AudioPlayer key={voice.audio_url} src={voice.audio_url} label={`试听声音:${voice.name}`} /> : <div className="flex h-10 items-center gap-2 rounded-lg bg-muted/40 px-3 text-[11px] text-muted-foreground"><AudioLines className="size-4" />{taskLabels[voice.parameters.task]} · 使用后生成试听</div>}<div className="mt-4 flex items-center justify-between gap-2 border-t pt-4"><div className="flex gap-1"><Button type="button" variant="ghost" size="icon-sm" aria-label={`编辑声音:${voice.name}`} onClick={() => onEdit(voice)} disabled={busy}><Pencil className="size-3.5" /></Button><Button type="button" variant="ghost" size="icon-sm" aria-label={`删除声音:${voice.name}`} onClick={() => onDelete(voice)} disabled={busy}><Trash2 className="size-3.5" /></Button></div><Button type="button" variant="secondary" size="sm" onClick={() => onUse(voice)} className="text-xs">使用声音<ArrowUpRight className="size-3.5" /></Button></div></div>
    </article>)}</div> : <div className="rounded-2xl border bg-card"><EmptyState icon={<Mic2 className="size-6" />} title={loading ? "正在读取声音库" : query || filter !== "all" ? "没有找到匹配的声音" : "留住你喜欢的声音"} description={query || filter !== "all" ? "试试其他关键词,或切换声音分类。" : "上传一段参考音频,或者保存一个内置音色与声音设计,建立自己的声音库。"} action={!query && filter === "all" && !loading ? <Button onClick={onCreate} size="sm"><Plus />创建第一个声音</Button> : undefined} /></div>}
  </div>
}