22
33import { createContext , useContext , useState , useEffect , useMemo , ReactNode } from 'react'
44import { SupabaseEvent , SUPABASE_HOST } from '~/lib/eventsTypes'
5- import { cover } from 'three/src/extras/TextureUtils.js'
65
76interface EventsContextValue {
87 // Events data
98 allEvents : SupabaseEvent [ ]
109 filteredEvents : SupabaseEvent [ ]
10+ filteredOnDemandEvents : SupabaseEvent [ ]
1111 staticEvents : SupabaseEvent [ ]
12+ onDemandEvents : SupabaseEvent [ ]
1213 lumaEvents : SupabaseEvent [ ]
1314 featuredEvent : SupabaseEvent | undefined
1415
@@ -30,9 +31,10 @@ const EventsContext = createContext<EventsContextValue | undefined>(undefined)
3031interface EventsProviderProps {
3132 children : ReactNode
3233 staticEvents : SupabaseEvent [ ]
34+ onDemandEvents : SupabaseEvent [ ]
3335}
3436
35- export function EventsProvider ( { children, staticEvents } : EventsProviderProps ) {
37+ export function EventsProvider ( { children, staticEvents, onDemandEvents } : EventsProviderProps ) {
3638 const [ lumaEvents , setLumaEvents ] = useState < SupabaseEvent [ ] > ( [ ] )
3739 const [ isLoading , setIsLoading ] = useState ( true )
3840 const [ searchQuery , setSearchQuery ] = useState ( '' )
@@ -99,9 +101,12 @@ export function EventsProvider({ children, staticEvents }: EventsProviderProps)
99101 } , [ staticEvents , lumaEvents ] )
100102
101103 // Calculate categories with counts
104+ // - Webinar: count only upcoming webinars (not on-demand)
105+ // - On-demand: count only on-demand events
102106 const categories = useMemo ( ( ) => {
103107 const categoryCounts : { [ key : string ] : number } = { all : 0 }
104108
109+ // Count upcoming events (excluding on-demand)
105110 allEvents . forEach ( ( event ) => {
106111 categoryCounts . all += 1
107112
@@ -110,8 +115,15 @@ export function EventsProvider({ children, staticEvents }: EventsProviderProps)
110115 } )
111116 } )
112117
118+ // Count on-demand events separately
119+ onDemandEvents . forEach ( ( event ) => {
120+ categoryCounts . all += 1
121+ // Add to 'on-demand' category instead of 'webinar'
122+ categoryCounts [ 'on-demand' ] = ( categoryCounts [ 'on-demand' ] || 0 ) + 1
123+ } )
124+
113125 return categoryCounts
114- } , [ allEvents ] )
126+ } , [ allEvents , onDemandEvents ] )
115127
116128 // Toggle category selection
117129 const toggleCategory = ( category : string ) => {
@@ -135,8 +147,13 @@ export function EventsProvider({ children, staticEvents }: EventsProviderProps)
135147 } )
136148 }
137149
138- // Filter events by search query and category
150+ // Filter upcoming events by search query and category
139151 const filteredEvents = useMemo ( ( ) => {
152+ // If 'on-demand' is selected, don't show upcoming events
153+ if ( selectedCategories . includes ( 'on-demand' ) && ! selectedCategories . includes ( 'all' ) ) {
154+ return [ ]
155+ }
156+
140157 let filtered = allEvents
141158
142159 // Filter by categories (multiple selection)
@@ -167,6 +184,31 @@ export function EventsProvider({ children, staticEvents }: EventsProviderProps)
167184 } )
168185 } , [ allEvents , selectedCategories , searchQuery ] )
169186
187+ // Filter on-demand events separately by search query and category
188+ const filteredOnDemandEvents = useMemo ( ( ) => {
189+ // If specific categories are selected (not 'all' and not 'on-demand'), don't show on-demand events
190+ if ( ! selectedCategories . includes ( 'all' ) && ! selectedCategories . includes ( 'on-demand' ) ) {
191+ return [ ]
192+ }
193+
194+ let filtered = onDemandEvents
195+
196+ // Filter by search query
197+ if ( searchQuery . trim ( ) ) {
198+ const query = searchQuery . toLowerCase ( )
199+ filtered = filtered . filter ( ( event ) => {
200+ const titleMatch = event . title ?. toLowerCase ( ) . includes ( query )
201+ const descriptionMatch = event . description ?. toLowerCase ( ) . includes ( query )
202+ const locationMatch = event . location ?. toLowerCase ( ) . includes ( query )
203+ const tagsMatch = event . tags ?. some ( ( tag ) => tag . toLowerCase ( ) . includes ( query ) )
204+
205+ return titleMatch || descriptionMatch || locationMatch || tagsMatch
206+ } )
207+ }
208+
209+ return filtered
210+ } , [ onDemandEvents , selectedCategories , searchQuery ] )
211+
170212 // Featured event: nearest upcoming event, or if none, the most recent past event
171213 const featuredEvent = useMemo ( ( ) => {
172214 if ( allEvents . length === 0 ) return undefined
@@ -208,7 +250,9 @@ export function EventsProvider({ children, staticEvents }: EventsProviderProps)
208250 const value : EventsContextValue = {
209251 allEvents,
210252 filteredEvents,
253+ filteredOnDemandEvents,
211254 staticEvents,
255+ onDemandEvents,
212256 lumaEvents,
213257 isLoading,
214258 searchQuery,
0 commit comments