@@ -51,6 +51,7 @@ export default function Community() {
5151 const [ searchOperator , setSearchOperator ] = useState ( DEFAULT_OPERATOR ) ;
5252
5353 const PAGE_SIZE = 10 ;
54+ const PAGE_BUTTON_LIMIT = 5 ;
5455
5556 useEffect ( ( ) => {
5657 let ignore = false ;
@@ -332,9 +333,16 @@ export default function Community() {
332333 setLastKnownPage ( currentPage ) ;
333334 } , [ currentPage , totalPages ] ) ;
334335
335- const pageNumbers = useMemo ( ( ) => (
336- Array . from ( { length : totalPages } , ( _ , index ) => index + 1 )
337- ) , [ totalPages ] ) ;
336+ const pageNumbers = useMemo ( ( ) => {
337+ if ( totalPages <= 0 ) return [ ] ;
338+ if ( totalPages <= PAGE_BUTTON_LIMIT ) {
339+ return Array . from ( { length : totalPages } , ( _ , index ) => index + 1 ) ;
340+ }
341+ const chunkIndex = Math . floor ( ( currentPage - 1 ) / PAGE_BUTTON_LIMIT ) ;
342+ const start = chunkIndex * PAGE_BUTTON_LIMIT + 1 ;
343+ const end = Math . min ( start + PAGE_BUTTON_LIMIT - 1 , totalPages ) ;
344+ return Array . from ( { length : end - start + 1 } , ( _ , index ) => start + index ) ;
345+ } , [ currentPage , totalPages ] ) ;
338346
339347 const paginatedPosts = useMemo ( ( ) => {
340348 const start = ( currentPage - 1 ) * PAGE_SIZE ;
@@ -421,21 +429,22 @@ export default function Community() {
421429 . slice ( 0 , 5 ) ;
422430 } , [ posts ] ) ;
423431
424- const jumpBy = 5 ;
425432 const goToPage = ( page ) => {
426433 if ( page < 1 || page > totalPages ) return ;
427434 setCurrentPage ( page ) ;
428435 } ;
429436
430- const handleChunkMove = ( direction ) => {
431- const target = currentPage + direction * jumpBy ;
432- if ( target < 1 ) {
433- setCurrentPage ( 1 ) ;
434- } else if ( target > totalPages ) {
435- setCurrentPage ( totalPages ) ;
436- } else {
437- setCurrentPage ( target ) ;
438- }
437+ const canGoPrev = currentPage > 1 ;
438+ const canGoNext = currentPage < totalPages ;
439+
440+ const handlePrevPage = ( ) => {
441+ if ( ! canGoPrev ) return ;
442+ goToPage ( currentPage - 1 ) ;
443+ } ;
444+
445+ const handleNextPage = ( ) => {
446+ if ( ! canGoNext ) return ;
447+ goToPage ( currentPage + 1 ) ;
439448 } ;
440449
441450 return (
@@ -649,17 +658,17 @@ export default function Community() {
649658 type = "button"
650659 className = "page-button"
651660 onClick = { ( ) => goToPage ( 1 ) }
652- disabled = { currentPage === 1 }
661+ disabled = { ! canGoPrev }
653662 >
654- 처음
663+ <<
655664 </ button >
656665 < button
657666 type = "button"
658667 className = "page-button"
659- onClick = { ( ) => handleChunkMove ( - 1 ) }
660- disabled = { currentPage === 1 }
668+ onClick = { handlePrevPage }
669+ disabled = { ! canGoPrev }
661670 >
662- -5쪽
671+ <
663672 </ button >
664673 { pageNumbers . map ( ( page ) => (
665674 < button
@@ -674,18 +683,18 @@ export default function Community() {
674683 < button
675684 type = "button"
676685 className = "page-button"
677- onClick = { ( ) => handleChunkMove ( 1 ) }
678- disabled = { currentPage === totalPages }
686+ onClick = { handleNextPage }
687+ disabled = { ! canGoNext }
679688 >
680- +5쪽
689+ >
681690 </ button >
682691 < button
683692 type = "button"
684693 className = "page-button"
685694 onClick = { ( ) => goToPage ( totalPages ) }
686- disabled = { currentPage === totalPages }
695+ disabled = { ! canGoNext }
687696 >
688- 마지막
697+ >>
689698 </ button >
690699 </ div >
691700 </ div >
0 commit comments