import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Badge } from '@/components/ui/badge' import { Skeleton } from '@/components/ui/skeleton' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' import { FolderOpen, FolderPlus, MoreVertical, Edit, Trash, Plus, Search, TreePine } from 'lucide-react' import { toast } from 'sonner' import { categoryService } from '@/services/categoryService' export default function Categories() { const queryClient = useQueryClient() const [search, setSearch] = useState('') // 获取分类树 const { data: categories, isLoading: categoriesLoading } = useQuery({ queryKey: ['categories-tree'], queryFn: categoryService.getCategoryTree }) // 获取分类统计 const { data: stats, isLoading: statsLoading } = useQuery({ queryKey: ['category-stats'], queryFn: categoryService.getStats }) // 删除分类 const deleteCategoryMutation = useMutation({ mutationFn: categoryService.deleteCategory, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['categories-tree'] }) queryClient.invalidateQueries({ queryKey: ['category-stats'] }) toast.success('分类删除成功') }, onError: (error: any) => { toast.error(error?.message || '删除失败') } }) const handleDeleteCategory = (categoryId: number) => { if (confirm('确定要删除这个分类吗?')) { deleteCategoryMutation.mutate(categoryId) } } const renderCategoryTree = (categories: any[], level = 0) => { return categories?.map((category) => (
0 ? 'ml-6 border-l-4 border-l-primary/20' : ''}`}>
{category.name} {category.isActive ? '启用' : '禁用'} {category.photoCount} 张照片
{category.description && (

{category.description}

)}
编辑 添加子分类 handleDeleteCategory(category.id)} disabled={category.photoCount > 0 || category.children?.length > 0} > 删除
{category.children && category.children.length > 0 && (
{renderCategoryTree(category.children, level + 1)}
)}
)) } return (
{/* 页面头部 */}

分类管理

管理照片分类和相册结构

{/* 统计卡片 */}
总分类数 {statsLoading ? ( ) : (
{stats?.total || 0}
)}
活跃分类 {statsLoading ? ( ) : (
{stats?.active || 0}
)}
顶级分类 {statsLoading ? ( ) : (
{stats?.topLevel || 0}
)}
平均照片数 {statsLoading ? ( ) : (
{stats?.total ? Math.round(Object.values(stats.photoCounts || {}).reduce((a, b) => a + b, 0) / stats.total) : 0}
)}
{/* 搜索栏 */}
setSearch(e.target.value)} className="pl-10" />
{/* 分类列表 */} 分类结构 {categoriesLoading ? (
{[...Array(5)].map((_, i) => (
))}
) : categories?.length ? (
{renderCategoryTree(categories)}
) : (

暂无分类

创建您的第一个分类来组织照片

)}
) }