import { useEffect, useState } from 'react'; import type { PageId } from '../../lib/urlState'; import type { AuthUser } from '../../App'; interface TopBarProps { currentPage: PageId; onNavigate: (page: PageId) => void; isAdmin?: boolean; authEnabled?: boolean; user?: AuthUser | null; appName?: string; logoUrl?: string | null; onOpenDrawer: () => void; hamburgerButtonRef?: React.RefObject; navDrawerOpen?: boolean; } export const NAV_ITEMS: Array<{ id: PageId; label: string; adminOnly: boolean; requiresAuth: boolean }> = [ { id: 'tasks', label: 'タスク', adminOnly: false, requiresAuth: false }, { id: 'schedules', label: 'スケジュール', adminOnly: false, requiresAuth: false }, { id: 'pieces', label: 'Pieces', adminOnly: true, requiresAuth: false }, { id: 'captcha', label: 'CAPTCHA', adminOnly: true, requiresAuth: false }, { id: 'settings', label: '設定', adminOnly: false, requiresAuth: false }, { id: 'users', label: 'ユーザー', adminOnly: true, requiresAuth: true }, { id: 'help', label: 'ヘルプ', adminOnly: false, requiresAuth: false }, { id: 'userfolder', label: 'ユーザーフォルダ', adminOnly: false, requiresAuth: false }, ]; export function estimateCollapseThreshold(navCount: number): number { return 430 + navCount * 78 + 60; } export function useViewportNarrow(threshold: number): boolean { const [narrow, setNarrow] = useState(() => typeof window !== 'undefined' ? window.innerWidth < threshold : false, ); useEffect(() => { if (typeof window === 'undefined') return; const update = () => setNarrow(window.innerWidth < threshold); update(); window.addEventListener('resize', update); return () => window.removeEventListener('resize', update); }, [threshold]); return narrow; } export function visibleNavItemsFor(isAdmin: boolean, authEnabled: boolean) { return NAV_ITEMS.filter(item => { if (item.adminOnly && !isAdmin) return false; if (item.requiresAuth && !authEnabled) return false; return true; }); } export function useCompactNav(isAdmin: boolean, authEnabled: boolean): boolean { const visible = visibleNavItemsFor(isAdmin, authEnabled); return useViewportNarrow(estimateCollapseThreshold(visible.length)); } export function TopBar({ currentPage, onNavigate, isAdmin = true, authEnabled = false, user = null, appName = 'MAESTRO', logoUrl = null, onOpenDrawer, hamburgerButtonRef, navDrawerOpen = false, }: TopBarProps) { const visibleNav = visibleNavItemsFor(isAdmin, authEnabled); const compactMode = useViewportNarrow(estimateCollapseThreshold(visibleNav.length)); return (
{compactMode && ( )} {appName} v{__APP_VERSION__} {!compactMode && ( )}
{user && (
{user.avatarUrl ? ( {user.name ) : (
{(user.name ?? user.email).charAt(0)}
)} {user.name ?? user.email}
ログアウト
)}
); }