import React, { useState } from "react"; import { Box, Flex, Group, Text, Button, TextInput, Select, Badge } from "@mantine/core"; import { useTranslation } from "react-i18next"; import SearchIcon from "@mui/icons-material/Search"; import SortIcon from "@mui/icons-material/Sort"; import FileCard from "./FileCard"; import { FileWithUrl } from "../../types/file"; interface FileGridProps { files: FileWithUrl[]; onRemove?: (index: number) => void; onDoubleClick?: (file: FileWithUrl) => void; onView?: (file: FileWithUrl) => void; onEdit?: (file: FileWithUrl) => void; onSelect?: (fileId: string) => void; selectedFiles?: string[]; showSearch?: boolean; showSort?: boolean; maxDisplay?: number; // If set, shows only this many files with "Show All" option onShowAll?: () => void; showingAll?: boolean; onDeleteAll?: () => void; isFileSupported?: (fileName: string) => boolean; // Function to check if file is supported } type SortOption = 'date' | 'name' | 'size'; const FileGrid = ({ files, onRemove, onDoubleClick, onView, onEdit, onSelect, selectedFiles = [], showSearch = false, showSort = false, maxDisplay, onShowAll, showingAll = false, onDeleteAll, isFileSupported }: FileGridProps) => { const { t } = useTranslation(); const [searchTerm, setSearchTerm] = useState(""); const [sortBy, setSortBy] = useState('date'); // Filter files based on search term const filteredFiles = files.filter(file => file.name.toLowerCase().includes(searchTerm.toLowerCase()) ); // Sort files const sortedFiles = [...filteredFiles].sort((a, b) => { switch (sortBy) { case 'date': return (b.lastModified || 0) - (a.lastModified || 0); case 'name': return a.name.localeCompare(b.name); case 'size': return (b.size || 0) - (a.size || 0); default: return 0; } }); // Apply max display limit if specified const displayFiles = maxDisplay && !showingAll ? sortedFiles.slice(0, maxDisplay) : sortedFiles; const hasMoreFiles = maxDisplay && !showingAll && sortedFiles.length > maxDisplay; return ( {/* Search and Sort Controls */} {(showSearch || showSort || onDeleteAll) && ( {showSearch && ( } value={searchTerm} onChange={(e) => setSearchTerm(e.currentTarget.value)} style={{ flexGrow: 1, maxWidth: 300, minWidth: 200 }} /> )} {showSort && (