import React, { useState } from 'react'; import Layout from '../components/Layout/Layout'; import Card, { CardHeader, CardContent } from '../components/common/Card'; import Button from '../components/common/Button'; import Input from '../components/common/Input'; import { useApp } from '../contexts/AppContext'; const CategoriesPage: React.FC = () => { const { categories, addCategory, updateCategory, deleteCategory, promptGroups } = useApp(); const [showAddForm, setShowAddForm] = useState(false); const [editingCategoryId, setEditingCategoryId] = useState(null); const [categoryName, setCategoryName] = useState(''); const [categoryColor, setCategoryColor] = useState('#007AFF'); const colorOptions = [ { color: '#007AFF', name: '蓝色' }, { color: '#4CD964', name: '绿色' }, { color: '#FF3B30', name: '红色' }, { color: '#FF9500', name: '橙色' }, { color: '#FFCC00', name: '黄色' }, { color: '#5856D6', name: '紫色' }, { color: '#FF2D55', name: '粉色' }, { color: '#5AC8FA', name: '浅蓝色' }, ]; const resetForm = () => { setCategoryName(''); setCategoryColor('#007AFF'); setShowAddForm(false); setEditingCategoryId(null); }; const handleAddCategory = () => { if (!categoryName.trim()) return; addCategory({ name: categoryName.trim(), color: categoryColor }); resetForm(); }; const handleUpdateCategory = () => { if (!editingCategoryId || !categoryName.trim()) return; updateCategory(editingCategoryId, { name: categoryName.trim(), color: categoryColor }); resetForm(); }; const handleEditCategory = (categoryId: string) => { const category = categories.find(c => c._id === categoryId); if (category) { setCategoryName(category.name); setCategoryColor(category.color); setEditingCategoryId(categoryId); setShowAddForm(false); } }; const handleDeleteCategory = (categoryId: string) => { // 检查是否有使用该分类的提示词组 const usingGroups = promptGroups.filter(group => group.category === categoryId); if (usingGroups.length > 0) { alert(`无法删除此分类,有 ${usingGroups.length} 个提示词组正在使用它。请先修改这些提示词组的分类。`); return; } if (window.confirm('确定要删除此分类吗?')) { deleteCategory(categoryId); } }; const getCategoryUsageCount = (categoryId: string) => { return promptGroups.filter(group => group.category === categoryId).length; }; return (

分类管理

{!showAddForm && !editingCategoryId && ( )}
{(showAddForm || editingCategoryId) && (
setCategoryName(e.target.value)} placeholder="请输入分类名称" required />
{colorOptions.map((option) => (
setCategoryColor(option.color)} title={option.name} /> ))}
)}
{categories.map((category) => (
{category.name}
使用次数: {getCategoryUsageCount(category._id)}
))}
); }; export default CategoriesPage;