Files
supabase/apps/www/lib/eventsUtils.ts
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

74 lines
2.1 KiB
TypeScript

import { EventHost } from './eventsTypes'
/**
* Formats hosts array for display
* If there are more than 2 hosts, shows "FirstHost and X more"
*
* @param hosts - Array of event hosts
* @returns Object with displayText and fullList (for title attribute)
*
* @example
* formatHosts([host1]) => { displayText: "Supabase", fullList: "Supabase" }
* formatHosts([host1, host2]) => { displayText: "Supabase and GitHub", fullList: "Supabase, GitHub" }
* formatHosts([host1, host2, host3]) => { displayText: "Supabase and 2 more", fullList: "Supabase, GitHub, Vercel" }
*/
export function formatHosts(hosts: EventHost[]): {
displayText: string
fullList: string
} {
if (!hosts || hosts.length === 0) {
return {
displayText: '',
fullList: '',
}
}
// Get host names, preferring 'name' over constructed name from first/last
const hostNames = hosts.map((host) => {
if (host.name) return host.name
if (host.first_name && host.last_name) {
return `${host.first_name} ${host.last_name}`
}
if (host.first_name) return host.first_name
return 'Unknown Host'
})
// Full list for title attribute (comma-separated)
const fullList = hostNames.join(', ')
// Display text logic
let displayText: string
if (hostNames.length === 1) {
displayText = hostNames[0]
} else if (hostNames.length === 2) {
displayText = `${hostNames[0]} and ${hostNames[1]}`
} else {
const remaining = hostNames.length - 1
displayText = `${hostNames[0]} and ${remaining} more`
}
return {
displayText,
fullList,
}
}
function isValidTimeZone(timeZone: string) {
if (!timeZone) return false
try {
new Intl.DateTimeFormat('en-US', { timeZone })
return true
} catch {
return false
}
}
// Server and first client render use the event's own zone so the HTML matches and shows
// attendees' local dates; after hydration the visitor's zone applies.
export function getEventTimeZone(date: string, eventTimeZone: string, isHydrated: boolean) {
if (date.endsWith('T12:00:00Z')) return 'UTC'
if (isHydrated) return undefined
return isValidTimeZone(eventTimeZone) ? eventTimeZone : 'UTC'
}