export function formatBytes(bytes: number): string { if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'; const units = ['B', 'KB', 'MB', 'GB', 'TB']; const idx = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1); const val = bytes / Math.pow(1024, idx); return `${val >= 10 || idx === 0 ? val.toFixed(0) : val.toFixed(1)} ${units[idx]}`; } export function formatDateVi(iso?: string | null): string { if (!iso) return '—'; const d = new Date(iso); if (Number.isNaN(d.getTime())) return iso.slice(0, 10); return d.toLocaleDateString('vi-VN', { day: '2-digit', month: '2-digit', year: 'numeric', }); } export function normalizePath(path: string): string { if (!path || path === '/') return '/'; return path.replace(/\/+$/, ''); } export function joinPath(base: string, name: string): string { const cleanBase = base.replace(/^\/+|\/+$/g, ''); const cleanName = name.replace(/^\/+/, ''); return cleanBase ? `${cleanBase}/${cleanName}` : cleanName; } export type FileCategory = 'dir' | 'image' | 'code' | 'markdown' | 'archive' | 'text'; const IMAGE_EXTS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico']); const CODE_EXTS = new Set([ 'ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs', 'py', 'go', 'rs', 'json', 'yaml', 'yml', 'toml', 'html', 'css', 'scss', 'sh', 'bash', 'sql', 'env', ]); const MD_EXTS = new Set(['md', 'mdx', 'txt', 'log']); const ARCHIVE_EXTS = new Set(['zip', 'tar', 'gz', 'tgz', '7z', 'rar']); export function getFileCategory(name: string, isDir: boolean): FileCategory { if (isDir) return 'dir'; const ext = name.split('.').pop()?.toLowerCase() || ''; if (IMAGE_EXTS.has(ext)) return 'image'; if (CODE_EXTS.has(ext)) return 'code'; if (MD_EXTS.has(ext)) return 'markdown'; if (ARCHIVE_EXTS.has(ext)) return 'archive'; return 'text'; }