import React, { useState, useRef } from 'react' import { Upload, Video, Music, Image, File, Search, Filter, Grid, List } from 'lucide-react' import { useMediaStore } from '../stores/useMediaStore' import { useProjectStore } from '../stores/useProjectStore' interface MediaItem { id: string name: string type: 'video' | 'audio' | 'image' path: string size: number duration?: number thumbnail?: string createdAt: string } interface MediaLibraryProps { className?: string } const MediaLibrary: React.FC = ({ className = '' }) => { const fileInputRef = useRef(null) const [searchTerm, setSearchTerm] = useState('') const [filterType, setFilterType] = useState<'all' | 'video' | 'audio' | 'image'>('all') const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid') const [dragOver, setDragOver] = useState(false) const { setCurrentMedia } = useMediaStore() const { currentProject, addVideoTrack, addAudioTrack } = useProjectStore() // Mock media items - in real app this would come from the project const [mediaItems, setMediaItems] = useState([ { id: '1', name: 'sample_video.mp4', type: 'video', path: '/tmp/test_video.mp4', size: 1024000, duration: 30, createdAt: '2025-07-10T09:00:00Z' }, { id: '2', name: 'background_music.mp3', type: 'audio', path: '/tmp/audio.mp3', size: 512000, duration: 120, createdAt: '2025-07-10T09:15:00Z' } ]) // Filter media items const filteredItems = mediaItems.filter(item => { const matchesSearch = item.name.toLowerCase().includes(searchTerm.toLowerCase()) const matchesFilter = filterType === 'all' || item.type === filterType return matchesSearch && matchesFilter }) // Handle file upload const handleFileUpload = (files: FileList | null) => { if (!files) return Array.from(files).forEach(file => { const fileType = getFileType(file) if (fileType) { const newItem: MediaItem = { id: crypto.randomUUID(), name: file.name, type: fileType, path: URL.createObjectURL(file), size: file.size, createdAt: new Date().toISOString() } setMediaItems(prev => [...prev, newItem]) } }) } // Determine file type const getFileType = (file: File): 'video' | 'audio' | 'image' | null => { if (file.type.startsWith('video/')) return 'video' if (file.type.startsWith('audio/')) return 'audio' if (file.type.startsWith('image/')) return 'image' return null } // Handle drag and drop const handleDragOver = (e: React.DragEvent) => { e.preventDefault() setDragOver(true) } const handleDragLeave = (e: React.DragEvent) => { e.preventDefault() setDragOver(false) } const handleDrop = (e: React.DragEvent) => { e.preventDefault() setDragOver(false) handleFileUpload(e.dataTransfer.files) } // Handle media item click const handleMediaClick = (item: MediaItem) => { setCurrentMedia(item.path) } // Handle adding to timeline const handleAddToTimeline = (item: MediaItem) => { if (!currentProject) return if (item.type === 'video') { addVideoTrack({ id: crypto.randomUUID(), name: item.name, file_path: item.path, start_time: 0, duration: item.duration || 0 }) } else if (item.type === 'audio') { addAudioTrack({ id: crypto.randomUUID(), name: item.name, file_path: item.path, start_time: 0, duration: item.duration || 0, volume: 1.0 }) } } // Format file size const formatFileSize = (bytes: number): string => { if (bytes === 0) return '0 B' const k = 1024 const sizes = ['B', 'KB', 'MB', 'GB'] const i = Math.floor(Math.log(bytes) / Math.log(k)) return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] } // Format duration const formatDuration = (seconds?: number): string => { if (!seconds) return '' const mins = Math.floor(seconds / 60) const secs = Math.floor(seconds % 60) return `${mins}:${secs.toString().padStart(2, '0')}` } // Get icon for media type const getMediaIcon = (type: string) => { switch (type) { case 'video': return