@@ -6,39 +6,14 @@ import { useEffect, useRef, useState } from "react";
66import { UiEvent as EventType } from "@/hooks/useEvents" ;
77
88import { Button } from "./button" ;
9+ import { EventDateDisplay } from "./EventDateDisplay" ;
910
1011type EventCarouselProps = {
1112 items : EventType [ ] ;
1213} ;
1314
1415const GAP = 40 ;
1516
16- function formatEventDateDisplay ( dateString : string ) : string {
17- try {
18- const date = new Date ( dateString ) ;
19- const weekday = new Intl . DateTimeFormat ( "en-US" , {
20- weekday : "long" ,
21- } ) . format ( date ) ;
22- const day = new Intl . DateTimeFormat ( "en-US" , { day : "numeric" } ) . format (
23- date ,
24- ) ;
25- const month = new Intl . DateTimeFormat ( "en-US" , { month : "short" } ) . format (
26- date ,
27- ) ;
28- const time = new Intl . DateTimeFormat ( "en-US" , {
29- hour : "2-digit" ,
30- minute : "2-digit" ,
31- hour12 : true ,
32- } )
33- . format ( date )
34- . replace ( "AM" , "am" )
35- . replace ( "PM" , "pm" ) ;
36- return `${ weekday } ${ day } ${ month } ${ time } ` ;
37- } catch {
38- return "" ;
39- }
40- }
41-
4217export default function EventCarousel ( { items } : EventCarouselProps ) {
4318 const viewportRef = useRef < HTMLDivElement > ( null ) ;
4419 const firstItemRef = useRef < HTMLAnchorElement > ( null ) ;
@@ -76,16 +51,15 @@ export default function EventCarousel({ items }: EventCarouselProps) {
7651
7752 useEffect ( ( ) => {
7853 const updateVisibleCount = ( ) => {
79- if ( window . innerWidth < 768 ) {
80- setVisibleCount ( 1 ) ;
81- } else {
82- setVisibleCount ( 3 ) ;
83- }
54+ const newVisibleCount = window . innerWidth < 768 ? 1 : 3 ;
55+ const newMaxIndex = Math . max ( 0 , items . length - newVisibleCount ) ;
56+ setVisibleCount ( newVisibleCount ) ;
57+ setCurrentIndex ( ( prev ) => Math . min ( prev , newMaxIndex ) ) ;
8458 } ;
8559 updateVisibleCount ( ) ;
8660 window . addEventListener ( "resize" , updateVisibleCount ) ;
8761 return ( ) => window . removeEventListener ( "resize" , updateVisibleCount ) ;
88- } , [ ] ) ;
62+ } , [ items . length ] ) ;
8963
9064 return (
9165 < div className = "container mx-auto rounded-lg bg-primary-foreground px-4 py-8 lg:px-12" >
@@ -124,7 +98,7 @@ export default function EventCarousel({ items }: EventCarouselProps) {
12498 ) }
12599
126100 < div className = "mt-10 px-10" >
127- < div ref = { viewportRef } className = "overflow-hidden" >
101+ < div ref = { viewportRef } className = "overflow-hidden px-2 md:px-4 " >
128102 < div
129103 className = "flex transition-transform duration-300 ease-out"
130104 style = { {
@@ -137,7 +111,7 @@ export default function EventCarousel({ items }: EventCarouselProps) {
137111 href = { `/events/${ event . id } ` }
138112 key = { event . id }
139113 ref = { index === 0 ? firstItemRef : undefined }
140- className = { `block w-full flex-shrink-0 rounded-xl transition-transform duration-200 ease-in-out hover:scale-110 md:w-[calc((100%-80px)/3)] ${ index === 0 ? "origin-left" : "" } ` }
114+ className = { `block w-full flex-shrink-0 rounded-xl transition-transform duration-200 ease-in-out hover:scale-110 md:w-[calc((100%-80px)/3)] ${ index === currentIndex ? "origin-left" : "" } ` }
141115 >
142116 < div className = "relative aspect-[16/9] w-full overflow-hidden rounded-lg" >
143117 < Image
@@ -152,8 +126,8 @@ export default function EventCarousel({ items }: EventCarouselProps) {
152126 { event . name }
153127 </ h3 >
154128
155- < p className = "mb-4 text-sm text-primary" >
156- { formatEventDateDisplay ( event . date ) }
129+ < p className = "mb-4 text-base text-primary" >
130+ < EventDateDisplay date = { event . date } />
157131 </ p >
158132
159133 < div className = "mt-3 w-full border-b border-white/20" />
0 commit comments