import { redirect } from 'next/navigation' import Link from 'next/link' import { auth } from '@/lib/auth' import { db } from '@/lib/db' import { rounds, roundPlayers, courses, tees } from '@/lib/db/schema' import { eq, inArray } from 'drizzle-orm' import { buttonVariants } from '@/components/ui/button' import { cn } from '@/lib/utils' import { headers } from 'next/headers' export default async function DashboardPage() { const session = await auth.api.getSession({ headers: await headers() }) if (!session) redirect('/login') const memberships = await db .select({ roundId: roundPlayers.roundId }) .from(roundPlayers) .where(eq(roundPlayers.userId, session.user.id)) const roundIds = memberships.map((m) => m.roundId) const activeRounds = roundIds.length > 0 ? await db .select({ id: rounds.id, name: rounds.name, date: rounds.date, status: rounds.status, holesCount: rounds.holesCount, courseName: courses.name, teeName: tees.name, teeColor: tees.color, }) .from(rounds) .innerJoin(courses, eq(rounds.courseId, courses.id)) .innerJoin(tees, eq(rounds.teeId, tees.id)) .where(inArray(rounds.id, roundIds)) .orderBy(rounds.date) : [] const visibleRounds = activeRounds.filter((r) => r.status !== 'completed') return (
No active rounds. Start one!
)}