import type { FC, ReactNode } from 'react'; import { useEffect, useMemo, useState } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import { LayoutDashboard, Calendar, Users, Stethoscope, Briefcase, Wallet, CreditCard, ShoppingCart, Receipt, Store, BarChart3, Settings, Menu, X, ChevronRight, LogOut, MessageSquare, Target, Cake, Boxes, ClipboardList, ClipboardPlus, Banknote, Percent, Star, Pencil, ChevronUp, ChevronDown, MessageCircle, CircleDollarSign, PanelsTopLeft, } from 'lucide-react'; import { Button, Modal, Input } from './ui'; import QuickLinksDock, { QuickLinksModePicker } from './QuickLinksDock'; import GlobalSearch from './GlobalSearch'; import ThemePicker from './ThemePicker'; import NotificationBell from './NotificationBell'; import { useAuth, ROLE_LABELS } from '../lib/auth'; import { useChatUnread } from '../lib/chat'; import { useQuickLinks } from '../lib/quicklinks'; import { odooApi } from '../services/odoo'; interface LayoutProps { children: ReactNode; title: string; subtitle?: string; } const LOGO_SKEEN = '/skeen-brand/logos/Logo%20Completo%20Blanco.png'; const menuGroups = [ { label: 'Principal', items: [ { label: 'Dashboard', icon: LayoutDashboard, href: '/' }, { label: 'Agenda', icon: Calendar, href: '/agenda' }, ], }, { label: 'Clínica', items: [ { label: 'Pacientes', icon: Users, href: '/pacientes' }, { label: 'Visitas', icon: ClipboardList, href: '/visitas' }, { label: 'Consultas', icon: ClipboardPlus, href: '/consultas' }, { label: 'Mis ventas', icon: CircleDollarSign, href: '/mis-ventas' }, { label: 'Médicos', icon: Stethoscope, href: '/medicos' }, { label: 'Servicios', icon: Briefcase, href: '/servicios' }, ], }, { label: 'Operaciones', items: [ { label: 'Punto de Venta', icon: Store, href: '/pos' }, { label: 'Ventas', icon: ShoppingCart, href: '/ventas' }, { label: 'Pagos', icon: CreditCard, href: '/pagos' }, { label: 'Adeudos', icon: Banknote, href: '/reportes?tab=adeudos' }, { label: 'Monedero', icon: Wallet, href: '/monedero' }, { label: 'Inventario', icon: Boxes, href: '/inventario' }, { label: 'Cortes de Caja', icon: Receipt, href: '/cortes' }, ], }, { label: 'Analítica', items: [ { label: 'Reportes', icon: BarChart3, href: '/reportes' }, { label: 'Comisiones', icon: Percent, href: '/reportes?tab=comisiones' }, { label: 'Configuración', icon: Settings, href: '/configuracion' }, ], }, { label: 'Comunicación', items: [ { label: 'Chat', icon: MessageCircle, href: '/chat' }, { label: 'Cumpleañeros', icon: Cake, href: '/cumpleanos' }, ], }, { label: 'WACRM', items: [ { label: 'Mensajes', icon: MessageSquare, href: '/wacrm/messages' }, { label: 'Leads', icon: Target, href: '/wacrm/leads' }, ], }, ]; export const SkeenLayout: FC = ({ children, title, subtitle }) => { const location = useLocation(); const navigate = useNavigate(); const { user, canSee, hasRole, logout } = useAuth(); const [sidebarOpen, setSidebarOpen] = useState(false); const chatUnread = useChatUnread(); // Sidebar colapsable (modo iconos). Preferencia persistente; en pantallas // medianas (menores a 1440px) el estado inicial es colapsado. const [collapsed, setCollapsed] = useState(() => { const saved = localStorage.getItem('skeen_sidebar_collapsed'); if (saved !== null) return saved === '1'; return window.innerWidth < 1440; }); const toggleCollapsed = () => { setCollapsed((v) => { localStorage.setItem('skeen_sidebar_collapsed', v ? '0' : '1'); return !v; }); }; const visibleGroups = useMemo( () => menuGroups .map((g) => ({ ...g, items: g.items.filter((it) => canSee(it.href)) })) .filter((g) => g.items.length > 0), [canSee, user] ); // Accesos rápidos personalizables (localStorage por usuario) const quick = useQuickLinks(); const dockItems = useMemo( () => menuGroups.flatMap((g) => g.items).filter((it) => canSee(it.href)), [canSee] ); const quickItems = useMemo( () => quick.links .filter((href) => canSee(href)) .map((href) => { for (const g of menuGroups) { const found = g.items.find((it) => it.href === href); if (found) return found; } return null; }) .filter((it): it is (typeof menuGroups)[number]['items'][number] => it !== null), [quick.links, canSee] ); const initials = (user?.name || 'US') .split(' ') .filter(Boolean) .slice(0, 2) .map((w) => w[0]?.toUpperCase()) .join(''); const handleLogout = async () => { await logout(); navigate('/login', { replace: true }); }; const [goal, setGoal] = useState<{ goal: number; current: number; pct: number } | null>(null); const [goalModalOpen, setGoalModalOpen] = useState(false); const [goalInput, setGoalInput] = useState(''); const [goalSaving, setGoalSaving] = useState(false); useEffect(() => { let alive = true; const fetchGoal = () => { odooApi.getSalesGoal() .then((res) => { if (alive && res.status === 'success') { setGoal({ goal: res.goal, current: res.current, pct: res.pct }); } }) .catch(() => {}); }; fetchGoal(); const id = setInterval(fetchGoal, 60000); return () => { alive = false; clearInterval(id); }; }, []); const openGoalModal = () => { setGoalInput(goal?.goal ? String(goal.goal) : ''); setGoalModalOpen(true); }; const saveGoal = async () => { const v = parseFloat(goalInput); if (Number.isNaN(v) || v < 0) return; try { setGoalSaving(true); await odooApi.setSalesGoal(v); setGoal((g) => (g ? { ...g, goal: v, pct: g.current && v ? (g.current / v) * 100 : 0 } : g)); setGoalModalOpen(false); } catch { // ignore } finally { setGoalSaving(false); } }; const fmtMoney = (n: number) => n >= 1000000 ? `$${(n / 1000000).toFixed(1)}M` : n >= 1000 ? `$${(n / 1000).toFixed(0)}k` : `$${n.toFixed(0)}`; const pctClamped = Math.min(goal?.pct ?? 0, 100); useEffect(() => { if (!sidebarOpen) return; const originalOverflow = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = originalOverflow; }; }, [sidebarOpen]); const isActive = (href: string) => { if (href === '/') return location.pathname === '/'; return location.pathname === href || location.pathname.startsWith(`${href}/`); }; const SidebarContent = ({ collapsed = false, showToggle = true }: { collapsed?: boolean; showToggle?: boolean }) => ( <>
SKEEN {!collapsed && showToggle && ( )}
{initials || 'US'}
{!collapsed && (

{user?.name || 'Usuario'}

{user ? ROLE_LABELS[user.role] : ''}

)}
{collapsed && ( )}
); return (
{sidebarOpen && (
setSidebarOpen(false)} aria-hidden="true" /> )}

{title}

{subtitle &&

{subtitle}

}
{goal && goal.goal > 0 && ( )} {goal && goal.goal <= 0 && hasRole('admin') && ( )}
{children}
setGoalModalOpen(false)} title="Meta de ventas del mes" maxWidth="sm" footer={ <> } >

Define el objetivo de ventas (MXN) para el mes en curso. La barra del header muestra el avance en tiempo real.

setGoalInput(e.target.value)} placeholder="ej. 500000" />
); }; export default SkeenLayout;