import React, { useState, useEffect } from 'react'; import { NavLink, useLocation, useNavigate } from 'react-router-dom'; import { LayoutDashboard, Users, UserCircle, Bell, Search, Menu, X, LogOut, Hexagon, Settings, Building2, Sun, Moon, Loader2, Layers, ChevronLeft, ChevronRight, Key, Target } from 'lucide-react'; import { getAttendances, getUsers, getUserById, logout, searchGlobal, getNotifications, markNotificationAsRead, markAllNotificationsAsRead, deleteNotification, clearAllNotifications, returnToSuperAdmin } from '../services/dataService'; import { User } from '../types'; import notificationSound from '../src/assets/audio/notification.mp3'; const SidebarItem = ({ to, icon: Icon, label, collapsed }: { to: string, icon: any, label: string, collapsed: boolean }) => ( `flex items-center gap-3 px-4 py-3 rounded-xl transition-all duration-200 group ${ isActive ? 'bg-brand-yellow text-zinc-950 font-semibold shadow-md shadow-brand-yellow/20' : 'text-zinc-500 dark:text-dark-muted hover:bg-zinc-100 dark:hover:bg-dark-border hover:text-zinc-900 dark:hover:text-dark-text' }` } > {!collapsed && {label}} ); export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [isSidebarCollapsed, setIsSidebarCollapsed] = useState(() => { return localStorage.getItem('ctms_sidebar_collapsed') === 'true'; }); const [isDark, setIsDark] = useState(document.documentElement.classList.contains('dark')); const location = useLocation(); const navigate = useNavigate(); const [currentUser, setCurrentUser] = useState(null); const toggleSidebar = () => { const newState = !isSidebarCollapsed; setIsSidebarCollapsed(newState); localStorage.setItem('ctms_sidebar_collapsed', String(newState)); }; // Search State const [searchQuery, setSearchQuery] = useState(''); const [searchResults, setSearchResults] = useState<{ members: User[], teams: any[], attendances: any[], organizations?: any[] }>({ members: [], teams: [], attendances: [], organizations: [] }); const [isSearching, setIsSearching] = useState(false); const [showSearchResults, setShowSearchResults] = useState(false); // Notifications State const [notifications, setNotifications] = useState([]); const [showNotifications, setShowNotifications] = useState(false); const unreadCount = notifications.filter(n => !n.is_read).length; const previousUnreadCountRef = React.useRef(0); const isInitialLoadRef = React.useRef(true); // Pre-initialize audio to ensure it's loaded and ready const audioRef = React.useRef(null); const playNotificationSound = () => { if (currentUser?.sound_enabled !== false && audioRef.current) { // Reset time to 0 to allow rapid replays audioRef.current.currentTime = 0; const playPromise = audioRef.current.play(); if (playPromise !== undefined) { playPromise.catch(e => console.log('Audio play blocked by browser policy:', e)); } } }; const loadNotifications = async () => { const data = await getNotifications(); const newUnreadCount = data.filter((n: any) => !n.is_read).length; // Only play sound if it's NOT the first load AND the count actually increased if (!isInitialLoadRef.current && newUnreadCount > previousUnreadCountRef.current) { playNotificationSound(); } setNotifications(data); previousUnreadCountRef.current = newUnreadCount; isInitialLoadRef.current = false; }; const handleBellClick = async () => { const willOpen = !showNotifications; setShowNotifications(willOpen); if (willOpen && unreadCount > 0) { // Optimistic update setNotifications(prev => prev.map(n => ({ ...n, is_read: true }))); previousUnreadCountRef.current = 0; await markAllNotificationsAsRead(); loadNotifications(); } }; useEffect(() => { const delayDebounceFn = setTimeout(async () => { if (searchQuery.length >= 2) { setIsSearching(true); const results = await searchGlobal(searchQuery); setSearchResults(results); setIsSearching(false); setShowSearchResults(true); } else { setSearchResults({ members: [], teams: [], attendances: [], organizations: [] }); setShowSearchResults(false); } }, 300); return () => clearTimeout(delayDebounceFn); }, [searchQuery]); const getSearchPlaceholder = () => { if (currentUser?.role === 'super_admin') return 'Buscar membros, equipes, atendimentos ou organizações...'; if (currentUser?.role === 'agent') return 'Buscar atendimentos...'; return 'Buscar membros, equipes ou atendimentos...'; }; useEffect(() => { const fetchCurrentUser = async () => { const storedUserId = localStorage.getItem('ctms_user_id'); if (!storedUserId) { navigate('/login'); return; } try { const user = await getUserById(storedUserId); if (user) { setCurrentUser(user); } else { navigate('/login'); } } catch (err) { console.error("Layout fetch failed:", err); } }; fetchCurrentUser(); loadNotifications(); const interval = setInterval(loadNotifications, 10000); return () => clearInterval(interval); }, [navigate]); const handleLogout = () => { logout(); navigate('/login'); }; const toggleDarkMode = () => { const newDark = !isDark; setIsDark(newDark); if (newDark) { document.documentElement.classList.add('dark'); document.cookie = "dark_mode=1; path=/; max-age=31536000"; } else { document.documentElement.classList.remove('dark'); document.cookie = "dark_mode=0; path=/; max-age=31536000"; } }; // Simple title mapping based on route const getPageTitle = () => { if (location.pathname === '/') return 'Dashboard'; if (location.pathname.includes('/admin/users')) return 'Membros'; if (location.pathname.includes('/admin/teams')) return 'Times'; if (location.pathname.includes('/users/')) return 'Histórico do Usuário'; if (location.pathname.includes('/attendances')) return 'Detalhes do Atendimento'; if (location.pathname.includes('/super-admin')) return 'Gestão de Organizações'; if (location.pathname.includes('/profile')) return 'Meu Perfil'; return 'CTMS'; }; if (!currentUser) return null; const isSuperAdmin = currentUser.role === 'super_admin'; return (
{/* Sidebar */} {/* Main Content */}
{/* Header */}
{/* Search Bar - Moved to left/center and made wider */}
{isSearching ? : } setSearchQuery(e.target.value)} onFocus={() => searchQuery.length >= 2 && setShowSearchResults(true)} className="bg-transparent border-none outline-none text-sm ml-3 w-full text-zinc-700 dark:text-dark-text placeholder-zinc-400 dark:placeholder-dark-muted" />
{/* Search Results Dropdown */} {showSearchResults && (
{/* Organizations Section (Super Admin only) */} {searchResults.organizations && searchResults.organizations.length > 0 && (
Organizações
{searchResults.organizations.map(o => ( ))}
)} {/* Members Section */} {searchResults.members.length > 0 && (
Membros
{searchResults.members.map(m => { const backendUrl = import.meta.env.PROD ? '' : 'http://localhost:3001'; const avatarSrc = m.avatar_url ? (m.avatar_url.startsWith('http') ? m.avatar_url : `${backendUrl}${m.avatar_url}`) : `https://ui-avatars.com/api/?name=${encodeURIComponent(m.name)}&background=random`; return ( ); })}
)} {/* Teams Section */} {searchResults.teams.length > 0 && (
Equipes
{searchResults.teams.map(t => ( ))}
)} {/* Attendances Section */} {searchResults.attendances.length > 0 && (
Atendimentos
{searchResults.attendances.map(a => ( ))}
)} {searchResults.members.length === 0 && searchResults.teams.length === 0 && searchResults.attendances.length === 0 && (!searchResults.organizations || searchResults.organizations.length === 0) && (
Nenhum resultado encontrado para "{searchQuery}"
)}
)}
{/* Dark Mode Toggle */} {/* Notifications */}
{showNotifications && (

Notificações

{unreadCount > 0 && ( )} {notifications.length > 0 && ( )}
{notifications.length > 0 ? ( notifications.map(n => (
{ if (!n.is_read) await markNotificationAsRead(n.id); if (n.link) navigate(n.link); setShowNotifications(false); loadNotifications(); }} >
{n.type === 'success' ? 'SUCESSO' : n.type === 'warning' ? 'AVISO' : n.type === 'error' ? 'ERRO' : 'INFO'} {new Date(n.created_at).toLocaleDateString('pt-BR')}
{n.title}

{n.message}

{/* Delete Button */}
)) ) : (
Nenhuma notificação por enquanto.
)}
)}
{/* Close notifications when clicking outside */} {showNotifications &&
setShowNotifications(false)} />}
{/* Scrollable Content Area */}
{children}
{/* Overlay for mobile */} {isMobileMenuOpen && (
setIsMobileMenuOpen(false)} /> )} {/* Hidden Audio Player for Notifications */}
); };