1- import { useState , useEffect } from "react" ;
1+ import { useEffect , useRef , useState } from "react" ;
22import {
33 Button ,
44 CircularProgress ,
55 Divider ,
6+ ImageList ,
7+ ImageListItem ,
68 Stack ,
79 Typography ,
810} from "@mui/material" ;
@@ -16,11 +18,10 @@ const Post = ({ post, username }) => {
1618 const { share } = post ;
1719
1820 return (
19- < Grid >
21+ < ImageListItem key = { share } >
2022 < Stack
2123 sx = { {
22- maxWidth : "300px" ,
23- minWidth : "200px" ,
24+ display : "inline-block" ,
2425 borderRadius : 2 ,
2526 border : "solid 1px #eee" ,
2627 padding : 2 ,
@@ -84,13 +85,31 @@ const Post = ({ post, username }) => {
8485 </ Button >
8586 </ Stack >
8687 </ Stack >
87- </ Grid >
88+ </ ImageListItem >
8889 ) ;
8990} ;
9091
9192const PinnedPosts = ( { username } ) => {
9293 const [ posts , setPosts ] = useState ( [ ] ) ;
9394 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 ] ) ;
94113
95114 useEffect ( ( ) => {
96115 // TODO: handle pagination
@@ -109,17 +128,31 @@ const PinnedPosts = ({ username }) => {
109128 } , [ username ] ) ;
110129
111130 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 >
123156 </ Grid >
124157 ) ;
125158} ;
0 commit comments