'use client'; import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { onAuthStateChanged, User } from 'firebase/auth'; import { firebaseAuth } from '../../../lib/firebase-client'; import { fetchManageableClubs, ManageableClubSummary } from '../../../lib/club-data'; import { OverviewSkeleton } from '../../../components/founder-portal/skeleton-state'; import { SafeErrorState } from '../../../components/founder-portal/safe-error-state'; export default function ClubManageDiscoveryPage() { const router = useRouter(); const [currentUser, setCurrentUser] = useState(null); const [authChecked, setAuthChecked] = useState(false); const [loading, setLoading] = useState(true); const [clubs, setClubs] = useState([]); const [error, setError] = useState(null); useEffect(() => { const unsubscribe = onAuthStateChanged(firebaseAuth, (user) => { setCurrentUser(user); setAuthChecked(true); }); return () => unsubscribe(); }, []); async function loadClubs(uid: string) { setLoading(true); setError(null); try { const results = await fetchManageableClubs(uid); setClubs(results); if (results.length === 1) { // Automatically redirect to the single manageable club const target = results[0].slug || results[0].id; router.replace(`/clubs/${target}/manage`); } } catch { setError("Unable to discover manageable clubs. Please check your network connection."); } finally { setLoading(false); } } useEffect(() => { if (!authChecked) return; if (currentUser) { void loadClubs(currentUser.uid); } else { setLoading(false); } }, [authChecked, currentUser]); // Loading state (auth checking or discovering clubs) if (!authChecked || loading) { return (
); } // Unauthenticated state: prompt sign-in if (!currentUser) { return (
lock
Rider Authentication Required

Sign In to Access Portal

Founder Portal requires an active MotorSignal rider account with Club Owner or Admin administration permissions.

login Sign In with Rider Account Create New Account Browse Public Motorcycle Clubs →
); } // Error state if (error) { return (
loadClubs(currentUser.uid)} />
); } // Single club redirecting transition if (clubs.length === 1) { return (

Redirecting to {clubs[0].name}...

); } // 0 clubs: No clubs to manage if (clubs.length === 0) { return (
hub
Management Roster // 0 Clubs

No Clubs to Manage

Your rider account doesn't currently have Owner or Admin permissions for any registered clubs. To manage a club, create a new club or request admin privileges from your club owner.

add_circle Register Your Club explore Browse Public Clubs
); } // >1 clubs: Clean Club Selector return (
{/* Header */}
Founder Portal Cockpit

Select Club to Manage

Choose an active motorcycle club from your authorized administrative rosters.

add Register New Club
{/* Club Cards Grid */}
{clubs.map((c) => (
sports_motorsports

{c.name}

{c.role === 'owner' ? 'Owner' : 'Admin'}
{c.slug && /{c.slug}} • {c.memberCount} {c.memberCount === 1 ? 'member' : 'members'} {c.homeRegion && ( <> • {c.homeRegion} )}
Manage Club arrow_forward
))}
{/* Footer info */}
shield Only clubs where you are an active Owner or Admin appear here.
All Public Clubs →
); }