'use client'; import { useCallback } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Download, FileIcon } from 'lucide-react'; import { deleteFile } from '@kit/module-builder/actions/file-actions'; import { Badge } from '@kit/ui/badge'; import { Button } from '@kit/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card'; import { useActionWithToast } from '@kit/ui/use-action-with-toast'; import { DeleteConfirmButton } from './delete-confirm-button'; interface FileRecord { id: string; file_name: string; original_name: string; mime_type: string; file_size: number; created_at: string; storage_path: string; publicUrl: string; } interface FilesTableProps { files: FileRecord[]; pagination: { total: number; page: number; pageSize: number }; } function formatFileSize(bytes: number): string { if (bytes >= 1024 * 1024) { return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } return `${(bytes / 1024).toFixed(1)} KB`; } function formatDate(dateStr: string): string { return new Date(dateStr).toLocaleDateString('de-AT', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', }); } function getMimeLabel(mimeType: string): string { const map: Record = { 'application/pdf': 'PDF', 'image/jpeg': 'JPEG', 'image/png': 'PNG', 'image/gif': 'GIF', 'image/webp': 'WebP', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'DOCX', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'XLSX', 'text/csv': 'CSV', 'text/plain': 'TXT', 'application/zip': 'ZIP', }; return map[mimeType] ?? mimeType.split('/').pop()?.toUpperCase() ?? 'Datei'; } export function FilesTable({ files, pagination }: FilesTableProps) { const router = useRouter(); const searchParams = useSearchParams(); const { total, page, pageSize } = pagination; const totalPages = Math.max(1, Math.ceil(total / pageSize)); const { execute: executeDelete, isPending: isDeleting } = useActionWithToast( deleteFile, { successMessage: 'Datei gelöscht', onSuccess: () => router.refresh(), }, ); const handlePageChange = useCallback( (newPage: number) => { const params = new URLSearchParams(searchParams.toString()); params.set('page', String(newPage)); router.push(`?${params.toString()}`); }, [router, searchParams], ); return ( Dateien ({total}) {files.length === 0 ? (

Keine Dateien vorhanden

Laden Sie Ihre erste Datei hoch.

) : (
{files.map((file) => ( ))}
Dateiname Typ Größe Hochgeladen Aktionen
{file.original_name} {getMimeLabel(file.mime_type)} {formatFileSize(file.file_size)} {formatDate(file.created_at)}
executeDelete({ fileId: file.id })} />
)} {totalPages > 1 && (

Seite {page} von {totalPages} ({total} Einträge)

)}
); }