Files
mxivideo/src/components/ProjectMaterialsCenter.tsx
2025-07-11 12:31:09 +08:00

313 lines
12 KiB
TypeScript

import React, { useState } from 'react'
import { Video, Play, Search, Filter, Tag, Clock, HardDrive, Eye, User } from 'lucide-react'
import { Project } from '../services/projectService'
import { VideoSegment, MediaService } from '../services/mediaService'
import { Model } from '../services/modelService'
import VideoPlayer from './VideoPlayer'
interface ProjectMaterialsCenterProps {
project: Project
materials: VideoSegment[]
models: Model[]
onMaterialsChange: (materials: VideoSegment[]) => void
}
const ProjectMaterialsCenter: React.FC<ProjectMaterialsCenterProps> = ({
project,
materials,
models,
onMaterialsChange
}) => {
const [searchTerm, setSearchTerm] = useState('')
const [selectedModelIds, setSelectedModelIds] = useState<string[]>([])
const [filterType, setFilterType] = useState<'all' | 'original' | 'segmented'>('all')
const [selectedMaterial, setSelectedMaterial] = useState<VideoSegment | null>(null)
const [showVideoPlayer, setShowVideoPlayer] = useState(false)
// 过滤素材
const filteredMaterials = materials.filter(material => {
// 搜索过滤
const matchesSearch = material.filename.toLowerCase().includes(searchTerm.toLowerCase()) ||
(material.tags || []).some(tag => tag.toLowerCase().includes(searchTerm.toLowerCase()))
// 模特过滤(如果选择了模特)
const matchesModel = selectedModelIds.length === 0 ||
selectedModelIds.some(modelId => {
const model = models.find(m => m.id === modelId)
return model && (material.tags || []).includes(model.model_number)
})
// 类型过滤
let matchesType = true
if (filterType === 'original') {
matchesType = material.segment_index === 0
} else if (filterType === 'segmented') {
matchesType = material.segment_index > 0
}
return matchesSearch && matchesModel && matchesType
})
const handleModelToggle = (modelId: string) => {
setSelectedModelIds(prev =>
prev.includes(modelId)
? prev.filter(id => id !== modelId)
: [...prev, modelId]
)
}
const handleUseMaterial = async (material: VideoSegment) => {
try {
await MediaService.incrementSegmentUsage(material.id)
const updatedMaterials = materials.map(m =>
m.id === material.id ? { ...m, use_count: m.use_count + 1 } : m
)
onMaterialsChange(updatedMaterials)
} catch (error) {
console.error('Failed to use material:', error)
}
}
const handlePlayMaterial = (material: VideoSegment) => {
setSelectedMaterial(material)
setShowVideoPlayer(true)
}
const formatDuration = (seconds: number) => {
const minutes = Math.floor(seconds / 60)
const remainingSeconds = Math.floor(seconds % 60)
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`
}
const formatFileSize = (bytes: number) => {
const sizes = ['B', 'KB', 'MB', 'GB']
if (bytes === 0) return '0 B'
const i = Math.floor(Math.log(bytes) / Math.log(1024))
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]
}
return (
<div className="h-full flex flex-col bg-white rounded-lg shadow-sm border border-gray-200">
{/* 头部 */}
<div className="p-6 border-b border-gray-200">
<div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-bold text-gray-900"></h2>
<div className="text-sm text-gray-600">
{filteredMaterials.length} / {materials.length}
</div>
</div>
{/* 搜索栏 */}
<div className="relative mb-4">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400" size={20} />
<input
type="text"
placeholder="搜索素材名称或标签..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10 pr-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent w-full"
/>
</div>
{/* 模特筛选标签 */}
<div className="mb-4">
<div className="flex items-center mb-2">
<User size={16} className="text-gray-400 mr-2" />
<span className="text-sm font-medium text-gray-700">:</span>
</div>
<div className="flex flex-wrap gap-2">
{models.map((model) => (
<button
key={model.id}
onClick={() => handleModelToggle(model.id)}
className={`px-3 py-1 text-sm rounded-full border transition-colors ${
selectedModelIds.includes(model.id)
? 'bg-blue-600 text-white border-blue-600'
: 'bg-white text-gray-700 border-gray-300 hover:border-blue-400'
}`}
>
{model.model_number}
</button>
))}
{selectedModelIds.length > 0 && (
<button
onClick={() => setSelectedModelIds([])}
className="px-3 py-1 text-sm bg-red-100 text-red-700 rounded-full hover:bg-red-200 transition-colors"
>
</button>
)}
</div>
</div>
{/* 类型过滤 */}
<div className="flex items-center space-x-4">
<div className="flex items-center">
<Filter size={16} className="text-gray-400 mr-2" />
<span className="text-sm font-medium text-gray-700">:</span>
</div>
<div className="flex space-x-2">
{[
{ value: 'all', label: '全部' },
{ value: 'original', label: '原始素材' },
{ value: 'segmented', label: '已分镜头' }
].map((option) => (
<button
key={option.value}
onClick={() => setFilterType(option.value as any)}
className={`px-3 py-1 text-sm rounded-lg transition-colors ${
filterType === option.value
? 'bg-blue-100 text-blue-700'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200'
}`}
>
{option.label}
</button>
))}
</div>
</div>
</div>
{/* 素材网格 */}
<div className="flex-1 overflow-y-auto p-6">
{filteredMaterials.length === 0 ? (
<div className="text-center py-12">
<Video className="mx-auto text-gray-400 mb-4" size={64} />
<h3 className="text-lg font-medium text-gray-900 mb-2">
{searchTerm || selectedModelIds.length > 0 || filterType !== 'all'
? '没有找到匹配的素材'
: '暂无项目素材'
}
</h3>
<p className="text-gray-600">
{searchTerm || selectedModelIds.length > 0 || filterType !== 'all'
? '尝试调整搜索条件或筛选器'
: `上传包含"${project.product_name}"标签的视频素材`
}
</p>
</div>
) : (
<div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-6">
{filteredMaterials.map((material) => (
<div key={material.id} className="bg-gray-50 rounded-lg overflow-hidden hover:shadow-md transition-shadow border border-gray-200">
{/* 视频缩略图 */}
<div className="relative h-40 bg-gray-100">
{material.thumbnail_path ? (
<img
src={material.thumbnail_path}
alt={material.filename}
className="w-full h-full object-cover"
/>
) : (
<div className="w-full h-full flex items-center justify-center">
<Video size={32} className="text-gray-400" />
</div>
)}
{/* 播放按钮 */}
<div className="absolute inset-0 bg-black bg-opacity-0 hover:bg-opacity-30 transition-all flex items-center justify-center">
<button
onClick={() => handlePlayMaterial(material)}
className="p-3 bg-white/80 rounded-full hover:bg-white transition-colors opacity-0 hover:opacity-100"
>
<Play size={24} className="text-gray-800" />
</button>
</div>
{/* 类型标识 */}
<div className="absolute top-2 left-2">
<span className={`px-2 py-1 text-xs rounded-full ${
material.segment_index === 0
? 'bg-green-100 text-green-800'
: 'bg-purple-100 text-purple-800'
}`}>
{material.segment_index === 0 ? '原始' : `片段${material.segment_index}`}
</span>
</div>
{/* 使用次数 */}
<div className="absolute top-2 right-2">
<span className="px-2 py-1 text-xs bg-black/50 text-white rounded-full">
使 {material.use_count}
</span>
</div>
</div>
{/* 素材信息 */}
<div className="p-4">
<h4 className="font-medium text-gray-900 mb-2 truncate" title={material.filename}>
{material.filename}
</h4>
{/* 基本信息 */}
<div className="flex items-center text-sm text-gray-600 mb-3">
<Clock size={14} className="mr-1" />
<span>{formatDuration(material.duration)}</span>
<span className="mx-2"></span>
<HardDrive size={14} className="mr-1" />
<span>{formatFileSize(material.file_size)}</span>
</div>
{/* 标签 */}
<div className="flex flex-wrap gap-1 mb-3">
{(material.tags || []).slice(0, 3).map((tag, index) => (
<span
key={index}
className={`inline-flex items-center px-2 py-1 text-xs rounded-full ${
tag === project.product_name
? 'bg-blue-100 text-blue-800'
: 'bg-gray-100 text-gray-700'
}`}
>
<Tag size={10} className="mr-1" />
{tag}
</span>
))}
{(material.tags || []).length > 3 && (
<span className="text-xs text-gray-500">+{(material.tags || []).length - 3}</span>
)}
</div>
{/* 操作按钮 */}
<div className="flex items-center justify-between">
<button
onClick={() => handlePlayMaterial(material)}
className="flex items-center px-3 py-1 text-sm text-blue-600 hover:text-blue-700 transition-colors"
>
<Eye size={14} className="mr-1" />
</button>
<button
onClick={() => handleUseMaterial(material)}
className="px-4 py-2 bg-blue-600 text-white text-sm rounded-lg hover:bg-blue-700 transition-colors"
>
使
</button>
</div>
</div>
</div>
))}
</div>
)}
</div>
{/* 视频播放器 */}
{selectedMaterial && (
<VideoPlayer
videoPath={selectedMaterial.file_path}
isOpen={showVideoPlayer}
onClose={() => {
setShowVideoPlayer(false)
setSelectedMaterial(null)
}}
title={`${selectedMaterial.filename} - ${
selectedMaterial.segment_index === 0 ? '原始素材' : `片段 ${selectedMaterial.segment_index}`
}`}
/>
)}
</div>
)
}
export default ProjectMaterialsCenter