135 lines
3.6 KiB
TypeScript
Executable File
135 lines
3.6 KiB
TypeScript
Executable File
import { useCallback } from "react"
|
|
import { message } from "antd"
|
|
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
|
import type { CreateEditPlanClipRequest, UpdateEditPlanClipRequest } from "@/api/template-editor"
|
|
import {
|
|
createEditPlanClip,
|
|
updateEditPlanClip,
|
|
deleteEditPlanClip,
|
|
batchDeleteEditPlanClips,
|
|
} from "@/api/template-editor"
|
|
import { useClipReorder } from "./useClipReorder"
|
|
import { useClipImport } from "./useClipImport"
|
|
|
|
const QUERY_KEY = "editPlanClips"
|
|
|
|
interface UseEditPlanClipMutationsOptions {
|
|
planId: string | undefined
|
|
selectedClipId: string | null
|
|
setSelectedClipId: (id: string | null) => void
|
|
clipsLength: number
|
|
}
|
|
|
|
export function useEditPlanClipMutations({
|
|
planId,
|
|
selectedClipId,
|
|
setSelectedClipId,
|
|
clipsLength,
|
|
}: UseEditPlanClipMutationsOptions) {
|
|
const queryClient = useQueryClient()
|
|
|
|
const invalidate = () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, planId] })
|
|
}
|
|
|
|
const createMutation = useMutation({
|
|
mutationFn: (data: CreateEditPlanClipRequest) => createEditPlanClip(planId!, data),
|
|
onSuccess: () => {
|
|
invalidate()
|
|
message.success("片段已添加")
|
|
},
|
|
onError: () => {
|
|
message.error("添加片段失败")
|
|
},
|
|
})
|
|
|
|
const addClip = useCallback(
|
|
(data: Omit<CreateEditPlanClipRequest, "order"> & { order?: number }) => {
|
|
if (!planId) return
|
|
const order = data.order ?? clipsLength
|
|
createMutation.mutate({ ...data, order })
|
|
},
|
|
[planId, clipsLength, createMutation],
|
|
)
|
|
|
|
const updateMutation = useMutation({
|
|
mutationFn: ({ clipId, data }: { clipId: string; data: UpdateEditPlanClipRequest }) =>
|
|
updateEditPlanClip(planId!, clipId, data),
|
|
onSuccess: () => {
|
|
invalidate()
|
|
},
|
|
onError: () => {
|
|
message.error("更新片段失败")
|
|
},
|
|
})
|
|
|
|
const updateClip = useCallback(
|
|
(clipId: string, data: UpdateEditPlanClipRequest) => {
|
|
if (!planId) return
|
|
updateMutation.mutate({ clipId, data })
|
|
},
|
|
[planId, updateMutation],
|
|
)
|
|
|
|
const deleteMutation = useMutation({
|
|
mutationFn: (clipId: string) => deleteEditPlanClip(planId!, clipId),
|
|
onSuccess: () => {
|
|
invalidate()
|
|
message.success("片段已删除")
|
|
},
|
|
onError: () => {
|
|
message.error("删除片段失败")
|
|
},
|
|
})
|
|
|
|
const removeClip = useCallback(
|
|
(clipId: string) => {
|
|
if (!planId) return
|
|
if (selectedClipId === clipId) {
|
|
setSelectedClipId(null)
|
|
}
|
|
deleteMutation.mutate(clipId)
|
|
},
|
|
[planId, selectedClipId, setSelectedClipId, deleteMutation],
|
|
)
|
|
|
|
const batchDeleteMutation = useMutation({
|
|
mutationFn: (clipIds: string[]) => batchDeleteEditPlanClips(planId!, clipIds),
|
|
onSuccess: (res) => {
|
|
invalidate()
|
|
message.success(`已删除 ${res.deleted_count} 个片段`)
|
|
},
|
|
onError: () => {
|
|
message.error("批量删除失败")
|
|
},
|
|
})
|
|
|
|
const batchRemoveClips = useCallback(
|
|
(clipIds: string[]) => {
|
|
if (!planId || clipIds.length === 0) return
|
|
if (selectedClipId && clipIds.includes(selectedClipId)) {
|
|
setSelectedClipId(null)
|
|
}
|
|
batchDeleteMutation.mutate(clipIds)
|
|
},
|
|
[planId, selectedClipId, setSelectedClipId, batchDeleteMutation],
|
|
)
|
|
|
|
const { reorderClips, isReordering } = useClipReorder(planId)
|
|
const { importFromAssets, isImporting } = useClipImport(planId)
|
|
|
|
return {
|
|
addClip,
|
|
updateClip,
|
|
removeClip,
|
|
batchRemoveClips,
|
|
reorderClips,
|
|
importFromAssets,
|
|
isCreating: createMutation.isPending,
|
|
isUpdating: updateMutation.isPending,
|
|
isDeleting: deleteMutation.isPending,
|
|
isReordering,
|
|
isImporting,
|
|
}
|
|
}
|