import React, { useState } from 'react'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Badge } from '@/components/ui/badge'; import { Trash2, Edit, X } from 'lucide-react'; // 获取图片URL的函数 const getImageUrl = (item) => { if (!item.stored_path) return ''; // 从完整路径中提取文件名 const filename = item.stored_path.split(/[\\/]/).pop(); if (!filename) return ''; // 构建完整的图片URL const baseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'; return `${baseURL}/materials/${filename}`; }; // 获取素材类型标签配置 const getMaterialTypeConfig = (imageType) => { switch (imageType) { case 'face': return { label: 'IP', className: 'bg-red-500 text-white border-red-500' }; case 'cloth': return { label: '服装', className: 'bg-blue-500 text-white border-blue-500' }; case 'original': return { label: '原图', className: 'bg-purple-500 text-white border-purple-500' }; default: return null; } }; export default function MaterialCard({ material, onDelete, onUpdateName }) { const [isEditing, setIsEditing] = useState(false); const [editName, setEditName] = useState(material.name || material.original_filename || ''); const handleSaveName = async () => { if (editName.trim() !== material.name) { await onUpdateName(material.id, editName.trim()); } setIsEditing(false); }; const handleCancelEdit = () => { setEditName(material.name || material.original_filename || ''); setIsEditing(false); }; const typeConfig = getMaterialTypeConfig(material.image_type); return (
{material.name { console.error('图片加载失败:', getImageUrl(material)); e.target.src = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgdmlld0JveD0iMCAwIDIwMCAyMDAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIiBmaWxsPSIjRjNGNEY2Ii8+CjxwYXRoIGQ9Ik01MCAxMDBMMTAwIDUwTDE1MCAxMDBMMTAwIDE1MFYxMDBIMFYxMDBaIiBmaWxsPSIjOUI5QkEwIi8+Cjwvc3ZnPgo='; }} /> {/* 删除按钮 */}
{/* 素材类型标签 */} {typeConfig && (
{typeConfig.label}
)}
{isEditing ? (
setEditName(e.target.value)} className="flex-1" autoFocus onKeyDown={(e) => { if (e.key === 'Enter') handleSaveName(); if (e.key === 'Escape') handleCancelEdit(); }} />
) : (
{material.name || material.original_filename}
)}
); }