"use client"; import React, { useState, useEffect } from 'react'; import { Folder, FileText, ChevronRight, Loader2 } from 'lucide-react'; interface MissionFile { type: 'folder' | 'file'; name: string; path: string; key: string; size?: number; lastModified?: string; } interface MissionFilesViewProps { missionId: string; onFileSelect: (file: MissionFile) => void; selectedFileKey?: string; initialPath?: string; // Optional initial path (e.g., "attachments") } export const MissionFilesView: React.FC = ({ missionId, onFileSelect, selectedFileKey, initialPath = 'attachments' // Default to attachments folder }) => { const [files, setFiles] = useState([]); const [currentPath, setCurrentPath] = useState(initialPath); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!missionId) return; const fetchFiles = async () => { try { setIsLoading(true); setError(null); const url = `/api/missions/${missionId}/files${currentPath ? `?path=${encodeURIComponent(currentPath)}` : ''}`; const response = await fetch(url); if (!response.ok) { throw new Error('Failed to fetch files'); } const data = await response.json(); setFiles(data.files || []); } catch (err) { console.error('Error fetching mission files:', err); setError(err instanceof Error ? err.message : 'Failed to load files'); } finally { setIsLoading(false); } }; fetchFiles(); }, [missionId, currentPath]); const handleItemClick = (item: MissionFile) => { if (item.type === 'folder') { // Navigate into folder const newPath = item.path.replace(`missions/${missionId}/`, '').replace(/\/$/, ''); setCurrentPath(newPath); } else { // Select file onFileSelect(item); } }; const handleBack = () => { const pathParts = currentPath.split('/').filter(Boolean); pathParts.pop(); setCurrentPath(pathParts.join('/')); }; if (isLoading) { return (
); } if (error) { return (

Erreur

{error}

); } return (

{currentPath ? currentPath.split('/').pop() : 'Fichiers'}

{currentPath && ( )}
{files.length === 0 ? (

Aucun fichier

) : (
    {files.map((file) => { const Icon = file.type === 'folder' ? Folder : FileText; return (
  • handleItemClick(file)} className={`p-4 hover:bg-carnet-hover cursor-pointer flex items-center space-x-2 ${ selectedFileKey === file.key ? 'bg-carnet-hover' : '' }`} > {file.name} {file.type === 'folder' && ( )}
  • ); })}
)}
); };