All files / apps/web/src/components Layout.jsx

0% Statements 0/23
0% Branches 0/42
0% Functions 0/11
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { Outlet, Link, useLocation, useNavigate } from 'react-router-dom'
import {
  Menu,
  X,
  LogOut,
  User,
  Settings,
  Crown,
  Home,
  Brain,
  Database,
  Bot,
  Info,
  CreditCard,
} from 'lucide-react'
import { useState, useEffect } from 'react'
import { useAuth } from '../contexts/AuthContext'
import OrgSwitcher from './OrgSwitcher'
 
import { useOrganizationInitialization } from '../hooks/useOrganizationInitialization'
import { useOrganizationStore } from '../store/organizationStore'
 
function NavLink({ to, icon: Icon, label, active, onClick }) {
  return (
    <Link
      to={to}
      onClick={onClick}
      className={`flex items-center gap-3 px-3 py-2 rounded-lg text-sm transition-colors ${
        active
          ? 'bg-indigo-50 text-indigo-700 font-medium'
          : 'text-gray-600 hover:bg-gray-100'
      }`}
    >
      {Icon && <Icon className="w-4 h-4 flex-shrink-0" />}
      {label}
    </Link>
  )
}
 
function Layout() {
  const [sidebarOpen, setSidebarOpen] = useState(false)
  const navigate = useNavigate()
  const location = useLocation()
  const { user, isAuthenticated, logout } = useAuth()
  const { currentOrganization } = useOrganizationStore()
 
  useOrganizationInitialization()
 
  useEffect(() => {
    window.scrollTo(0, 0)
  }, [location.pathname])
 
  const orgId = currentOrganization?.id
  const hasOrg = !!orgId
 
  const handleLogout = () => {
    logout()
    navigate('/login')
  }
 
  const nav = (
    <nav className="space-y-1 p-2 flex-1">
      <NavLink to={hasOrg ? `/${orgId}` : '/'} icon={Home} label="Accueil" active={hasOrg ? location.pathname === `/${orgId}` : location.pathname === '/'} />
      <NavLink to={hasOrg ? `/${orgId}/about` : '/about'} icon={Info} label="A propos" active={hasOrg ? location.pathname === `/${orgId}/about` : location.pathname === '/about'} />
      <NavLink to={hasOrg ? `/${orgId}/pricing` : '/pricing'} icon={CreditCard} label="Tarifs" active={hasOrg ? location.pathname === `/${orgId}/pricing` : location.pathname === '/pricing'} />
      {hasOrg && (
        <>
          <div className="pt-4 pb-2 px-3 text-xs font-semibold uppercase text-gray-400 tracking-wider">Extraction</div>
          <NavLink to={`/${orgId}/workflow-generic`} icon={Settings} label="Manuel" active={location.pathname.includes('workflow-generic')} />
          <NavLink to={`/${orgId}/workflow`} icon={Brain} label="Workflow" active={location.pathname.endsWith('/workflow')} />
          <div className="pt-4 pb-2 px-3 text-xs font-semibold uppercase text-gray-400 tracking-wider">Recherche</div>
          <NavLink to={`/${orgId}/knowledge-base`} icon={Database} label="Knowledge Base" active={location.pathname.includes('knowledge-base')} />
          <NavLink to={`/${orgId}/agent-chat`} icon={Bot} label="Agent Chat" active={location.pathname.includes('agent-chat')} />
        </>
      )}
    </nav>
  )
 
  return (
    <div className="min-h-screen bg-gray-50">
      {/* Mobile sidebar */}
      <div className={`fixed inset-0 z-50 lg:hidden ${sidebarOpen ? 'block' : 'hidden'}`}>
        <div className="fixed inset-0 bg-gray-900/50" onClick={() => setSidebarOpen(false)} />
        <div className="fixed inset-y-0 left-0 w-72 bg-white overflow-y-auto flex flex-col">
          <div className="flex items-center justify-between p-4 border-b">
            <span className="text-xl font-bold text-indigo-600">Nooloom</span>
            <button onClick={() => setSidebarOpen(false)}>
              <X className="w-6 h-6" />
            </button>
          </div>
          {nav}
 
          {/* Mobile auth section */}
          <div className="p-4 border-t space-y-3 mt-auto">
            {isAuthenticated && user?.role === 'admin' && (
              <Link
                to="/admin/users"
                onClick={() => setSidebarOpen(false)}
                className="flex items-center justify-center w-full px-3 py-2 text-sm text-purple-700 bg-purple-100 hover:bg-purple-200 rounded-lg transition-colors border border-purple-300"
              >
                <Crown className="w-4 h-4 mr-2" />
                Administration
              </Link>
            )}
 
            {isAuthenticated && (
              <div className="flex items-center space-x-2">
                <div className="flex-1 bg-white rounded-lg">
                  <OrgSwitcher />
                </div>
                {hasOrg && currentOrganization?.role === 'owner' && (
                  <Link
                    to={`/${orgId}/backoffice`}
                    onClick={() => setSidebarOpen(false)}
                    className="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-50 rounded-lg transition-colors"
                    title="Backoffice"
                  >
                    <Settings className="w-4 h-4" />
                  </Link>
                )}
              </div>
            )}
 
            {isAuthenticated ? (
              <Link
                to="/profile"
                onClick={() => setSidebarOpen(false)}
                className="flex items-center px-3 py-2 text-gray-700 hover:bg-slate-100 rounded-lg transition-colors border border-slate-200 hover:border-slate-300"
              >
                <div className="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center flex-shrink-0 mr-3">
                  <User className="w-4 h-4 text-indigo-600" />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="text-sm font-medium truncate">{user?.name}</p>
                  <p className="text-xs text-gray-500 truncate">{user?.email}</p>
                </div>
                <Settings className="w-4 h-4 text-gray-400" />
              </Link>
            ) : (
              <Link
                to="/login"
                onClick={() => setSidebarOpen(false)}
                className="flex items-center justify-center w-full px-3 py-2 text-sm text-white bg-indigo-600 rounded-lg hover:bg-indigo-700 transition-colors"
              >
                Connexion
              </Link>
            )}
          </div>
        </div>
      </div>
 
      {/* Desktop sidebar */}
      <div className="hidden lg:fixed lg:inset-y-0 lg:flex lg:w-72 lg:flex-col">
        <div className="flex flex-col flex-1 bg-white border-r">
          <div className="flex items-center h-16 px-6 border-b">
            <span className="text-xl font-bold text-indigo-600">Nooloom</span>
          </div>
          {nav}
 
          {/* Desktop auth section */}
          <div className="p-4 border-t space-y-3 mt-auto">
            {isAuthenticated && user?.role === 'admin' && (
              <Link
                to="/admin/users"
                className="flex items-center justify-center w-full px-3 py-2 text-sm text-purple-700 bg-purple-100 hover:bg-purple-200 rounded-lg transition-colors border border-purple-300"
              >
                <Crown className="w-4 h-4 mr-2" />
                Administration
              </Link>
            )}
 
            {isAuthenticated && (
              <div className="flex items-center space-x-2">
                <div className="flex-1 bg-white rounded-lg">
                  <OrgSwitcher />
                </div>
                {hasOrg && currentOrganization?.role === 'owner' && (
                  <Link
                    to={`/${orgId}/backoffice`}
                    className="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-50 rounded-lg transition-colors"
                    title="Backoffice"
                  >
                    <Settings className="w-4 h-4" />
                  </Link>
                )}
              </div>
            )}
 
            {isAuthenticated ? (
              <Link
                to="/profile"
                className="flex items-center px-3 py-2 text-gray-700 hover:bg-slate-100 rounded-lg transition-colors border border-slate-200 hover:border-slate-300"
              >
                <div className="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center flex-shrink-0 mr-3">
                  <User className="w-4 h-4 text-indigo-600" />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="text-sm font-medium truncate">{user?.name}</p>
                  <p className="text-xs text-gray-500 truncate">{user?.email}</p>
                </div>
                <Settings className="w-4 h-4 text-gray-400" />
              </Link>
            ) : (
              <Link
                to="/login"
                className="flex items-center justify-center w-full px-3 py-2 text-sm text-white bg-indigo-600 rounded-lg hover:bg-indigo-700 transition-colors"
              >
                Connexion
              </Link>
            )}
          </div>
        </div>
      </div>
 
      {/* Main content */}
      <div className="lg:pl-72">
        {/* Mobile header */}
        <div className="sticky top-0 z-40 flex items-center justify-between h-16 px-4 bg-white border-b lg:hidden">
          <button onClick={() => setSidebarOpen(true)} className="p-2">
            <Menu className="w-6 h-6" />
          </button>
          <span className="text-lg font-semibold">Nooloom</span>
          <div className="w-8">
            {isAuthenticated && (
              <div className="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center">
                <User className="w-5 h-5 text-indigo-600" />
              </div>
            )}
          </div>
        </div>
 
        {/* Page content */}
        <main className="p-0">
          <Outlet />
        </main>
      </div>
    </div>
  )
}
 
export default Layout