148 lines
4.9 KiB
TypeScript
Executable File
148 lines
4.9 KiB
TypeScript
Executable File
import { useCallback } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { usePost } from '@fenghuo/client';
|
|
import type { Article } from '@fenghuo/common';
|
|
import { ArticleAction, BatchActionType } from '@/lib/articles/types';
|
|
|
|
interface UseArticleActionsProps {
|
|
selectedArticles: string[];
|
|
batchAction: string;
|
|
resetSelection: () => void;
|
|
setQuickEditId: (id: string | null) => void;
|
|
}
|
|
|
|
interface UseArticleActionsReturn {
|
|
handleQuickAction: (articleId: string, action: string) => void;
|
|
handleBatchAction: () => void;
|
|
handleArticleAction: (articleId: string, action: ArticleAction) => void;
|
|
handleQuickEditSave: (articleId: string, updates: Partial<Article>) => void;
|
|
}
|
|
|
|
export function useArticleActions({
|
|
selectedArticles,
|
|
batchAction,
|
|
resetSelection,
|
|
setQuickEditId,
|
|
}: UseArticleActionsProps): UseArticleActionsReturn {
|
|
const router = useRouter();
|
|
|
|
// 后端 Mutations
|
|
const { update, softDeleteByIds } = usePost();
|
|
|
|
// 快速操作(发布/取消发布)
|
|
const handleQuickAction = useCallback(
|
|
(articleId: string, action: string) => {
|
|
// 根据不同操作调用不同后端接口
|
|
switch (action) {
|
|
case 'publish':
|
|
update.mutate({
|
|
where: { id: articleId },
|
|
data: { status: 'published', publishedAt: new Date() },
|
|
});
|
|
break;
|
|
case 'draft':
|
|
update.mutate({
|
|
where: { id: articleId },
|
|
data: { status: 'draft' },
|
|
});
|
|
break;
|
|
case 'archive':
|
|
update.mutate({
|
|
where: { id: articleId },
|
|
data: { status: 'archived' },
|
|
});
|
|
break;
|
|
case 'delete':
|
|
softDeleteByIds.mutate({ ids: [articleId], data: { status: 'deleted' } });
|
|
break;
|
|
default:
|
|
console.warn('未知快速操作', action);
|
|
}
|
|
},
|
|
[update, softDeleteByIds],
|
|
);
|
|
|
|
// 批量操作
|
|
const handleBatchAction = useCallback(() => {
|
|
if (!batchAction || selectedArticles.length === 0) return;
|
|
|
|
switch (batchAction as BatchActionType) {
|
|
case 'publish':
|
|
update.mutate({
|
|
where: { id: { in: selectedArticles } } as any,
|
|
data: { status: 'published', publishedAt: new Date() },
|
|
} as any);
|
|
break;
|
|
case 'draft':
|
|
update.mutate({
|
|
where: { id: { in: selectedArticles } } as any,
|
|
data: { status: 'draft' },
|
|
} as any);
|
|
break;
|
|
case 'archive':
|
|
update.mutate({
|
|
where: { id: { in: selectedArticles } } as any,
|
|
data: { status: 'archived' },
|
|
} as any);
|
|
break;
|
|
case 'delete':
|
|
softDeleteByIds.mutate({ ids: selectedArticles, data: { status: 'deleted' } });
|
|
break;
|
|
default:
|
|
console.warn('未知批量操作', batchAction);
|
|
}
|
|
|
|
resetSelection();
|
|
}, [batchAction, selectedArticles, update, softDeleteByIds, resetSelection]);
|
|
|
|
// 单个文章操作
|
|
const handleArticleAction = useCallback((articleId: string, action: ArticleAction) => {
|
|
// 根据操作类型处理
|
|
switch (action) {
|
|
case 'edit':
|
|
// 跳转到编辑页面
|
|
router.push(`/editor?id=${articleId}`);
|
|
break;
|
|
case 'view':
|
|
// 跳转到查看页面
|
|
console.log('跳转到查看页面');
|
|
break;
|
|
case 'preview':
|
|
// 打开预览窗口
|
|
console.log('打开预览窗口');
|
|
break;
|
|
case 'delete':
|
|
softDeleteByIds.mutate({ ids: [articleId], data: { status: 'deleted' } });
|
|
break;
|
|
case 'duplicate':
|
|
// 复制文章
|
|
console.log('复制文章');
|
|
break;
|
|
case 'restore':
|
|
// 恢复文章
|
|
console.log('恢复文章');
|
|
break;
|
|
default:
|
|
console.log('未知操作');
|
|
}
|
|
}, [router, softDeleteByIds]);
|
|
|
|
// 快速编辑保存
|
|
const handleQuickEditSave = useCallback(
|
|
(articleId: string, updates: Partial<Article>) => {
|
|
update.mutate({
|
|
where: { id: articleId },
|
|
data: updates as any,
|
|
});
|
|
setQuickEditId(null);
|
|
},
|
|
[update, setQuickEditId],
|
|
);
|
|
|
|
return {
|
|
handleQuickAction,
|
|
handleBatchAction,
|
|
handleArticleAction,
|
|
handleQuickEditSave,
|
|
};
|
|
}
|