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

0% Statements 0/60
0% Branches 0/113
0% Functions 0/13
0% Lines 0/55

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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { Link, useLocation } from 'react-router-dom'
import { useState } from 'react'
 
// Color mapping for Tailwind classes - Light theme (frontoffice)
const lightColorStyles = {
  blue: {
    bg: 'bg-blue-50',
    text: 'text-blue-700',
    border: 'border-blue-500',
    icon: 'text-blue-600',
    dot: 'bg-blue-500'
  },
  purple: {
    bg: 'bg-purple-50',
    text: 'text-purple-700',
    border: 'border-purple-500',
    icon: 'text-purple-600',
    dot: 'bg-purple-500'
  },
  amber: {
    bg: 'bg-amber-50',
    text: 'text-amber-700',
    border: 'border-amber-500',
    icon: 'text-amber-600',
    dot: 'bg-amber-500'
  },
  green: {
    bg: 'bg-green-50',
    text: 'text-green-700',
    border: 'border-green-500',
    icon: 'text-green-600',
    dot: 'bg-green-500'
  },
  indigo: {
    bg: 'bg-indigo-50',
    text: 'text-indigo-700',
    border: 'border-indigo-500',
    icon: 'text-indigo-600',
    dot: 'bg-indigo-500'
  },
  gray: {
    bg: 'bg-gray-50',
    text: 'text-gray-700',
    border: 'border-gray-500',
    icon: 'text-gray-600',
    dot: 'bg-gray-500'
  }
}
 
// Color mapping for Tailwind classes - Dark theme (backoffice)
const darkColorStyles = {
  indigo: {
    text: 'text-indigo-400',
    indicator: 'bg-indigo-500',
    active: 'border-indigo-500'
  },
  blue: {
    text: 'text-blue-400',
    indicator: 'bg-blue-500',
    active: 'border-blue-500'
  },
  purple: {
    text: 'text-purple-400',
    indicator: 'bg-purple-500',
    active: 'border-purple-500'
  },
  amber: {
    text: 'text-amber-400',
    indicator: 'bg-amber-500',
    active: 'border-amber-500'
  },
  green: {
    text: 'text-green-400',
    indicator: 'bg-green-500',
    active: 'border-green-500'
  },
  gray: {
    text: 'text-gray-400',
    indicator: 'bg-gray-500',
    active: 'border-gray-500'
  },
  red: {
    text: 'text-red-400',
    indicator: 'bg-red-500',
    active: 'border-red-500'
  }
}
 
