"use client"; import { Table, TableBody, TableCell, TableHeader, TableRow, } from "../ui/table"; import Badge from "../ui/badge/Badge"; export type MediaSortColumn = | "created_at" | "updated_at" | "size" | "original_name" | "mime_type"; export interface MediaItem { id: string; user_id: string; storage_key: string; original_name: string; mime_type: string; size: number; file_metadata: Record | null; created_at: string; updated_at: string; } interface MediaTableProps { data: MediaItem[]; onSort: (column: MediaSortColumn) => void; sortBy?: MediaSortColumn; sortOrder?: "asc" | "desc"; // Các props mới thêm cho yêu cầu chọn, xem, xóa selectedIds: string[]; onToggleSelect: (id: string) => void; onToggleSelectAll: (checked: boolean) => void; onViewSingle: (item: MediaItem, index: number) => void; onDeleteSingle: (id: string) => void; } export default function MediaTable({ data, onSort, sortBy, sortOrder, selectedIds, onToggleSelect, onToggleSelectAll, onViewSingle, onDeleteSingle, }: MediaTableProps) { const formatDate = (dateString: string | null | undefined) => { if (!dateString) return "-"; const date = new Date(dateString); return date.toLocaleDateString("vi-VN", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", }); }; const formatBytes = (bytes: number) => { if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB", "TB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; }; const SortIcon = ({ column }: { column: MediaSortColumn }) => { const isActive = sortBy === column; return (
); }; const getMimeTypeBadge = (mimeType: string) => { if (mimeType.includes("image")) { return ( Hình ảnh ); } if (mimeType.includes("pdf") || mimeType.includes("word") || mimeType.includes("document")) { return ( Tài liệu ); } return ( Khác ); }; const isAllSelected = data.length > 0 && selectedIds.length === data.length; return (
onToggleSelectAll(e.target.checked)} className="w-4 h-4 text-brand-500 border-gray-300 rounded cursor-pointer focus:ring-brand-500 dark:bg-gray-800 dark:border-gray-600" />
onSort("original_name")}> Tên tệp
onSort("mime_type")}> Định dạng
onSort("size")}> Kích thước
onSort("created_at")}> Ngày tải lên
onSort("updated_at")}> Cập nhật
Thao tác
{data.length > 0 ? ( data.map((item, idx) => ( {/* Yêu cầu 1: Cột 0 - Ô hình vuông chọn từng item */} onToggleSelect(item.id)} className="w-4 h-4 text-brand-500 border-gray-300 rounded cursor-pointer focus:ring-brand-500 dark:bg-gray-800 dark:border-gray-600" /> {item.original_name}
{getMimeTypeBadge(item.mime_type)}
{item.mime_type}
{formatBytes(item.size)} {formatDate(item.created_at)} {formatDate(item.updated_at)}
)) ) : ( Không tìm thấy tệp tin nào )}
); }