import { useState, useEffect } from 'react'; import { triggerBackup, listBackups, restoreBackup } from '../backupApi'; import Modal from './Modal'; function AdminPanel({ onShowToast }) { const [backups, setBackups] = useState([]); const [loading, setLoading] = useState(false); const [restoreModal, setRestoreModal] = useState({ isOpen: false, filename: '' }); const [restoring, setRestoring] = useState(false); useEffect(() => { loadBackups(); }, []); const loadBackups = async () => { try { setLoading(true); const data = await listBackups(); setBackups(data.backups || []); } catch (error) { onShowToast(error.message || 'שגיאה בטעינת גיבויים', 'error'); } finally { setLoading(false); } }; const handleCreateBackup = async () => { try { setLoading(true); const result = await triggerBackup(); onShowToast('גיבוי נוצר בהצלחה! 📦', 'success'); loadBackups(); // Refresh list } catch (error) { onShowToast(error.message || 'שגיאה ביצירת גיבוי', 'error'); } finally { setLoading(false); } }; const handleRestoreClick = (filename) => { setRestoreModal({ isOpen: true, filename }); }; const handleRestoreConfirm = async () => { console.log('Restore confirm clicked, filename:', restoreModal.filename); setRestoreModal({ isOpen: false, filename: '' }); setRestoring(true); try { console.log('Starting restore...'); const result = await restoreBackup(restoreModal.filename); console.log('Restore result:', result); onShowToast('שחזור הושלם בהצלחה! ♻️ מרענן את הדף...', 'success'); // Refresh page after 2 seconds to reload all data setTimeout(() => { window.location.reload(); }, 2000); } catch (error) { console.error('Restore error:', error); onShowToast(error.message || 'שגיאה בשחזור גיבוי', 'error'); setRestoring(false); } }; const formatBytes = (bytes) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]; }; const formatDate = (isoString) => { const date = new Date(isoString); return date.toLocaleString('he-IL', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); }; return (
| קובץ | תאריך | גודל | פעולות |
|---|---|---|---|
| {backup.filename} | {formatDate(backup.last_modified)} | {formatBytes(backup.size)} |
מוריד גיבוי...
משחזר מסד נתונים...
אנא המתן, התהליך עשוי לקחת מספר דקות
פעולה זו תמחק את כל הנתונים הנוכחיים!
האם אתה בטוח שברצונך לשחזר מהגיבוי:
{restoreModal.filename}