Files
supabase/apps/www/components/Events/new/EventBanner.tsx
Pamela Chia d2d0a9ec8e fix(www): server-render the /events list (#52637)
`/events` showed skeletons until a client-side request to the Luma
events API finished, so the raw HTML had no upcoming events and every
category count read zero. I moved the Luma fetch to the server next to
the Notion and MDX loaders, so the HTML now includes the event list, and
I set the page to revalidate every 5 minutes.

**Changed:**
- **Event list in the HTML**: I built `getLumaEvents` on the upstream
fetch from `/api-v2/luma-events`, which I made shareable and gave a 10 s
timeout, with the same public-only filter and mapping the client used. I
removed the client fetch, the loading state and both skeletons.
- **Luma failures**: on a missing API key, an upstream error or a
timeout, `getLumaEvents` returns no Luma events and reports the error to
Sentry, the same soft fallback the Notion loader uses, so neither the
build nor a regeneration can fail on Luma.
- **Dates without hydration mismatches**: I format each event's date and
time in the event's own time zone on the server and the first client
render (banner times carry the zone name, and the banner always shows
the year). After hydration the page switches to the visitor's time zone,
as before.

**Note:** before JavaScript loads, visitors now see event-local times
labeled with their zone where they used to see skeletons. If Luma is
down during a regeneration, the page shows no Luma events until the next
successful regeneration.

## To test

Tested on the PR preview:
- [x] Fetch `<preview-url>/api-v2/luma-events?after=<now>`, then fetch
`<preview-url>/events` with a Chrome user agent, strip `<script>`,
`<style>` and `<template>` elements and React's `<!-- -->` separators:
expect every returned event name in the HTML, `All (N)` equal to the
number of event cards (`h3.leading-snug`), and no `animate-pulse`
skeletons (5/5 names, `All (5)` with 5 cards, 0 skeletons; a 6 PM Denver
event groups under its own local date)
- [x] Open `/events` in a browser outside UTC: expect no hydration
errors in the console, banner times in local time without a zone label,
and no request to `/api-v2/luma-events` (Asia/Singapore: none of either)
- [x] Click a category filter, then All; search for a title word, then
clear it; toggle On-demand: expect the list to narrow and restore as
before, and the banner to hide on On-demand

## Linear
- part of GROWTH-1416
2026-11-02 21:48:56 +08:00

185 lines
6.0 KiB
TypeScript

'use client'
import { useEvents } from '~/app/events/context'
import { formatHosts, getEventTimeZone } from '~/lib/eventsUtils'
import { ArrowRightIcon, CalendarIcon, MapPinIcon, VideoIcon } from 'lucide-react'
import Link from 'next/link'
import { Badge, Button } from 'ui'
export function EventBanner() {
const { featuredEvent, isHydrated } = useEvents()
if (!featuredEvent) return null
return (
<section className="flex flex-col gap-6 rounded-lg py-6">
<article className="flex flex-col md:flex-row md:items-stretch gap-8 lg:py-2">
<div className="flex flex-col gap-6 flex-1">
<div className="flex flex-col">
<div className="flex items-center gap-4">
<h2 className="text-2xl font-medium lg:line-clamp-2">{featuredEvent.title}</h2>
{featuredEvent.isSpeaking && (
<Badge variant="success" className="flex items-center gap-1 shrink-0">
Speaking
</Badge>
)}
</div>
{featuredEvent.hosts.length > 0 && (
<p
className="text-lg font-medium text-foreground-light"
title={`Hosted by ${formatHosts(featuredEvent.hosts).fullList}`}
>
{formatHosts(featuredEvent.hosts).displayText}
</p>
)}
</div>
<p className="text-foreground-light line-clamp-3 lg:line-clamp-4">
{featuredEvent.description}
</p>
</div>
<div className="flex flex-col justify-between gap-8">
<div className="flex flex-row gap-6">
<DateWidget
date={featuredEvent.date}
endDate={featuredEvent.end_date}
eventTimeZone={featuredEvent.timezone}
isHydrated={isHydrated}
/>
<LocationWidget
location={featuredEvent.location}
categories={featuredEvent.categories}
/>
</div>
<div className="flex items-center md:justify-end gap-2">
{featuredEvent.meetingLink && (
<Button variant="secondary" size="medium" asChild>
<Link href={featuredEvent.meetingLink} target="_blank" rel="noopener noreferrer">
Meet with us
</Link>
</Button>
)}
{featuredEvent.link && (
<Button
variant="primary"
size="medium"
asChild
iconRight={<ArrowRightIcon size={14} />}
>
<Link
href={featuredEvent.link.href}
target={featuredEvent.link.target}
rel="noopener noreferrer"
>
Register
</Link>
</Button>
)}
</div>
</div>
</article>
</section>
)
}
const DateWidget = ({
date,
endDate,
eventTimeZone,
isHydrated,
}: {
date: string
endDate?: string
eventTimeZone: string
isHydrated: boolean
}) => {
const eventDate = new Date(date)
// Date-only Notion events are normalized to noon UTC (T12:00:00Z); detect them
// so we can display in UTC and suppress the fabricated time component.
const isDateOnly = date.endsWith('T12:00:00Z')
const timeZone = getEventTimeZone(date, eventTimeZone, isHydrated)
const formattedDate = eventDate.toLocaleDateString('en-US', {
weekday: 'long',
month: 'short',
day: 'numeric',
timeZone,
})
const currentYear = new Date().toLocaleDateString('en-US', { year: 'numeric' })
const eventYear = eventDate.toLocaleDateString('en-US', { year: 'numeric', timeZone })
// Always show the year until hydration: the server's clock can't decide the visitor's current year.
const formattedDateWithYear =
!isHydrated || eventYear !== currentYear ? `${formattedDate}, ${eventYear}` : formattedDate
const timeFormat = {
hour: 'numeric',
minute: '2-digit',
hour12: true,
timeZone,
timeZoneName: isHydrated ? undefined : 'short',
} as const
// Only show time for events that have a real time component
let timeDisplay: string | null = null
if (!isDateOnly) {
const startTime = eventDate.toLocaleTimeString('en-US', timeFormat)
if (endDate && !endDate.endsWith('T12:00:00Z')) {
const endEventDate = new Date(endDate)
const endTime = endEventDate.toLocaleTimeString('en-US', timeFormat)
timeDisplay = `${startTime} - ${endTime}`
} else {
timeDisplay = startTime
}
}
return (
<div className="flex items-center gap-4">
<div className="bg-surface-100 w-10 h-10 flex items-center justify-center border rounded-md">
<CalendarIcon className="size-5" strokeWidth={1.5} />
</div>
<div className="flex flex-col gap-0">
<p>{formattedDateWithYear}</p>
{timeDisplay && <p className="text-foreground-light text-sm">{timeDisplay}</p>}
</div>
</div>
)
}
const LocationWidget = ({ location, categories }: { location?: string; categories?: string[] }) => {
// Webinars have no physical location; show a camera icon and the format instead.
if (categories?.includes('webinar')) {
return (
<div className="flex items-center gap-4">
<div className="bg-surface-100 w-10 h-10 flex items-center justify-center border rounded-md">
<VideoIcon className="size-5" strokeWidth={1.5} />
</div>
<div className="flex flex-col gap-0">
<p>Supabase Live</p>
</div>
</div>
)
}
const hasLocation = location && location.length > 0
const locationText = hasLocation ? location : 'Unknown'
return (
<div className="flex items-center gap-4">
<div className="bg-surface-100 w-10 h-10 flex items-center justify-center border rounded-md">
<MapPinIcon className="size-5" strokeWidth={1.5} />
</div>
<div className="flex flex-col gap-0">
<p>Hosted at</p>
<p className="text-foreground-light text-sm">{locationText}</p>
</div>
</div>
)
}