import React from 'react'; import { Stack, Paper, Text, Badge, Group, Collapse, Box, ScrollArea, Code, Divider } from '@mantine/core'; import { useFileContext } from '../../contexts/FileContext'; import { FileOperation, FileOperationHistory as FileOperationHistoryType } from '../../types/fileContext'; import { PageOperation } from '../../types/pageEditor'; interface FileOperationHistoryProps { fileId: string; showOnlyApplied?: boolean; maxHeight?: number; } const FileOperationHistory: React.FC = ({ fileId, showOnlyApplied = false, maxHeight = 400 }) => { const { getFileHistory, getAppliedOperations } = useFileContext(); const history = getFileHistory(fileId); const allOperations = showOnlyApplied ? getAppliedOperations(fileId) : history?.operations || []; const operations = allOperations.filter(op => 'fileIds' in op) as FileOperation[]; const formatTimestamp = (timestamp: number) => { return new Date(timestamp).toLocaleString(); }; const getOperationIcon = (type: string) => { switch (type) { case 'split': return '✂️'; case 'merge': return '🔗'; case 'compress': return '🗜️'; case 'rotate': return '🔄'; case 'delete': return '🗑️'; case 'move': return '↕️'; case 'insert': return '📄'; case 'upload': return '⬆️'; case 'add': return '➕'; case 'remove': return '➖'; case 'replace': return '🔄'; case 'convert': return '🔄'; default: return '⚙️'; } }; const getStatusColor = (status: string) => { switch (status) { case 'applied': return 'green'; case 'failed': return 'red'; case 'pending': return 'yellow'; default: return 'gray'; } }; const renderOperationDetails = (operation: FileOperation) => { if ('metadata' in operation && operation.metadata) { const { metadata } = operation; return ( {metadata.parameters && ( Parameters: {JSON.stringify(metadata.parameters, null, 2)} )} {metadata.originalFileName && ( Original file: {metadata.originalFileName} )} {metadata.outputFileNames && ( Output files: {metadata.outputFileNames.join(', ')} )} {metadata.fileSize && ( File size: {(metadata.fileSize / 1024 / 1024).toFixed(2)} MB )} {metadata.pageCount && ( Pages: {metadata.pageCount} )} {metadata.error && ( Error: {metadata.error} )} ); } return null; }; if (!history || operations.length === 0) { return ( {showOnlyApplied ? 'No applied operations found' : 'No operation history available'} ); } return ( {showOnlyApplied ? 'Applied Operations' : 'Operation History'} {operations.length} operations {operations.map((operation, index) => ( {getOperationIcon(operation.type)} {operation.type.charAt(0).toUpperCase() + operation.type.slice(1)} {formatTimestamp(operation.timestamp)} {operation.status} {renderOperationDetails(operation)} {index < operations.length - 1 && } ))} {history && ( Created: {formatTimestamp(history.createdAt)} Last modified: {formatTimestamp(history.lastModified)} )} ); }; export default FileOperationHistory;