// FileListComponent.jsx
import React from "react";
const DiffViewer = ({ diff, onClose }) => {
if (!diff) return null;
const lines = diff.split('\n');
const handleDownload = () => {
// Create a Blob from the diff string
const blob = new Blob([diff], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'file_changes.diff'; // Specify a default filename
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
return (
<div className="fixed inset-0 bg-gray-900 bg-opacity-75 z-50 flex items-center justify-center p-4">
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-5xl w-full max-h-[90vh] flex flex-col overflow-hidden">
<div className="flex justify-between items-center p-4 border-b border-gray-200 dark:border-gray-700">
<h2 className="text-xl font-semibold text-gray-900 dark:text-gray-100">File Changes</h2>
<div className="flex items-center space-x-2">
{/* Download Button */}
<button
onClick={handleDownload}
className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 transition-colors duration-200"
>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.707-8.707a1 1 0 00-1.414 1.414l4 4a1 1 0 001.414 0l4-4a1 1 0 00-1.414-1.414L11 10.586V3a1 1 0 10-2 0v7.586L6.707 8.293z" clipRule="evenodd" />
</svg>
</button>
{/* Close Button */}
<button
onClick={onClose}
className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 transition-colors duration-200"
>
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
<pre className="p-4 overflow-y-auto text-sm font-mono flex-grow">
{lines.map((line, index) => {
let color = 'text-gray-900 dark:text-gray-100';
if (line.startsWith('+')) {
color = 'text-green-600 dark:text-green-400';
} else if (line.startsWith('-')) {
color = 'text-red-600 dark:text-red-400';
}
return (
<div key={index} className={color}>
{line}
</div>
);
})}
</pre>
</div>
</div>
);
};
export default DiffViewer;