import { Button } from '@/components/ui/button';
import { Download } from 'lucide-react';
const BackupDownloader = ({ backup }: { backup: ApplicationBackup }) => {
const handleDownload = async (id: number | string) => {
try {
// Show loading state
const downloadBtn = document.getElementById(`download-btn-${id}`);
if (downloadBtn) {
// downloadBtn.disabled = true;
downloadBtn.innerHTML = '↻ Downloading...';
}
// Trigger file download
const response = await fetch(`/system/backup/${id}/download`, {
method: 'GET',
headers: {
Accept: 'application/zip',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '',
},
});
if (!response.ok) {
throw new Error('Download failed');
}
// Get the filename from content-disposition header
const contentDisposition = response.headers.get('content-disposition');
const filename = contentDisposition ? contentDisposition.split('filename=')[1].replace(/"/g, '') : `backup_${id}.zip`;
// Create blob and download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// Cleanup
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
console.error('Download failed:', error);
alert('Failed to download backup. Please try again.');
} finally {
// Reset button state
const downloadBtn = document.getElementById(`download-btn-${id}`);
if (downloadBtn) {
// downloadBtn.disabled = false;
downloadBtn.innerHTML =
'Download';
}
}
};
return (
// Update your button JSX:
);
};
export default BackupDownloader;