'use client'; import React, { useState, useRef, useEffect, useId } from 'react'; import Link from 'next/link'; import { useSearchParams } from 'next/navigation'; import { Club, UserPortalPermissions, ClubRole } from './types'; interface FounderTopBarProps { club: Club; permissions: UserPortalPermissions; activeNav?: string; onToggleMobileSidebar?: () => void; onPlanRide?: () => void; onPostAnnouncement?: () => void; onSwitchRole?: (role: ClubRole) => void; onSwitchClub?: () => void; pendingApplicationsCount?: number; upcomingRidesCount?: number; } export function FounderTopBar({ club, permissions, activeNav, onToggleMobileSidebar, onPlanRide, onPostAnnouncement, onSwitchRole, onSwitchClub, pendingApplicationsCount, upcomingRidesCount, }: FounderTopBarProps) { const [userMenuOpen, setUserMenuOpen] = useState(false); const userMenuRef = useRef(null); const [notificationsOpen, setNotificationsOpen] = useState(false); const notificationsRef = useRef(null); const notificationsButtonRef = useRef(null); const notificationsId = useId(); const searchParams = useSearchParams(); const previewQuery = searchParams?.get('preview') === 'demo' ? '?preview=demo' : ''; const managePath = `/clubs/${club.slug || club.id}/manage`; const pendingCount = permissions.canApproveApplications ? (pendingApplicationsCount ?? 0) : 0; useEffect(() => { setNotificationsOpen(false); }, [club.id, activeNav, permissions.role]); useEffect(() => { if (!notificationsOpen) return; function handleClickOutside(event: MouseEvent) { if (!notificationsRef.current?.contains(event.target as Node)) setNotificationsOpen(false); } function handleKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') { setNotificationsOpen(false); notificationsButtonRef.current?.focus(); } } function handleFocusOutside(event: FocusEvent) { if (!notificationsRef.current?.contains(event.target as Node)) setNotificationsOpen(false); } document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleKeyDown); document.addEventListener('focusin', handleFocusOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleKeyDown); document.removeEventListener('focusin', handleFocusOutside); }; }, [notificationsOpen]); useEffect(() => { function handleClickOutside(event: MouseEvent) { if (userMenuRef.current && !userMenuRef.current.contains(event.target as Node)) { setUserMenuOpen(false); } } function handleKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') { setUserMenuOpen(false); } } if (userMenuOpen) { document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleKeyDown); } return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleKeyDown); }; }, [userMenuOpen]); return (
{/* Left: Mobile hamburger + Breadcrumbs + Switch Club */}
{/* Mobile Hamburger Toggle */}
Clubs / {club.name} {activeNav === 'profile' && ( <> / Club Profile )} {(activeNav === 'members' || activeNav === 'wall') && ( <> / {activeNav === 'members' ? 'Members' : 'Wall'} )} {activeNav === 'applications' && ( <> / Applications )} {activeNav === 'ranks_roles' && ( <> / Ranks & Roles )} {activeNav === 'rides' && ( <> / Rides )} {activeNav === 'stats' && ( <> / Stats )} {activeNav === 'moderation' && ( <> / Moderation )} {activeNav === 'settings' && ( <> / Settings )}
{/* Middle: Search Input (desktop/tablet only, hidden on lg to prevent crush) */}
search
{/* Right: Actions + Role Switcher preview pill + Notifications + Profile */}
{/* Authoritative Role Badge (Production) vs Interactive Role Switcher (Development Only) */} {process.env.NODE_ENV === 'development' ? (
Dev Preview: {(['owner', 'admin', 'member'] as const).map((r) => ( ))}
) : (
{permissions.role}
)} {/* Post Announcement (Owner / Admin only) */} {permissions.canManageSettings && ( )} {/* Plan Club Ride (Owner / Admin only) */} {permissions.canPlanRides && ( )} {/* Notifications */}
{pendingCount > 0 && } {notificationsOpen && (

Club updates

{permissions.canManageSettings ? 'Open a section to review current club activity.' : 'Club management updates are available to Owners and Admins.'}

{permissions.canApproveApplications && setNotificationsOpen(false)} className="block rounded-xl border border-brand-border p-3 hover:border-brand-cyan focus-visible:outline focus-visible:outline-brand-cyan"> {pendingCount > 0 ? `${pendingCount} pending ${pendingCount === 1 ? 'application' : 'applications'}` : 'Membership applications'} Review membership requests. } {permissions.canPlanRides && setNotificationsOpen(false)} className="block rounded-xl border border-brand-border p-3 hover:border-brand-cyan focus-visible:outline focus-visible:outline-brand-cyan"> {upcomingRidesCount && upcomingRidesCount > 0 ? `${upcomingRidesCount} upcoming ${upcomingRidesCount === 1 ? 'ride' : 'rides'}` : 'Upcoming rides'} See scheduled club rides. } {permissions.canManageSettings && setNotificationsOpen(false)} className="block rounded-xl border border-brand-border p-3 hover:border-brand-cyan focus-visible:outline focus-visible:outline-brand-cyan"> Club Wall Read announcements and discussions. }
)}
{/* Rider Profile Menu Dropdown */}
{userMenuOpen && (
{club.founderName || 'Rider Account'} {permissions.role === 'owner' ? 'Club Owner' : permissions.role === 'admin' ? 'Club Admin' : 'Club Member'}
setUserMenuOpen(false)} role="menuitem" className="flex items-center gap-3 px-3 py-2.5 rounded-xl text-slate-200 hover:text-brand-cyan hover:bg-surface-container transition-colors text-xs font-semibold" > person Rider Account setUserMenuOpen(false)} role="menuitem" className="flex items-center gap-3 px-3 py-2.5 rounded-xl text-slate-200 hover:text-brand-cyan hover:bg-surface-container transition-colors text-xs font-semibold" > groups Group Rides setUserMenuOpen(false)} role="menuitem" className="flex items-center gap-3 px-3 py-2.5 rounded-xl text-slate-200 hover:text-brand-cyan hover:bg-surface-container transition-colors text-xs font-semibold" > shield Founder Portal {(permissions.role === 'owner' || permissions.role === 'admin') && ( setUserMenuOpen(false)} role="menuitem" className="flex items-center gap-3 px-3 py-2.5 rounded-xl text-slate-200 hover:text-brand-cyan hover:bg-surface-container transition-colors text-xs font-semibold" > admin_panel_settings Operations Admin )}
)}
); }