function Navigation({
  sections = [],
  theme = 'light',
  collapsible = false,
  collapsed = false,
  showSubtitles = false,
  expandAll = false,
  onItemClick,
  className = '',
  basePath = '',
  userRole = null
}) {
  const location = useLocation()
  
  // Filter sections based on user role
  const visibleSections = sections.filter(section => {
    if (!section.roles) return true
    return section.roles.includes(userRole)
  })
  
  const resolveHref = (href) => {
    if (!href || href === '/backoffice') return basePath || href
    // Absolute paths (starting with /) are used as-is - for frontoffice
    if (href.startsWith('/')) return href
    // Relative paths get basePath prepended - for backoffice
    if (!basePath) return href
    const baseEndsWithSlash = basePath.endsWith('/')
    const hrefStartsWithSlash = href.startsWith('/')
    if (baseEndsWithSlash || hrefStartsWithSlash) {
      return `${basePath}${href}`
    }
    return `${basePath}/${href}`
  }
  
  const [expandedSections, setExpandedSections] = useState(() => {
    // Auto-expand section containing current path, or all sections if expandAll is true
    // Sections without title are always "expanded" (always visible)
    const currentPath = location.pathname
    const sectionsState = {}
    sections.forEach((section, idx) => {
      if (!section.title) {
        // Sections without title are always visible
        sectionsState[idx] = true
      } else if (expandAll) {
        // Expand all sections when expandAll is true
        sectionsState[idx] = true
      } else {
        const hasActiveItem = section.items?.some(item => {
          const resolvedHref = resolveHref(item.href)
          const isItemActive = currentPath === resolvedHref ||
            (item.href !== '/backoffice' && currentPath.startsWith(resolvedHref + '/'))
          return isItemActive
        })
        sectionsState[idx] = hasActiveItem
      }
    })
    return sectionsState
  })
 
  const toggleSection = (idx) => {
    setExpandedSections(prev => ({
      ...prev,
      [idx]: !prev[idx]
    }))
  }
 
  const isLight = theme === 'light'
  const colorStyles = isLight ? lightColorStyles : darkColorStyles
 
  return (
    <nav className={`space-y-1 ${className}`}>
      {visibleSections.map((section, idx) => {
        const items = section.items
        const color = section.color || 'gray'
        // Use the index from visibleSections but track original section index for expansion state
        const originalIdx = sections.indexOf(section)
        const isExpanded = expandedSections[originalIdx]
        
        // Check if any item in this section is active
        const hasActiveItem = items?.some(item => {
          const resolvedHref = resolveHref(item.href)
          const isItemActive = location.pathname === resolvedHref || 
            (item.href !== '/backoffice' && location.pathname.startsWith(resolvedHref + '/')) ||
            ((item.href === '' || item.href === '/') && location.pathname === basePath)
          return isItemActive
        })
        
        const sectionColor = colorStyles[color] || colorStyles.gray
        
        // If no title, render items directly without section header
        if (!section.title) {
          return (
            <div key={idx} className="mb-2 space-y-1">
              {items?.map((item) => {
                const Icon = item.icon
                const resolvedHref = resolveHref(item.href)
                const isActive = location.pathname === resolvedHref || 
                  (item.href !== '/backoffice' && location.pathname.startsWith(resolvedHref + '/')) ||
                  (item.href === '/' && location.pathname === basePath)
                
                return (
                  <Link
                    key={item.name}
                    to={resolvedHref}
                    onClick={onItemClick}
                    className={`flex items-center px-4 py-2 rounded-lg transition-all text-sm ${
                      isLight
                        ? `${isActive 
                            ? `bg-gray-100 text-gray-900 border-l-4 border-gray-500` 
                            : 'text-gray-600 hover:bg-gray-50 border-l-4 border-transparent'
                          }`
                        : `${isActive 
                            ? `bg-slate-700 text-white border-l-4 border-gray-500` 
                            : 'text-slate-300 hover:bg-slate-800 hover:text-white border-l-4 border-transparent'
                          }`
                    }`}
                  >
                    {Icon && <Icon className={`w-5 h-5 flex-shrink-0 ${isLight ? (isActive ? 'text-gray-700' : 'text-gray-400') : ''}`} />}
                    <div className={`flex-1 min-w-0 ${Icon ? 'ml-3' : ''}`}>
                      <div className="text-sm font-medium truncate">{item.name}</div>
                    </div>
                  </Link>
                )
              })}
            </div>
          )
        }
 
        return (
          <div key={idx} className="mb-2">
            {/* Section Header */}
            <button
              onClick={() => toggleSection(idx)}
              disabled={collapsed}
              className={`w-full flex items-center justify-between px-4 py-2 text-xs font-semibold uppercase tracking-wider transition-colors rounded-lg ${
                isLight 
                  ? 'text-gray-500 hover:text-gray-700' 
                  : `hover:bg-slate-800 ${hasActiveItem ? 'text-white' : 'text-gray-400'}`
              } ${collapsed ? 'cursor-default' : 'cursor-pointer'}`}
            >
              <div className="flex items-center">
                {color && (
                  <span className={`w-2 h-2 rounded-full mr-2 ${
                    isLight ? sectionColor.dot : sectionColor.indicator
                  }`}></span>
                )}
                <span className={collapsed ? 'hidden' : ''}>{section.title}</span>
              </div>
              {!collapsed && (
                <svg 
                  className={`w-4 h-4 transition-transform duration-200 ${isExpanded ? 'rotate-90' : ''}`} 
                  fill="none" 
                  viewBox="0 0 24 24" 
                  stroke="currentColor"
                >
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                </svg>
              )}
            </button>
            
            {/* Section Items */}
            {isExpanded && !collapsed && (
              <div className={`space-y-1 mt-1 ${isLight ? '' : 'ml-0'}`}>
                {items?.map((item) => {
                  const Icon = item.icon
                  const resolvedHref = resolveHref(item.href)
                  // Check if active: exact match, starts with href + '/', or is basePath (for root items like '/' or '')
                  const isActive = location.pathname === resolvedHref || 
                    (item.href !== '/backoffice' && location.pathname.startsWith(resolvedHref + '/')) ||
                    ((item.href === '' || item.href === '/') && location.pathname === basePath)
                  
                  return (
                    <Link
                      key={item.name}
                      to={resolvedHref}
                      onClick={onItemClick}
                      className={`flex items-center px-4 py-2 rounded-lg transition-all text-sm ${
                        isLight
                          ? `${isActive 
                              ? `${sectionColor.bg} ${sectionColor.text} border-l-4 ${sectionColor.border}` 
                              : 'text-gray-600 hover:bg-gray-50 border-l-4 border-transparent'
                            }`
                          : `${isActive 
                              ? `bg-indigo-600 text-white border-l-4 ${sectionColor.active || 'border-indigo-500'}` 
                              : 'text-slate-300 hover:bg-slate-800 hover:text-white border-l-4 border-transparent'
                            }`
                      }`}
                    >
                      {Icon && <Icon className={`w-5 h-5 flex-shrink-0 ${isLight ? (isActive ? sectionColor.icon : 'text-gray-400') : ''}`} />}
                      <div className={`flex-1 min-w-0 ${Icon ? 'ml-3' : ''}`}>
                        <div className="text-sm font-medium truncate">{item.name}</div>
                        {showSubtitles && item.subtitle && (
                          <div className={`text-xs truncate ${isLight ? 'text-gray-400' : 'text-slate-400'}`}>
                            {item.subtitle}
                          </div>
                        )}
                      </div>
                    </Link>
                  )
                })}
              </div>
            )}
          </div>
        )
      })}
    </nav>
  )
}
 
export default Navigation