import React, { useState, useMemo } from 'react'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { CustomDropdown } from './shared/CustomDropdown'; import { Badge } from '@/components/ui/badge'; import { Search, Eye, Edit, Trash2, Plus, ArrowUpDown, ArrowUp, ArrowDown, Monitor, Laptop, HardDrive } from 'lucide-react'; import { SystemDetails, SystemDetailsWithAsset } from '@/types/asset-management'; import { LoadingSpinner } from './shared'; import Pagination from './shared/Pagination'; import { usePagination } from '@/hooks/asset-management/usePagination'; interface SystemDetailsListProps { systemDetails: SystemDetails[] | SystemDetailsWithAsset[]; isLoading?: boolean; onView?: (systemDetails: SystemDetails) => void; onEdit?: (systemDetails: SystemDetails) => void; onDelete?: (systemDetails: SystemDetails) => void; onCreate?: () => void; onNavigateToAsset?: (assetId: number) => void; showActions?: boolean; showAssetInfo?: boolean; } type SortField = keyof SystemDetails; type SortDirection = 'asc' | 'desc'; interface SortConfig { field: SortField; direction: SortDirection; } const SystemDetailsList: React.FC = ({ systemDetails, isLoading = false, onView, onEdit, onDelete, onCreate, onNavigateToAsset, showActions = true, showAssetInfo = true }) => { const [searchTerm, setSearchTerm] = useState(''); const [manufacturerFilter, setManufacturerFilter] = useState('all'); const [sortConfig, setSortConfig] = useState({ field: 'systemDetailsID', direction: 'asc' }); // Helper function to get system type icon const getSystemTypeIcon = (systemModel: string) => { const model = systemModel.toLowerCase(); if (model.includes('laptop') || model.includes('notebook')) return ; if (model.includes('monitor') || model.includes('display')) return ; return ; }; // Get unique manufacturers for filter const uniqueManufacturers = useMemo(() => { const manufacturers = systemDetails.map(details => details.manufacturer); return Array.from(new Set(manufacturers)).sort(); }, [systemDetails]); // Filter and sort system details const filteredAndSortedSystemDetails = useMemo(() => { let filtered = systemDetails.filter(details => { const matchesSearch = details.systemModel.toLowerCase().includes(searchTerm.toLowerCase()) || details.manufacturer.toLowerCase().includes(searchTerm.toLowerCase()) || details.processor.toLowerCase().includes(searchTerm.toLowerCase()) || details.installedRAM.toLowerCase().includes(searchTerm.toLowerCase()) || details.graphics.toLowerCase().includes(searchTerm.toLowerCase()) || details.diskModel.toLowerCase().includes(searchTerm.toLowerCase()) || details.assetID.toString().includes(searchTerm); const matchesManufacturer = manufacturerFilter === 'all' || details.manufacturer === manufacturerFilter; return matchesSearch && matchesManufacturer; }); // Sort system details filtered.sort((a, b) => { const aValue = a[sortConfig.field]; const bValue = b[sortConfig.field]; if (typeof aValue === 'string' && typeof bValue === 'string') { const comparison = aValue.localeCompare(bValue); return sortConfig.direction === 'asc' ? comparison : -comparison; } if (typeof aValue === 'number' && typeof bValue === 'number') { const comparison = aValue - bValue; return sortConfig.direction === 'asc' ? comparison : -comparison; } return 0; }); return filtered; }, [systemDetails, searchTerm, manufacturerFilter, sortConfig]); // Use pagination hook const { currentPage, itemsPerPage, totalPages, paginatedData: paginatedSystemDetails, setCurrentPage, setItemsPerPage, } = usePagination(filteredAndSortedSystemDetails); const handleSort = (field: SortField) => { setSortConfig(prev => ({ field, direction: prev.field === field && prev.direction === 'asc' ? 'desc' : 'asc' })); }; const getSortIcon = (field: SortField) => { if (sortConfig.field !== field) { return ; } return sortConfig.direction === 'asc' ? : ; }; if (isLoading) { return (

System Details

); } return (
{/* Header */}

System Details

{onCreate && ( )}
{/* Filters */}
setSearchTerm(e.target.value)} className="pl-10" />
({ value: manufacturer, label: manufacturer })) ]} value={manufacturerFilter} onChange={setManufacturerFilter} placeholder="Filter by manufacturer" className="w-full sm:w-48" aria-label="Filter by manufacturer" />
{/* Results count */}
Showing {paginatedSystemDetails.length} of {filteredAndSortedSystemDetails.length} system details {filteredAndSortedSystemDetails.length !== systemDetails.length && ( (filtered from {systemDetails.length} total) )}
{/* Table */}
handleSort('systemDetailsID')} >
ID {getSortIcon('systemDetailsID')}
{showAssetInfo && ( handleSort('assetID')} >
Asset ID {getSortIcon('assetID')}
)} handleSort('systemModel')} >
System Model {getSortIcon('systemModel')}
handleSort('manufacturer')} >
Manufacturer {getSortIcon('manufacturer')}
handleSort('processor')} >
Processor {getSortIcon('processor')}
handleSort('installedRAM')} >
RAM {getSortIcon('installedRAM')}
Storage {showActions && Actions}
{paginatedSystemDetails.length === 0 ? ( {systemDetails.length === 0 ? "No system details found. Create your first system details record to get started." : "No system details match your current filters." } ) : ( paginatedSystemDetails.map((details) => { const detailsWithAsset = details as SystemDetailsWithAsset; return ( {details.systemDetailsID} {showAssetInfo && (
{details.assetID} {detailsWithAsset.asset && ( {detailsWithAsset.asset.assetType} )} {onNavigateToAsset && ( )}
)}
{getSystemTypeIcon(details.systemModel)} {details.systemModel}
{details.manufacturer} {details.processor} {details.installedRAM}
{details.diskSize}
{details.diskModel}
{showActions && (
{onView && ( )} {onEdit && ( )} {onDelete && ( )}
)}
); }) )}
{/* Pagination */} {filteredAndSortedSystemDetails.length > 0 && ( )}
); }; export default SystemDetailsList;