import { useState, useEffect, useCallback } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import './Navigation.css'; import { LogoutWithOptions, GetLocalDataSize } from '../../wailsjs/go/app/Application'; function Navigation() { const location = useLocation(); const navigate = useNavigate(); const [showLogoutConfirm, setShowLogoutConfirm] = useState(false); const [isLoggingOut, setIsLoggingOut] = useState(false); const [deleteLocalData, setDeleteLocalData] = useState(true); // Default to delete for security const [localDataSize, setLocalDataSize] = useState(0); const isActive = (path) => { return location.pathname === path || location.pathname.startsWith(path + '/'); }; // Format bytes to human-readable size 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 parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; const handleLogoutClick = (e) => { e.preventDefault(); // Get local data size when opening the modal GetLocalDataSize() .then((size) => { setLocalDataSize(size); }) .catch((error) => { console.error('Failed to get local data size:', error); setLocalDataSize(0); }); setShowLogoutConfirm(true); }; const handleLogoutConfirm = () => { setIsLoggingOut(true); LogoutWithOptions(deleteLocalData) .then(() => { // Reset state before navigating setDeleteLocalData(true); navigate('/login'); }) .catch((error) => { console.error('Logout failed:', error); alert('Logout failed: ' + error.message); setIsLoggingOut(false); setShowLogoutConfirm(false); }); }; const handleLogoutCancel = useCallback(() => { if (!isLoggingOut) { setShowLogoutConfirm(false); setDeleteLocalData(true); // Reset to default } }, [isLoggingOut]); // Handle Escape key to close modal useEffect(() => { const handleKeyDown = (e) => { if (e.key === 'Escape' && showLogoutConfirm && !isLoggingOut) { handleLogoutCancel(); } }; if (showLogoutConfirm) { document.addEventListener('keydown', handleKeyDown); } return () => { document.removeEventListener('keydown', handleKeyDown); }; }, [showLogoutConfirm, isLoggingOut, handleLogoutCancel]); return ( <> {/* Logout Confirmation Modal */} {showLogoutConfirm && (
e.stopPropagation()} style={{ backgroundColor: 'white', borderRadius: '8px', padding: '30px', maxWidth: '500px', width: '90%', boxShadow: '0 4px 20px rgba(0, 0, 0, 0.3)', }}>

Sign Out

You are about to sign out. You'll need to log in again next time.

{/* Security Warning */}

Security Notice

For your security, we recommend deleting locally saved data when signing out. This includes your cached files and metadata{localDataSize > 0 ? ` (${formatBytes(localDataSize)})` : ''}. If you keep local data, anyone with access to this device may be able to view your files when you log in again.

{/* Data deletion options */}
)} ); } export default Navigation;