/** * 側邊欄導航 (動態功能列表) */ 'use client' import Link from 'next/link' import { usePathname } from 'next/navigation' import { signOut, useSession } from 'next-auth/react' import { useState, useEffect } from 'react' import apiClient from '@/lib/api-client' import { systemFunctionService, SystemFunctionNode } from '@/services/systemFunction.service' // 預設圖示 const DefaultIcon = () => ( ) // 節點分類圖示 const NodeIcon = () => ( ) interface MenuItemProps { item: SystemFunctionNode level: number pathname: string } function MenuItem({ item, level, pathname }: MenuItemProps) { const [isExpanded, setIsExpanded] = useState(true) const hasChildren = item.children && item.children.length > 0 const isNode = item.function_type === 1 const route = systemFunctionService.codeToRoute(item.code) const isActive = pathname === route || pathname.startsWith(route + '/') if (isNode) { // NODE: 顯示為分類標題 return (
{/* 子選單 */} {hasChildren && isExpanded && (
{item.children.map(child => ( ))}
)}
) } // FUNCTION: 顯示為可點擊連結 return ( 0 ? 'ml-2' : '' } ${ isActive ? 'bg-gray-800 text-white' : 'text-gray-300 hover:bg-gray-800 hover:text-white' }`} > {item.function_icon ? ( {item.function_icon} ) : ( )} {item.name} ) } export function Sidebar() { const pathname = usePathname() const { data: session } = useSession() const [tenantName, setTenantName] = useState('') const [menuTree, setMenuTree] = useState([]) const [loading, setLoading] = useState(true) // 取得租戶資訊 useEffect(() => { const fetchTenantInfo = async () => { try { const data: any = await apiClient.get('/tenants/current') setTenantName(data.name || data.code) } catch (error) { console.error('Failed to fetch tenant info:', error) } } if (session?.user) { fetchTenantInfo() } }, [session]) // 載入功能列表 useEffect(() => { const loadMenu = async () => { try { // 直接從後端 API 取得租戶資訊 let isSysmana = false try { const tenantResponse: any = await apiClient.get('/tenants/current') isSysmana = tenantResponse.is_sysmana || false console.log('[Sidebar] Tenant is_sysmana:', isSysmana) } catch (error) { console.error('[Sidebar] Failed to fetch tenant info:', error) // 如果取得失敗,嘗試從 session 取得 isSysmana = (session?.user as any)?.tenant?.is_sysmana || false } console.log('[Sidebar] Loading menu with is_sysmana:', isSysmana) const tree = await systemFunctionService.getMenuTree(isSysmana) console.log('[Sidebar] Loaded menu tree:', tree.length, 'items') setMenuTree(tree) } catch (error) { console.error('[Sidebar] Failed to load menu:', error) // 失敗時使用空陣列 setMenuTree([]) } finally { setLoading(false) } } if (session?.user) { loadMenu() } }, [session]) return (
{/* Logo */}

HR Portal

{tenantName && (

({tenantName})

)}
{/* Navigation */} {/* User Info & Logout */}
{session?.user && (

{session.user.name}

{session.user.email}

)}
) }