import { useState, useEffect } from 'react'; import { TauriAPI } from '../api/tauri'; import '../styles/AssetBrowser.css'; interface AssetItem { name: string; path: string; type: 'file' | 'folder'; extension?: string; } interface AssetBrowserProps { projectPath: string | null; locale: string; } export function AssetBrowser({ projectPath, locale }: AssetBrowserProps) { const [currentPath, setCurrentPath] = useState(''); const [assets, setAssets] = useState([]); const [selectedAsset, setSelectedAsset] = useState(null); const [loading, setLoading] = useState(false); const translations = { en: { title: 'Assets', noProject: 'No project loaded', loading: 'Loading...', empty: 'No assets found', name: 'Name', type: 'Type', file: 'File', folder: 'Folder' }, zh: { title: '资产', noProject: '没有加载项目', loading: '加载中...', empty: '没有找到资产', name: '名称', type: '类型', file: '文件', folder: '文件夹' } }; const t = translations[locale as keyof typeof translations] || translations.en; useEffect(() => { if (projectPath) { setCurrentPath(projectPath); loadAssets(projectPath); } else { setAssets([]); setCurrentPath(''); } }, [projectPath]); const loadAssets = async (path: string) => { setLoading(true); try { const files = await TauriAPI.scanDirectory(path, '*'); const assetItems: AssetItem[] = files.map(filePath => { const name = filePath.split(/[\\/]/).pop() || ''; const extension = name.includes('.') ? name.split('.').pop() : undefined; return { name, path: filePath, type: 'file' as const, extension }; }); assetItems.sort((a, b) => { if (a.type === b.type) { return a.name.localeCompare(b.name); } return a.type === 'folder' ? -1 : 1; }); setAssets(assetItems); } catch (error) { console.error('Failed to load assets:', error); setAssets([]); } finally { setLoading(false); } }; const handleAssetClick = (asset: AssetItem) => { setSelectedAsset(asset.path); if (asset.type === 'folder') { setCurrentPath(asset.path); loadAssets(asset.path); } }; const handleAssetDoubleClick = (asset: AssetItem) => { console.log('Open asset:', asset); }; const getFileIcon = (extension?: string) => { switch (extension?.toLowerCase()) { case 'ts': case 'tsx': case 'js': case 'jsx': return ( ); case 'json': return ( ); case 'png': case 'jpg': case 'jpeg': case 'gif': return ( ); default: return ( ); } }; const getFolderIcon = () => ( ); if (!projectPath) { return (

{t.title}

{t.noProject}

); } return (

{t.title}

{currentPath}
{loading ? (

{t.loading}

) : assets.length === 0 ? (

{t.empty}

) : (
{assets.map((asset, index) => (
handleAssetClick(asset)} onDoubleClick={() => handleAssetDoubleClick(asset)} > {asset.type === 'folder' ? getFolderIcon() : getFileIcon(asset.extension)}
{asset.name}
{asset.type === 'folder' ? t.folder : asset.extension || t.file}
))}
)}
); }