import React, { useState, useEffect } from 'react'; import { Box, Button, Modal, TextField, } from "@mui/material"; import { DataGrid, GridColDef } from '@mui/x-data-grid'; import { Search } from '@mui/icons-material'; import './DirectorySearchModal.css'; interface DirectorySearchModalProps { dir: string; open: boolean; onClose: () => void; modalId: string; } interface FileData { id: number; filePath: string; fileName: string; fileExtension: string; createdAt: string; modifiedAt: string; accessedAt: string; size: number; } const columns: GridColDef[] = [ { field: 'filePath', headerName: 'File Path', width: 250 }, { field: 'fileName', headerName: 'File Name', width: 150 }, { field: 'fileExtension', headerName: 'File Extension', width: 100 }, { field: 'size', headerName: 'Size', width: 100, valueFormatter: (value:any) => { console.log("params.value:", value); // Debugging lin // Ensure the value is a number const bytes = typeof value === 'number' ? value : 0; if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = 2; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; }, }, { field: 'createdAt', headerName: 'Created At', width: 150 }, { field: 'modifiedAt', headerName: 'Modified At', width: 150 }, { field: 'accessedAt', headerName: 'Accessed At', width: 150 }, ]; const DirectorySearchModal: React.FC = ({ dir, open, onClose, modalId }) => { const [searchQuery, setSearchQuery] = useState(''); const [rows, setRows] = useState([]); useEffect(() => { handleSearchChange({ target: { value: '*' } } as any); }, []); const handleSearchChange = async (event: React.ChangeEvent) => { const query = event.target.value; setSearchQuery(query); window.electron.meilisearchSearch(dir, query) .then((response: { success: boolean; data: any[]; error?: string }) => { if (response.success && response.data) { const fileData: FileData[] = response.data.map((doc: any, index: number) => ({ id: doc.name, filePath: doc.name, fileName: doc.fileName, fileExtension: doc.extension, size: doc.size, // Ensure size is not undefined createdAt: doc.createdAt, modifiedAt: doc.modifiedAt, accessedAt: doc.accessedAt, })); setRows(fileData); } else { console.error('Error searching Meilisearch:', response.error); setRows([]); } }); }; return (
); }; export default DirectorySearchModal;