import React, { useEffect, useState } from 'react'; import { FileText, Download, Loader, Trash2, AlertCircle } from 'lucide-react'; interface FileListProps { supabase: any; darkMode: boolean; isAdmin?: boolean; } interface FileData { id: string; filename: string; storage_path: string; file_type: string; uploaded_at: string; } const FileList: React.FC = ({ supabase, darkMode, isAdmin = false }) => { const [files, setFiles] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [downloading, setDownloading] = useState(null); const [deleting, setDeleting] = useState(null); useEffect(() => { fetchFiles(); }, []); const fetchFiles = async () => { try { setError(null); setLoading(true); // Check if Supabase is initialized properly if (!supabase) { throw new Error('Database connection not initialized'); } // Test connection with a simple query first const { error: connectionError } = await supabase .from('codette_files') .select('count'); if (connectionError) { throw connectionError; } // Proceed with actual data fetch const { data, error } = await supabase .from('codette_files') .select('*') .order('uploaded_at', { ascending: false }); if (error) throw error; setFiles(data || []); } catch (err: any) { console.error('Error fetching files:', err); setError(err.message || 'Failed to fetch files. Please check your connection.'); setFiles([]); } finally { setLoading(false); } }; const handleDownload = async (file: FileData) => { try { setDownloading(file.id); setError(null); const { data, error } = await supabase.storage .from('codette-files') .download(file.storage_path); if (error) throw error; const url = window.URL.createObjectURL(data); const a = document.createElement('a'); a.href = url; a.download = file.filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } catch (err: any) { console.error('Error downloading file:', err); setError(err.message || 'Failed to download file. Please try again.'); } finally { setDownloading(null); } }; const handleDelete = async (file: FileData) => { if (!isAdmin) return; if (!confirm('Are you sure you want to delete this file?')) return; try { setDeleting(file.id); setError(null); // Delete from storage const { error: storageError } = await supabase.storage .from('codette-files') .remove([file.storage_path]); if (storageError) throw storageError; // Delete from database const { error: dbError } = await supabase .from('codette_files') .delete() .match({ id: file.id }); if (dbError) throw dbError; // Update local state setFiles(files.filter(f => f.id !== file.id)); } catch (err: any) { console.error('Error deleting file:', err); setError(err.message || 'Failed to delete file. Please try again.'); } finally { setDeleting(null); } }; const handleRetry = () => { fetchFiles(); }; if (loading) { return (
); } if (error) { return (

Connection Error

{error}

); } return (

Uploaded Files

{files.length === 0 ? (

No files uploaded yet.

) : (
{files.map((file) => (

{file.filename}

{new Date(file.uploaded_at).toLocaleDateString()}

{isAdmin && ( )}
))}
)}
); }; export default FileList;