1
- import { useState , useEffect } from "react" ;
1
+ import { useEffect , useRef , useState } from "react" ;
2
2
import {
3
3
Button ,
4
4
CircularProgress ,
5
5
Divider ,
6
+ ImageList ,
7
+ ImageListItem ,
6
8
Stack ,
7
9
Typography ,
8
10
} from "@mui/material" ;
@@ -16,11 +18,10 @@ const Post = ({ post, username }) => {
16
18
const { share } = post ;
17
19
18
20
return (
19
- < Grid >
21
+ < ImageListItem key = { share } >
20
22
< Stack
21
23
sx = { {
22
- maxWidth : "300px" ,
23
- minWidth : "200px" ,
24
+ display : "inline-block" ,
24
25
borderRadius : 2 ,
25
26
border : "solid 1px #eee" ,
26
27
padding : 2 ,
@@ -84,13 +85,31 @@ const Post = ({ post, username }) => {
84
85
</ Button >
85
86
</ Stack >
86
87
</ Stack >
87
- </ Grid >
88
+ </ ImageListItem >
88
89
) ;
89
90
} ;
90
91
91
92
const PinnedPosts = ( { username } ) => {
92
93
const [ posts , setPosts ] = useState ( [ ] ) ;
93
94
const [ loading , setLoading ] = useState ( true ) ;
95
+ const [ cols , setCols ] = useState ( 1 ) ;
96
+ const containerRef = useRef ( null ) ;
97
+
98
+ useEffect ( ( ) => {
99
+ const handleResize = ( ) => {
100
+ if ( containerRef . current ) {
101
+ const width = containerRef . current . clientWidth ;
102
+ const newCols = Math . floor ( width / 280 ) ;
103
+ setCols ( newCols ) ;
104
+ }
105
+ } ;
106
+
107
+ handleResize ( ) ;
108
+ window . addEventListener ( "resize" , handleResize ) ;
109
+ return ( ) => {
110
+ window . removeEventListener ( "resize" , handleResize ) ;
111
+ } ;
112
+ } , [ containerRef ] ) ;
94
113
95
114
useEffect ( ( ) => {
96
115
// TODO: handle pagination
@@ -109,17 +128,31 @@ const PinnedPosts = ({ username }) => {
109
128
} , [ username ] ) ;
110
129
111
130
return (
112
- < Grid container gap = { 4 } sx = { { justifyContent : "space-around" } } >
113
- { loading && < CircularProgress /> }
114
- { posts . length === 0 && ! loading ? (
115
- < Grid >
116
- < Typography variant = "body1" > No pinned posts</ Typography >
117
- </ Grid >
118
- ) : (
119
- posts . map ( ( post , index ) => (
120
- < Post key = { index } post = { post } username = { username } />
121
- ) )
122
- ) }
131
+ < Grid
132
+ container
133
+ gap = { 4 }
134
+ sx = { { justifyContent : "space-around" , paddingTop : 0 } }
135
+ ref = { containerRef }
136
+ columns = { cols }
137
+ >
138
+ < ImageList
139
+ variant = "masonry"
140
+ cols = { cols }
141
+ gap = { 12 }
142
+ rowHeight = { "auto" }
143
+ sx = { { marginTop : 0 } }
144
+ >
145
+ { loading && < CircularProgress /> }
146
+ { posts . length === 0 && ! loading ? (
147
+ < Grid >
148
+ < Typography variant = "body1" > No pinned posts</ Typography >
149
+ </ Grid >
150
+ ) : (
151
+ posts . map ( ( post , index ) => (
152
+ < Post key = { index } post = { post } username = { username } />
153
+ ) )
154
+ ) }
155
+ </ ImageList >
123
156
</ Grid >
124
157
) ;
125
158
} ;
0 commit comments