@@ -18,7 +18,33 @@ import { Skeleton } from "@/components/ui/skeleton";
18
18
function PapersCarousel ( ) {
19
19
const [ displayPapers , setDisplayPapers ] = useState < IUpcomingPaper [ ] > ( [ ] ) ;
20
20
const [ isLoading , setIsLoading ] = useState ( true ) ;
21
- const chunkSize = 8 ;
21
+ const [ chunkSize , setChunkSize ] = useState < number > ( 4 ) ; // dynamic chunk size
22
+
23
+ useEffect ( ( ) => {
24
+ const handleResize = ( ) => {
25
+ if ( window . innerWidth < 640 ) {
26
+ setChunkSize ( 4 ) ;
27
+ } else {
28
+ setChunkSize ( 8 ) ;
29
+ }
30
+ } ;
31
+
32
+ // preload sample subjects
33
+ localStorage . setItem (
34
+ "userSubjects" ,
35
+ JSON . stringify ( [
36
+ "Information Security [CBS3002]" ,
37
+ "Foundations of Data Analytics [BCSE351E]" ,
38
+ "Design and Analysis of Algorithms [MCSE502L]" ,
39
+ "Complex Variables and Linear Algebra [BMAT201L]" ,
40
+ "Differential Equations and Transforms [BMAT102L]" ,
41
+ ] )
42
+ ) ;
43
+
44
+ handleResize ( ) ; // initialize
45
+ window . addEventListener ( "resize" , handleResize ) ;
46
+ return ( ) => window . removeEventListener ( "resize" , handleResize ) ;
47
+ } , [ ] ) ;
22
48
23
49
useEffect ( ( ) => {
24
50
async function fetchPapers ( ) {
@@ -56,46 +82,53 @@ function PapersCarousel() {
56
82
</ div >
57
83
58
84
< CarouselContent >
59
- { isLoading
60
- ? Array . from ( { length : 1 } ) . map ( ( _ , index ) => (
61
- < CarouselItem
62
- key = { `skeleton-carousel-item-${ index } ` }
63
- className = "grid grid-cols-2 grid-rows-2 gap-4 md:grid-cols-4 lg:auto-rows-fr"
64
- >
65
- { Array . from ( { length : chunkSize } ) . map ( ( _ , idx ) => (
66
- < div
67
- key = { idx }
68
- className = "h-full rounded-sm border-2 border-[#734DFF] bg-[#FFFFFF] shadow-lg dark:border-[#36266D] dark:bg-[#171720]"
69
- >
70
- < div className = "border-b-2 border-[#453D60] p-2" >
71
- < Skeleton className = "h-8 w-24 rounded-md" />
72
- </ div >
73
- < div className = "flex flex-col justify-between p-4" >
74
- < Skeleton className = "mb-4 h-8 w-40 rounded-md" />
75
- < div className = "flex gap-2" >
76
- < Skeleton className = "h-7 w-16 rounded-full" />
77
- < Skeleton className = "h-7 w-16 rounded-full" />
78
- </ div >
79
- </ div >
80
- </ div >
81
- ) ) }
82
- </ CarouselItem >
83
- ) )
84
- : chunkedPapers . map ( ( paperGroup , index ) => (
85
- < CarouselItem
86
- key = { `carousel-item-${ index } ` }
87
- className = "grid grid-cols-2 grid-rows-2 gap-4 md:grid-cols-4 lg:auto-rows-fr"
85
+ { isLoading ? (
86
+ < CarouselItem
87
+ className = { `grid ${
88
+ chunkSize === 4
89
+ ? "grid-cols-2 grid-rows-2"
90
+ : "grid-cols-4"
91
+ } gap-4 lg:auto-rows-fr`}
92
+ >
93
+ { Array . from ( { length : chunkSize } ) . map ( ( _ , idx ) => (
94
+ < div
95
+ key = { idx }
96
+ className = "cursor-pointer rounded-sm border-2 border-[#734DFF] bg-[#FFFFFF] text-black shadow-lg transition duration-150 ease-in-out hover:bg-[#EFEAFF] dark:border-[#36266D] dark:bg-[#171720] dark:text-white hover:dark:bg-[#262635]"
88
97
>
89
- { paperGroup . map ( ( paper , subIndex ) => (
90
- < div key = { subIndex } className = "h-full" >
91
- < UpcomingPaper
92
- subject = { paper . subject }
93
- slots = { paper . slots }
94
- />
98
+ < div className = "border-b-2 border-[#453D60] p-2" >
99
+ < Skeleton className = "h-6 w-24 rounded-md" />
100
+ </ div >
101
+ < div className = "flex flex-col justify-between p-4" >
102
+ < Skeleton className = "mb-4 h-6 w-32 rounded-md" />
103
+ < div className = "flex gap-2" >
104
+ < Skeleton className = "h-7 w-16 rounded-full" />
105
+ < Skeleton className = "h-7 w-16 rounded-full" />
95
106
</ div >
96
- ) ) }
97
- </ CarouselItem >
107
+ </ div >
108
+ </ div >
98
109
) ) }
110
+ </ CarouselItem >
111
+ ) : (
112
+ chunkedPapers . map ( ( paperGroup , index ) => (
113
+ < CarouselItem
114
+ key = { `carousel-item-${ index } ` }
115
+ className = { `grid ${
116
+ chunkSize === 4
117
+ ? "grid-cols-2 grid-rows-2"
118
+ : "grid-cols-4"
119
+ } gap-4 lg:auto-rows-fr`}
120
+ >
121
+ { paperGroup . map ( ( paper , subIndex ) => (
122
+ < div key = { subIndex } className = "h-full" >
123
+ < UpcomingPaper
124
+ subject = { paper . subject }
125
+ slots = { paper . slots }
126
+ />
127
+ </ div >
128
+ ) ) }
129
+ </ CarouselItem >
130
+ ) )
131
+ ) }
99
132
</ CarouselContent >
100
133
</ Carousel >
101
134
</ div >
0 commit comments