import React, { useState } from 'react' import { formatDateAgo } from '../utils/date' import { formatBytes, formatDuration } from '../utils/format' import { resolveAssetUrl } from '../utils/url' export { formatBytes, formatDuration } export interface AssetSummary { id: string name: string type: string size?: number | null sizeByte?: number | null mimeType?: string | null updatedAt?: string | Date createdAt?: string | Date commentsCount?: number fileCount?: number creator?: { id: string name: string image?: string | null } | null agent?: { id: string name: string } | null preview?: { thumbnailUrl?: string duration?: number pageCount?: number proxyType?: string | null } | null media?: { original?: { key?: string filesizeInBytes?: number codec?: string } | null metadata?: { duration?: number } videoTranscodes?: Array<{ id: string url: string key: string width: number height: number size: number }> } | null versionStack?: { id: string versions: Array<{ version: number current: boolean id: string name?: string | null createdAt?: string | null previewUrl?: string | null }> } | null } export interface FileItemProps { asset: AssetSummary endpoint?: string apiKey?: string onClick: (e: React.MouseEvent) => void onImportRaw?: (asset: AssetSummary) => void onSelectVideoForImport?: (asset: AssetSummary) => void onLinkSequence?: (asset: AssetSummary) => void onUnlinkSequence?: (asset: AssetSummary) => void isLinked?: boolean linkedSequenceName?: string } export function getFileTypeCategory( asset: AssetSummary, ): 'folder' | 'video' | 'audio' | 'image' | 'file' { if (asset.type === 'folder') return 'folder' const name = asset.name.toLowerCase() const mime = asset.mimeType?.toLowerCase() || '' if (mime.startsWith('video/') || /\.(mp4|mov|avi|mkv|m4v|webm)$/.test(name)) { return 'video' } if (mime.startsWith('audio/') || /\.(mp3|wav|aac|flac|m4a|ogg)$/.test(name)) { return 'audio' } if (mime.startsWith('image/') || /\.(png|jpg|jpeg|gif|webp|svg|bmp)$/.test(name)) { return 'image' } return 'file' } export function getAssetVersionLabel(asset: AssetSummary): string | null { if (asset.versionStack?.versions && asset.versionStack.versions.length > 0) { const matched = asset.versionStack.versions.find((v) => v.id === asset.id) if (matched?.version != null) { return `v${matched.version}` } const current = asset.versionStack.versions.find((v) => v.current) if (current?.version != null) { return `v${current.version}` } const first = asset.versionStack.versions[0] if (first?.version != null) { return `v${first.version}` } return `v${asset.versionStack.versions.length}` } return null } /** List View Row Item */ export const FileItem: React.FC = ({ asset, endpoint = '', onClick, onImportRaw, onSelectVideoForImport, onLinkSequence, onUnlinkSequence, isLinked = false, linkedSequenceName, }) => { const [imgError, setImgError] = useState(false) const category = getFileTypeCategory(asset) const isFolder = category === 'folder' const versionLabel = getAssetVersionLabel(asset) const effectiveSize = asset.sizeByte ?? asset.size const timeText = formatDateAgo(asset.createdAt || asset.updatedAt) const thumbUrl = resolveAssetUrl(asset.preview?.thumbnailUrl, endpoint) const iconFallback = (
{category === 'folder' && ( )} {category === 'video' && ( )} {category === 'audio' && ( )} {category === 'image' && ( )} {category === 'file' && ( )}
) const handleImportAction = () => { if (isFolder) return if (category === 'video' && onSelectVideoForImport) { onSelectVideoForImport(asset) } else if (onImportRaw) { onImportRaw(asset) } } const handleRowClick = (e: React.MouseEvent) => { if (isFolder) { onClick(e) } else { e.stopPropagation() handleImportAction() } } return (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() handleRowClick(e as unknown as React.MouseEvent) } }} >
{thumbUrl && !isFolder && !imgError ? (
{asset.name} console.log('[UXP Image OK]', thumbUrl)} onError={() => { console.error('[UXP Image Failed]', thumbUrl) setImgError(true) }} />
) : ( iconFallback )}
{asset.name} {versionLabel ? `${versionLabel} • ` : ''} {isFolder ? 'Folder' : formatBytes(effectiveSize)} {timeText ? ` • ${timeText}` : ''}
{asset.commentsCount != null && asset.commentsCount > 0 && ( {asset.commentsCount} {asset.commentsCount === 1 ? 'comment' : 'comments'} )} {isFolder ? ( ) : (
{(onLinkSequence || onUnlinkSequence) && (category === 'video' || category === 'audio') && ( { e.stopPropagation() if (isLinked && onUnlinkSequence) { onUnlinkSequence(asset) } else if (onLinkSequence) { onLinkSequence(asset) } }} > {isLinked ? ( ) : ( )} )} {(onImportRaw || onSelectVideoForImport) && ( { e.stopPropagation() handleImportAction() }} > )}
)}
) } /** Card View Row Item (2 Columns: Left Preview, Right Info as Raw Text) */ export const FileCardItem: React.FC = ({ asset, endpoint = '', onClick, onImportRaw, onSelectVideoForImport, onLinkSequence, onUnlinkSequence, isLinked = false, linkedSequenceName, }) => { const [imgError, setImgError] = useState(false) const category = getFileTypeCategory(asset) const isFolder = category === 'folder' const effectiveSize = asset.sizeByte ?? asset.size const timeText = formatDateAgo(asset.createdAt || asset.updatedAt) const thumbUrl = resolveAssetUrl(asset.preview?.thumbnailUrl, endpoint) const duration = asset.preview?.duration ?? asset.media?.metadata?.duration const durationText = formatDuration(duration) const creatorName = asset.creator?.name || asset.agent?.name const cardFallback = (
{category === 'folder' && ( )} {category === 'video' && ( )} {category === 'audio' && ( )} {category === 'image' && ( )} {category === 'file' && ( )}
) const handleImportAction = () => { if (isFolder) return if (category === 'video' && onSelectVideoForImport) { onSelectVideoForImport(asset) } else if (onImportRaw) { onImportRaw(asset) } } const handleCardClick = (e: React.MouseEvent) => { if (isFolder) { onClick(e) } } // Assemble raw text details for Line 3 (Version • Duration • Size/Type • Comments) const details: string[] = [] const versionLabel = getAssetVersionLabel(asset) if (versionLabel) { details.push(versionLabel) } if (durationText) { details.push(durationText) } if (isFolder) { details.push(asset.fileCount != null ? `${asset.fileCount} items` : 'Folder') } else if (effectiveSize != null) { details.push(formatBytes(effectiveSize)) } if (asset.commentsCount != null && asset.commentsCount > 0) { details.push(`${asset.commentsCount} ${asset.commentsCount === 1 ? 'comment' : 'comments'}`) } if (isLinked) { details.push(`Linked: ${linkedSequenceName || 'Sequence'}`) } return (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() handleCardClick(e as unknown as React.MouseEvent) } } : undefined } > {/* Left Column: Preview Only (NO LABELS, NO BADGES) */}
{thumbUrl && !isFolder && !imgError ? ( {asset.name} setImgError(true)} /> ) : ( cardFallback )}
{/* Right Column: Title, Creator & Date, Duration / Size / Comments as Raw Text */}
{asset.name}
{(creatorName || timeText) && (
{creatorName && {creatorName}} {creatorName && timeText && •} {timeText && {timeText}}
)} {details.length > 0 && (
{details.map((item, idx) => ( {idx > 0 && •} {item} ))}
)}
{/* Far Right Action Column (Centered Vertically across the card) */}
{isFolder ? ( ) : ( <> {(onLinkSequence || onUnlinkSequence) && (category === 'video' || category === 'audio') && ( { e.stopPropagation() if (isLinked && onUnlinkSequence) { onUnlinkSequence(asset) } else if (onLinkSequence) { onLinkSequence(asset) } }} > {isLinked ? ( ) : ( )} )} {(onImportRaw || onSelectVideoForImport) && ( { e.stopPropagation() handleImportAction() }} > )} )}
) }