|
| 1 | +import type { Metadata } from "next"; |
| 2 | +import { SeasonView } from "@/components/seasons/season-view"; |
| 3 | +import { PLAY_ROUTES } from "@/constants/play"; |
| 4 | +import { SEASON_ROUTE } from "@/constants/seasons"; |
| 5 | +import { challengeProgress, getNextSeason, getSeasonById } from "@/lib/seasons"; |
| 6 | +import { getSeasonCounts } from "@/lib/seasons/progress"; |
| 7 | +import { getServerSeason } from "@/lib/seasons/server"; |
| 8 | +import { breadcrumbJsonLd, buildMetadata } from "@/lib/seo"; |
| 9 | +import { fitDescription } from "@/lib/seo/describe"; |
| 10 | +import { JsonLd } from "@/lib/seo/json-ld-script"; |
| 11 | +import { getCachedAuthUser, getCachedBareProfile } from "@/lib/server-cache"; |
| 12 | + |
| 13 | +type Props = { searchParams: Promise<Record<string, string | string[] | undefined>> }; |
| 14 | + |
| 15 | +export const dynamic = "force-dynamic"; |
| 16 | + |
| 17 | +export async function generateMetadata(): Promise<Metadata> { |
| 18 | + const { season } = await getServerSeason(); |
| 19 | + return buildMetadata({ |
| 20 | + title: season ? `${season.name}: season challenges` : "Seasons and challenges", |
| 21 | + description: fitDescription( |
| 22 | + season |
| 23 | + ? `${season.tagline} ${season.challenges.length} challenges for students on CampusLoop.` |
| 24 | + : "Every few weeks CampusLoop gets a new look and a new set of challenges for students.", |
| 25 | + ["See what is on and how far you are"] |
| 26 | + ), |
| 27 | + path: SEASON_ROUTE, |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +export default async function SeasonPage({ searchParams }: Props) { |
| 32 | + const [params, live, user] = await Promise.all([searchParams, getServerSeason(), getCachedAuthUser()]); |
| 33 | + const profile = user ? await getCachedBareProfile(user.id) : null; |
| 34 | + const now = live.now; |
| 35 | + |
| 36 | + // Preview: ?season=<id> (or "off") works in development and for admins only. |
| 37 | + const isDev = process.env.NODE_ENV === "development"; |
| 38 | + const wanted = typeof params.season === "string" ? params.season : null; |
| 39 | + let season = live.season; |
| 40 | + if (wanted && (isDev || profile?.role === "ADMIN")) { |
| 41 | + season = wanted === "off" ? null : (getSeasonById(wanted) ?? live.season); |
| 42 | + } |
| 43 | + |
| 44 | + let progress = null; |
| 45 | + if (season && profile?.onboardingCompleted) { |
| 46 | + progress = challengeProgress(season, await getSeasonCounts(profile.id, season)); |
| 47 | + } |
| 48 | + // Development only: look at the finished state without touching data. |
| 49 | + if (isDev && season && params.progress === "complete") { |
| 50 | + progress = season.challenges.map((c) => ({ id: c.id, count: c.target, target: c.target, done: true, ratio: 1 })); |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <JsonLd |
| 56 | + data={breadcrumbJsonLd([ |
| 57 | + { name: "Play", path: PLAY_ROUTES.hub }, |
| 58 | + { name: season ? season.name : "Seasons", path: SEASON_ROUTE }, |
| 59 | + ])} |
| 60 | + /> |
| 61 | + <SeasonView |
| 62 | + season={season} |
| 63 | + next={getNextSeason(now, season?.id)} |
| 64 | + progress={progress} |
| 65 | + now={now} |
| 66 | + signInHref={`/sign-in?returnTo=${SEASON_ROUTE}`} |
| 67 | + /> |
| 68 | + </> |
| 69 | + ); |
| 70 | +} |
0 commit comments