/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useEffectEvent } from '../../../../hooks/common/useEffectEvent';
import React, { useState, useEffect } from 'react';
import { Modal, Select, Typography, Spin } from '@/components/ui';
import { API, showError } from '../../../../helpers';
const { Text } = Typography;
const BatchCategoryModal = ({
visible,
onClose,
selectedModels,
onConfirm,
t,
}) => {
const [selectedCategories, setSelectedCategories] = useState([]);
const [loading, setLoading] = useState(false);
const [categoriesLoading, setCategoriesLoading] = useState(false);
const [level1Categories, setLevel1Categories] = useState([]);
const [level2Categories, setLevel2Categories] = useState([]);
// 加载分类数据
const loadCategories = async () => {
setCategoriesLoading(true);
try {
const res = await API.get('/api/model_categories/flat');
const { success, data, message } = res.data;
if (success) {
const categoriesData = data || [];
// 分离一级和二级分类,只保留启用的分类
const level1 = categoriesData.filter(
(cat) => cat.parent_id === 0 && cat.status === 1,
);
const level2 = categoriesData.filter(
(cat) => cat.parent_id !== 0 && cat.status === 1,
);
setLevel1Categories(level1);
setLevel2Categories(level2);
} else {
showError(message || t('加载分类失败'));
}
} catch (error) {
showError(t('加载分类失败: {{message}}', { message: error.message }));
} finally {
setCategoriesLoading(false);
}
};
// 当弹窗打开时加载分类
const onVisibilityChange = useEffectEvent(() => {
if (visible) {
loadCategories();
}
});
useEffect(() => onVisibilityChange(), [onVisibilityChange, visible]);
const handleConfirm = async () => {
if (selectedCategories.length === 0) return;
setLoading(true);
try {
await onConfirm(selectedCategories);
setSelectedCategories([]);
onClose();
} finally {
setLoading(false);
}
};
const handleClose = () => {
setSelectedCategories([]);
onClose();
};
// 按一级分类分组二级分类
const groupedCategories = level1Categories
.map((level1) => {
const children = level2Categories.filter(
(level2) => level2.parent_id === level1.id,
);
return {
label: level1.category_name,
children: children.map((cat) => ({
label: cat.category_name,
value: cat.category_key,
})),
};
})
.filter((group) => group.children.length > 0); // 只显示有子分类的一级分类
return (