import React, { useState } from 'react'; import { Film } from 'lucide-react'; import { DesignMD, CompanyProfile, BrandAsset } from '../../types'; import { UnifiedMediaLibrary, UnifiedMediaItem } from '../content-grid/UnifiedMediaLibrary'; type AssetFilter = 'all' | 'image' | 'video' | 'audio'; type SourceFilter = 'all' | 'uploaded' | 'generated'; interface BrandTabMediaProps { company: CompanyProfile; designMD: DesignMD; handleDesignChange: (key: keyof DesignMD, value: any) => void; onEditAsset?: (type: keyof DesignMD, url: string) => void; onSelectAsset?: (asset: BrandAsset) => void; selectedAssetId?: string; } export const BrandTabMedia: React.FC = ({ company, designMD, handleDesignChange, onSelectAsset, selectedAssetId }) => { const assets = designMD.brandAssets || []; const [uploadingBrandAsset, setUploadingBrandAsset] = useState(false); const [uploadingGenerated, setUploadingGenerated] = useState(false); const [filterType, setFilterType] = useState('all'); const [filterSource, setFilterSource] = useState('all'); const [refreshTrigger, setRefreshTrigger] = useState(0); const handleBrandAssetFiles = async (files: File[]) => { if (files.length === 0) return; setUploadingBrandAsset(true); try { let currentWorkspacePath = ''; if (window.electronAPI) { currentWorkspacePath = await window.electronAPI.fs.getWorkspacePath(); } const newAssets = [...assets]; for (const file of files) { const baseName = file.name.split('.')[0].replace(/[^a-zA-Z0-9-]/g, '-').toLowerCase(); let uniqueId = baseName; let counter = 1; while (newAssets.some(a => a.id === uniqueId)) { uniqueId = `${baseName}-${counter}`; counter++; } const formData = new FormData(); formData.append('file', file); let type: 'image' | 'video' | 'audio' = 'image'; let subfolder = 'files/images'; if (file.type.startsWith('video/')) { type = 'video'; subfolder = 'files/videos'; } else if (file.type.startsWith('audio/')) { type = 'audio'; subfolder = 'files/audios'; } if (currentWorkspacePath && company.id) { formData.append('brandId', company.id); formData.append('workspacePath', currentWorkspacePath); formData.append('subfolder', subfolder); } const endpoint = (currentWorkspacePath && company.id) ? '/api/upload/brand' : '/api/upload'; const res = await fetch(endpoint, { method: 'POST', body: formData }); if (!res.ok) throw new Error('Upload failed'); const data = await res.json(); if (data.url) { newAssets.push({ id: uniqueId, type, url: data.url, path: data.path }); } } handleDesignChange('brandAssets', newAssets); } catch (err) { console.error('Asset upload failed:', err); alert('Ocurrió un error al subir los archivos.'); } finally { setUploadingBrandAsset(false); } }; const handleDeleteAsset = (id: string) => { if (confirm(`¿Estás seguro de que deseas eliminar el asset "${id}"?`)) { handleDesignChange('brandAssets', assets.filter(a => a.id !== id)); } }; const handleRenameAsset = (oldId: string, newId: string) => { if (newId !== oldId && assets.some(a => a.id === newId)) { alert('Este identificador ya existe.'); return; } const newAssets = assets.map(a => { if (a.id === oldId) return { ...a, id: newId }; return a; }); handleDesignChange('brandAssets', newAssets); }; const onSelectUnified = (item: UnifiedMediaItem) => { if (!onSelectAsset) return; // Map UnifiedMediaItem back to BrandAsset if it's uploaded if (item.source === 'uploaded') { const asset = assets.find(a => a.id === item.id); if (asset) onSelectAsset(asset); } else { // It's generated. We can construct a mock BrandAsset to show in preview onSelectAsset({ id: item.name || item.id, type: item.type, path: item.path, url: item.url, date: item.date }); } }; return (
{/* Encabezado */}

Librería Unificada

Gestiona tus archivos base de marca y tus renders generados en un solo lugar.

{/* Unified Media Library as Global Drop Zone */}
); };