refactor(asset-ops): 重写主入口为组合层,保持导出兼容

This commit is contained in:
2026-07-27 12:53:34 +08:00
parent 464b4a392d
commit 646c5cebac
@@ -1,3 +1,9 @@
/**
* 素材操作 Hook(入口)
* 组合各子模块,保持导出不变
*
* 子模块位于 ./asset-operations/
*/
import { useState, useCallback } from "react"
import { useQueryClient } from "@tanstack/react-query"
import { useSingleOperations, useBatchHelpers } from "./asset-operations/useSingleOperations"
@@ -7,8 +13,8 @@ import {
useBatchClassify,
useBatchMark,
} from "./asset-operations/batchOperations"
import type { SmartViewType } from "../components/BatchMarkModal"
import type { BatchOperationResult } from "@/api/assets"
import type { SmartViewType } from "../components/BatchMarkModal"
interface UseAssetOperationsProps {
selectedIds: Set<string>
@@ -17,104 +23,62 @@ interface UseAssetOperationsProps {
export function useAssetOperations({ selectedIds, setSelectedIds }: UseAssetOperationsProps) {
const queryClient = useQueryClient()
const { invalidateAssets, showOperationResult: showResultBase } = useBatchHelpers(
queryClient,
setSelectedIds,
)
/* ── 诊断状态 ── */
const [diagnosingId, setDiagnosingId] = useState<string | null>(null)
/* ── 批量操作弹窗状态 ── */
const [tagModalOpen, setTagModalOpen] = useState(false)
const [classifyModalOpen, setClassifyModalOpen] = useState(false)
const [markModalOpen, setMarkModalOpen] = useState(false)
const [resultDrawerOpen, setResultDrawerOpen] = useState(false)
/* ── 批量打标签表单 ── */
const [batchTagInput, setBatchTagInput] = useState("")
const [batchTags, setBatchTags] = useState<string[]>([])
const [tagMode, setTagMode] = useState<"add" | "replace">("add")
/* ── 批量改分类表单 ── */
const [batchCategory, setBatchCategory] = useState("")
/* ── 批量智能标记表单 ── */
const [batchSmartView, setBatchSmartView] = useState<SmartViewType>("recommended")
/* ── 操作结果 ── */
const [resultDrawerOpen, setResultDrawerOpen] = useState(false)
const [operationResult, setOperationResult] = useState<BatchOperationResult | null>(null)
const [operationTitle, setOperationTitle] = useState("")
/* ── 批量操作 loading ── */
const [batchLoading, setBatchLoading] = useState(false)
/* ── 操作结果展示 ── */
const showOperationResult = useCallback(
(result: BatchOperationResult, title: string, clearSelection = true) => {
showResultBase(
result,
title,
setOperationResult,
setOperationTitle,
setResultDrawerOpen,
clearSelection,
)
},
[showResultBase],
)
/* ── 单个操作 ── */
const { handleDiagnose: handleDiagnoseRaw, handleSingleDelete } = useSingleOperations({
selectedIds,
setSelectedIds,
invalidateAssets,
})
// 包装一下,加上 diagnosingId 状态
const handleDiagnose = useCallback(
(asset: Parameters<typeof handleDiagnoseRaw>[0]) => handleDiagnoseRaw(asset, setDiagnosingId),
async (asset: Parameters<typeof handleDiagnoseRaw>[0]) => {
setDiagnosingId(asset.id)
try {
await handleDiagnoseRaw(asset)
} finally {
setDiagnosingId(null)
}
},
[handleDiagnoseRaw],
)
/* ── 批量操作 deps ── */
const batchDeps = {
/* ── 批量操作辅助 ── */
const { invalidateAssets } = useBatchHelpers({ queryClient, setSelectedIds })
// 包装 showResult 适配子模块的接口
const showResult = useCallback(
(result: BatchOperationResult, title: string, clearSelection = true) => {
setOperationResult(result)
setOperationTitle(title)
setResultDrawerOpen(true)
if (clearSelection) setSelectedIds(new Set())
},
[setSelectedIds],
)
/* ── 批量操作 ── */
const { batchLoading: deleteLoading, handleBatchDelete } = useBatchDelete({
selectedIds,
invalidateAssets,
showResult: showOperationResult,
setLoading: setBatchLoading,
queryClient,
}
/* ── 批量删除 ── */
const handleBatchDelete = useBatchDelete(batchDeps)
/* ── 批量打标签 ── */
const { handleBatchTag, handleTagInputKeyDown, removeBatchTag } = useBatchTag({
...batchDeps,
batchTags,
setBatchTags,
tagMode,
setTagMode,
setTagModalOpen,
setBatchTagInput,
batchTagInput,
showResult,
})
/* ── 批量改分类 ── */
const handleBatchClassify = useBatchClassify({
...batchDeps,
batchCategory,
setBatchCategory,
setClassifyModalOpen,
})
const tagResult = useBatchTag({ selectedIds, queryClient, showResult })
const classifyResult = useBatchClassify({ selectedIds, queryClient, showResult })
const markResult = useBatchMark({ selectedIds, queryClient, showResult })
/* ── 批量智能标记 ── */
const handleBatchMark = useBatchMark({
...batchDeps,
batchSmartView,
setBatchSmartView,
setMarkModalOpen,
})
// 取任一批量操作的 loading 状态(任意一个在加载都算加载中)
const batchLoading =
deleteLoading || tagResult.batchLoading || classifyResult.batchLoading || markResult.batchLoading
/* ── 关闭结果 Drawer ── */
const handleResultDrawerClose = useCallback(() => {
@@ -131,29 +95,29 @@ export function useAssetOperations({ selectedIds, setSelectedIds }: UseAssetOper
// 批量操作 loading
batchLoading,
// 批量打标签
tagModalOpen,
setTagModalOpen,
batchTagInput,
setBatchTagInput,
batchTags,
setBatchTags,
tagMode,
setTagMode,
handleBatchTag,
handleTagInputKeyDown,
removeBatchTag,
tagModalOpen: tagResult.tagModalOpen,
setTagModalOpen: tagResult.setTagModalOpen,
batchTagInput: tagResult.batchTagInput,
setBatchTagInput: tagResult.setBatchTagInput,
batchTags: tagResult.batchTags,
setBatchTags: tagResult.setBatchTags,
tagMode: tagResult.tagMode,
setTagMode: tagResult.setTagMode,
handleBatchTag: tagResult.handleBatchTag,
handleTagInputKeyDown: tagResult.handleTagInputKeyDown,
removeBatchTag: tagResult.removeBatchTag,
// 批量改分类
classifyModalOpen,
setClassifyModalOpen,
batchCategory,
setBatchCategory,
handleBatchClassify,
classifyModalOpen: classifyResult.classifyModalOpen,
setClassifyModalOpen: classifyResult.setClassifyModalOpen,
batchCategory: classifyResult.batchCategory,
setBatchCategory: classifyResult.setBatchCategory,
handleBatchClassify: classifyResult.handleBatchClassify,
// 批量智能标记
markModalOpen,
setMarkModalOpen,
batchSmartView,
setBatchSmartView,
handleBatchMark,
markModalOpen: markResult.markModalOpen,
setMarkModalOpen: markResult.setMarkModalOpen,
batchSmartView: markResult.batchSmartView as SmartViewType,
setBatchSmartView: markResult.setBatchSmartView,
handleBatchMark: markResult.handleBatchMark,
// 批量删除
handleBatchDelete,
// 操作结果