import React, { useState, useEffect } from 'react'; import { BrowserRouter as Router, Routes, Route, Link, useNavigate } from 'react-router-dom'; import { AppBar, Box, CssBaseline, Drawer, IconButton, List, ListItem, ListItemButton, ListItemIcon, ListItemText, Toolbar, Typography, Container, Button, Menu, MenuItem } from '@mui/material'; import { Menu as MenuIcon, Email as EmailIcon, Drafts as DraftsIcon, History as HistoryIcon, Description as DescriptionIcon, Dashboard as DashboardIcon, Settings as SettingsIcon, Home as HomeIcon, AccountCircle as AccountCircleIcon, AdminPanelSettings as AdminIcon } from '@mui/icons-material'; import axios from 'axios'; import WorkspaceDashboard from './components/WorkspaceDashboard'; import Dashboard from './components/Dashboard'; import EmailList from './components/EmailList'; import EmailDetail from './components/EmailDetail'; import HistoricalEmails from './components/HistoricalEmails'; import EnrollmentDocs from './components/EnrollmentDocs'; import Settings from './components/Settings'; import Login from './components/Login'; import ChangePasswordDialog from './components/ChangePasswordDialog'; import AdminPanel from './components/AdminPanel'; // Configure axios to send cookies with requests axios.defaults.withCredentials = true; const drawerWidth = 240; function AppContent() { const navigate = useNavigate(); const [mobileOpen, setMobileOpen] = useState(false); const [currentWorkspace, setCurrentWorkspace] = useState(null); const [workspaceInfo, setWorkspaceInfo] = useState(null); const [currentUser, setCurrentUser] = useState(null); const [authChecked, setAuthChecked] = useState(false); const [changePasswordOpen, setChangePasswordOpen] = useState(false); const [mustChangePassword, setMustChangePassword] = useState(false); const [accountMenuAnchor, setAccountMenuAnchor] = useState(null); // Check authentication on mount useEffect(() => { checkAuth(); }, []); const checkAuth = async () => { try { const response = await axios.get('/api/auth/me'); setCurrentUser(response.data); } catch (err) { // Not logged in setCurrentUser(null); } finally { setAuthChecked(true); } }; const handleLoginSuccess = (user, mustChange) => { setCurrentUser(user); setMustChangePassword(mustChange); if (mustChange) { setChangePasswordOpen(true); } }; const handleLogout = async () => { try { await axios.post('/api/auth/logout'); setCurrentUser(null); setCurrentWorkspace(null); setAccountMenuAnchor(null); navigate('/'); // Navigate to root after logout } catch (err) { console.error('Logout error:', err); } }; const handlePasswordChanged = async () => { setChangePasswordOpen(false); setMustChangePassword(false); // Refresh user data to get updated must_change_password flag try { const response = await axios.get('/api/auth/me'); setCurrentUser(response.data); } catch (err) { console.error('Error refreshing user data:', err); } }; useEffect(() => { if (currentWorkspace) { loadWorkspaceInfo(); } }, [currentWorkspace]); const loadWorkspaceInfo = async () => { try { const res = await fetch(`/api/workspaces/${currentWorkspace}`); const data = await res.json(); setWorkspaceInfo(data); } catch (err) { console.error('Errore caricamento workspace:', err); } }; const handleSelectWorkspace = (workspaceId) => { setCurrentWorkspace(workspaceId); }; const handleBackToWorkspaces = () => { setCurrentWorkspace(null); setWorkspaceInfo(null); }; const handleDrawerToggle = () => { setMobileOpen(!mobileOpen); }; const menuItems = [ { text: 'Dashboard', icon: , path: '/' }, { text: 'Email', icon: , path: '/emails' }, { text: 'Email Storiche', icon: , path: '/historical-emails' }, { text: 'Documenti di Contesto', icon: , path: '/enrollment-docs' }, { text: 'Impostazioni', icon: , path: '/settings' } ]; const drawer = (
Email RAG System {menuItems.map((item) => ( {item.icon} ))}
); // Show login if not authenticated if (!authChecked) { return
Loading...
; } if (!currentUser) { return ( <> ); } // Show workspace dashboard if no workspace selected if (!currentWorkspace) { return ( <> ITS MAKER ACADEMY - Sistema Risposta Email {/* Admin Panel Button */} {currentUser.is_admin && ( )} {/* Account Menu */} setAccountMenuAnchor(e.currentTarget)} > setAccountMenuAnchor(null)} > {currentUser.username} { setChangePasswordOpen(true); setAccountMenuAnchor(null); }}> Change Password Logout } /> :
Access Denied
} />
); } // Show main app with workspace sidebar return ( <> {workspaceInfo && ( {workspaceInfo.emoji} {workspaceInfo.title} )} {/* Account Menu */} setAccountMenuAnchor(e.currentTarget)} > setAccountMenuAnchor(null)} > {currentUser.username} { setChangePasswordOpen(true); setAccountMenuAnchor(null); }}> Change Password Logout {drawer} {drawer} } /> } /> } /> } /> } /> } /> :
Access Denied
} />
); } function App() { return ( ); } export default App;