mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +00:00
`/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
162 lines
4.6 KiB
TypeScript
162 lines
4.6 KiB
TypeScript
'use client'
|
|
|
|
import { SupabaseEvent } from '~/lib/eventsTypes'
|
|
import {
|
|
createContext,
|
|
ReactNode,
|
|
useContext,
|
|
useMemo,
|
|
useState,
|
|
useSyncExternalStore,
|
|
} from 'react'
|
|
|
|
interface EventsContextValue {
|
|
allEvents: SupabaseEvent[]
|
|
filteredEvents: SupabaseEvent[]
|
|
featuredEvent: SupabaseEvent | undefined
|
|
isHydrated: boolean
|
|
searchQuery: string
|
|
setSearchQuery: (query: string) => void
|
|
selectedCategories: string[]
|
|
toggleCategory: (category: string) => void
|
|
categories: { [key: string]: number }
|
|
}
|
|
|
|
const EventsContext = createContext<EventsContextValue | undefined>(undefined)
|
|
|
|
const subscribeToNothing = () => () => {}
|
|
|
|
interface EventsProviderProps {
|
|
children: ReactNode
|
|
notionEvents: SupabaseEvent[]
|
|
lumaEvents: SupabaseEvent[]
|
|
mdxEvents: SupabaseEvent[]
|
|
onDemandMdxEvents: SupabaseEvent[]
|
|
}
|
|
|
|
export function EventsProvider({
|
|
children,
|
|
notionEvents,
|
|
lumaEvents,
|
|
mdxEvents,
|
|
onDemandMdxEvents,
|
|
}: EventsProviderProps) {
|
|
const [searchQuery, setSearchQuery] = useState('')
|
|
const [selectedCategories, setSelectedCategories] = useState<string[]>(['all'])
|
|
const isHydrated = useSyncExternalStore(
|
|
subscribeToNothing,
|
|
() => true,
|
|
() => false
|
|
)
|
|
|
|
// Merge Notion + mdx + Luma events, showing only today and future.
|
|
const allEvents = useMemo(() => {
|
|
const now = new Date()
|
|
const startOfToday = new Date(
|
|
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())
|
|
)
|
|
return [...notionEvents, ...mdxEvents, ...lumaEvents].filter((event) => {
|
|
const eventDate = new Date(event.end_date || event.date)
|
|
return eventDate >= startOfToday
|
|
})
|
|
}, [notionEvents, mdxEvents, lumaEvents])
|
|
|
|
const isOnDemandView = selectedCategories.includes('on-demand')
|
|
|
|
const categories = useMemo(() => {
|
|
const counts: { [key: string]: number } = { all: 0 }
|
|
|
|
allEvents.forEach((event) => {
|
|
counts.all += 1
|
|
event.categories?.forEach((category) => {
|
|
counts[category] = (counts[category] || 0) + 1
|
|
})
|
|
})
|
|
|
|
if (onDemandMdxEvents.length > 0) {
|
|
counts['on-demand'] = onDemandMdxEvents.length
|
|
}
|
|
|
|
return counts
|
|
}, [allEvents, onDemandMdxEvents.length])
|
|
|
|
const toggleCategory = (category: string) => {
|
|
if (category === 'on-demand') {
|
|
setSelectedCategories((prev) => (prev.includes('on-demand') ? ['all'] : ['on-demand']))
|
|
return
|
|
}
|
|
|
|
if (category === 'all') {
|
|
setSelectedCategories(['all'])
|
|
return
|
|
}
|
|
|
|
setSelectedCategories((prev) => {
|
|
const withoutAll = prev.filter((c) => c !== 'all' && c !== 'on-demand')
|
|
|
|
if (withoutAll.includes(category)) {
|
|
const updated = withoutAll.filter((c) => c !== category)
|
|
return updated.length === 0 ? ['all'] : updated
|
|
} else {
|
|
return [...withoutAll, category]
|
|
}
|
|
})
|
|
}
|
|
|
|
const filteredEvents = useMemo(() => {
|
|
let filtered = isOnDemandView ? onDemandMdxEvents : allEvents
|
|
|
|
if (!isOnDemandView && !selectedCategories.includes('all')) {
|
|
filtered = filtered.filter((event) =>
|
|
event.categories?.some((cat) => selectedCategories.includes(cat))
|
|
)
|
|
}
|
|
|
|
if (searchQuery.trim()) {
|
|
const query = searchQuery.toLowerCase()
|
|
filtered = filtered.filter((event) => {
|
|
const titleMatch = event.title?.toLowerCase().includes(query)
|
|
const descriptionMatch = event.description?.toLowerCase().includes(query)
|
|
const locationMatch = event.location?.toLowerCase().includes(query)
|
|
const tagsMatch = event.tags?.some((tag) => tag.toLowerCase().includes(query))
|
|
|
|
return titleMatch || descriptionMatch || locationMatch || tagsMatch
|
|
})
|
|
}
|
|
|
|
return [...filtered].sort((a, b) => {
|
|
const dateA = new Date(a.date).getTime()
|
|
const dateB = new Date(b.date).getTime()
|
|
return isOnDemandView ? dateB - dateA : dateA - dateB
|
|
})
|
|
}, [allEvents, onDemandMdxEvents, isOnDemandView, selectedCategories, searchQuery])
|
|
|
|
const featuredEvent = useMemo(() => {
|
|
if (allEvents.length === 0) return undefined
|
|
|
|
return [...allEvents].sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime())[0]
|
|
}, [allEvents])
|
|
|
|
const value: EventsContextValue = {
|
|
allEvents,
|
|
filteredEvents,
|
|
featuredEvent,
|
|
isHydrated,
|
|
searchQuery,
|
|
setSearchQuery,
|
|
selectedCategories,
|
|
toggleCategory,
|
|
categories,
|
|
}
|
|
|
|
return <EventsContext.Provider value={value}>{children}</EventsContext.Provider>
|
|
}
|
|
|
|
export function useEvents() {
|
|
const context = useContext(EventsContext)
|
|
if (context === undefined) {
|
|
throw new Error('useEvents must be used within an EventsProvider')
|
|
}
|
|
return context
|
|
}
|