6437b96e54
CI/CD Pipeline / Canary Release to Production (push) Has been skipped
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Validate - Code Quality (push) Failing after 3m17s
CI/CD Pipeline / Validate - Type Check (mypy) (push) Successful in 1m8s
CI/CD Pipeline / Validate - Migration (alembic) (push) Successful in 1m20s
CI/CD Pipeline / Unit Tests (push) Failing after 5m11s
CI/CD Pipeline / Integration Tests (push) Successful in 3m7s
CI/CD Pipeline / Frontend Lint (push) Successful in 55s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 2m8s
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Successful in 13m32s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 2m27s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 5m42s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 1m33s
CI/CD Pipeline / Staging E2E Tests (push) Successful in 8m18s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 11m0s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (push) Failing after 28s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
107 lines
2.9 KiB
TypeScript
107 lines
2.9 KiB
TypeScript
import { useState, useCallback } from "react"
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
|
import { message } from "antd"
|
|
import { createAssetLibrary, deleteAssetLibrary } from "@/api/assets"
|
|
import type { AssetKind, LibraryItem } from "../types"
|
|
|
|
/**
|
|
* 视频库管理 Hook
|
|
* 封装视频库的创建、删除操作,以及新建弹窗的表单状态
|
|
*/
|
|
interface UseLibraryManagementProps {
|
|
libraries: LibraryItem[]
|
|
activeLibId: string
|
|
setActiveLibId: (id: string) => void
|
|
effectiveLibId: string
|
|
}
|
|
|
|
export function useLibraryManagement({
|
|
libraries,
|
|
setActiveLibId,
|
|
effectiveLibId,
|
|
}: UseLibraryManagementProps) {
|
|
const queryClient = useQueryClient()
|
|
|
|
/* ── 状态 ── */
|
|
const [createModalOpen, setCreateModalOpen] = useState(false)
|
|
const [newLibName, setNewLibName] = useState("")
|
|
const [newLibKind, setNewLibKind] = useState<AssetKind>("video")
|
|
|
|
/* ── Mutations ── */
|
|
const createLibMutation = useMutation({
|
|
mutationFn: createAssetLibrary,
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["asset-libraries"] })
|
|
message.success("视频库创建成功")
|
|
},
|
|
onError: () => {
|
|
message.error("创建视频库失败")
|
|
},
|
|
})
|
|
|
|
const deleteLibMutation = useMutation({
|
|
mutationFn: deleteAssetLibrary,
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["asset-libraries"] })
|
|
message.success("视频库已删除")
|
|
},
|
|
onError: () => {
|
|
message.error("删除视频库失败")
|
|
},
|
|
})
|
|
|
|
/* ── 新建视频库 ── */
|
|
const handleCreateLibrary = useCallback(async () => {
|
|
if (!newLibName.trim()) {
|
|
message.warning("请输入视频库名称")
|
|
return
|
|
}
|
|
try {
|
|
const newLib = await createLibMutation.mutateAsync({
|
|
name: newLibName.trim(),
|
|
kind: newLibKind,
|
|
})
|
|
setActiveLibId(newLib.id)
|
|
setCreateModalOpen(false)
|
|
setNewLibName("")
|
|
setNewLibKind("video")
|
|
} catch {
|
|
// error handled in mutation
|
|
}
|
|
}, [newLibName, newLibKind, createLibMutation, setActiveLibId])
|
|
|
|
/* ── 删除视频库 ── */
|
|
const handleDeleteLibrary = useCallback(
|
|
async (id: string) => {
|
|
try {
|
|
await deleteLibMutation.mutateAsync(id)
|
|
if (effectiveLibId === id) {
|
|
const remaining = libraries.filter((l) => l.id !== id)
|
|
if (remaining.length > 0) setActiveLibId(remaining[0].id)
|
|
else setActiveLibId("")
|
|
}
|
|
} catch {
|
|
// error handled in mutation
|
|
}
|
|
},
|
|
[deleteLibMutation, effectiveLibId, libraries, setActiveLibId],
|
|
)
|
|
|
|
return {
|
|
// 弹窗状态
|
|
createModalOpen,
|
|
setCreateModalOpen,
|
|
// 表单状态
|
|
newLibName,
|
|
setNewLibName,
|
|
newLibKind,
|
|
setNewLibKind,
|
|
// Mutations
|
|
isCreating: createLibMutation.isPending,
|
|
isDeleting: deleteLibMutation.isPending,
|
|
// Handlers
|
|
handleCreateLibrary,
|
|
handleDeleteLibrary,
|
|
}
|
|
}
|