Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect: Add FreeList and PaidList pagination #39058

Merged
merged 13 commits into from
Sep 5, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Adds threats list pagination
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { useCallback } from 'react';
import { JETPACK_SCAN_SLUG } from '../../constants';
import useAnalyticsTracks from '../../hooks/use-analytics-tracks';
import FreeAccordion, { FreeAccordionItem } from '../free-accordion';
import Pagination from './pagination';
import styles from './styles.module.scss';

const ThreatAccordionItem = ( {
Expand Down Expand Up @@ -85,38 +86,42 @@ const ThreatAccordionItem = ( {

const FreeList = ( { list } ) => {
return (
<FreeAccordion>
{ list.map(
( {
description,
fixedIn,
icon,
id,
label,
name,
source,
table,
title,
type,
version,
} ) => (
<ThreatAccordionItem
description={ description }
fixedIn={ fixedIn }
icon={ icon }
id={ id }
label={ label }
key={ id }
name={ name }
source={ source }
table={ table }
title={ title }
type={ type }
version={ version }
/>
)
<Pagination list={ list }>
{ ( { currentItems } ) => (
<FreeAccordion>
{ currentItems.map(
( {
description,
fixedIn,
icon,
id,
label,
name,
source,
table,
title,
type,
version,
} ) => (
<ThreatAccordionItem
description={ description }
fixedIn={ fixedIn }
icon={ icon }
id={ id }
label={ label }
key={ id }
name={ name }
source={ source }
table={ table }
title={ title }
type={ type }
version={ version }
/>
)
) }
</FreeAccordion>
) }
</FreeAccordion>
</Pagination>
);
};

Expand Down
159 changes: 159 additions & 0 deletions projects/plugins/protect/src/js/components/threats-list/pagination.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Button } from '@automattic/jetpack-components';
import React, { useCallback, useEffect, useState, useMemo, memo } from 'react';
import styles from './styles.module.scss';

const PaginationButton = memo( ( { pageNumber, currentPage, onPageChange } ) => {
const handleClick = useCallback( () => {
onPageChange( pageNumber );
}, [ onPageChange, pageNumber ] );

return (
<Button
onClick={ handleClick }
variant={ currentPage === pageNumber ? 'primary' : 'secondary' }
aria-current={ currentPage === pageNumber ? 'page' : undefined }
>
{ pageNumber }
</Button>
);
} );

const Pagination = ( { list, itemPerPage = 10, children } ) => {
const [ currentPage, setCurrentPage ] = useState( 1 );

const totalPages = useMemo( () => Math.ceil( list.length / itemPerPage ), [ list, itemPerPage ] );

const currentItems = useMemo( () => {
const indexOfLastItem = currentPage * itemPerPage;
const indexOfFirstItem = indexOfLastItem - itemPerPage;
return list.slice( indexOfFirstItem, indexOfLastItem );
}, [ currentPage, list, itemPerPage ] );

const onPageChange = useCallback( pageNumber => {
setCurrentPage( pageNumber );
}, [] );

const [ isSmall, setIsSmall ] = useState( window.matchMedia( '(max-width: 1220px)' ).matches );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could useBreakpointMatch be useful here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with useBreakpointMatch but found that we seem to have a unique requirement with how our UI shifts in various viewports that was not exactly covered by the options it provided (isSm, isMd, isLg).


useEffect( () => {
const mediaQuery = window.matchMedia( '(max-width: 1220px)' );

const handleMediaChange = event => {
setIsSmall( event.matches );
};

// Add event listeners
mediaQuery.addEventListener( 'change', handleMediaChange );

// Cleanup listeners on component unmount
return () => {
mediaQuery.removeEventListener( 'change', handleMediaChange );
};
}, [] );

const handleFirstPageClick = useCallback( () => {
onPageChange( 1 );
}, [ onPageChange ] );

const handlePreviousPageClick = useCallback( () => {
onPageChange( currentPage - 1 );
}, [ currentPage, onPageChange ] );

const handleNextPageClick = useCallback( () => {
onPageChange( currentPage + 1 );
}, [ currentPage, onPageChange ] );

const handleLastPageClick = useCallback( () => {
onPageChange( totalPages );
}, [ onPageChange, totalPages ] );

const getPageNumbers = useCallback( () => {
const pageNumbers = [];

if ( ! isSmall ) {
pageNumbers.push( 1 );
}

const start = isSmall ? 1 : 2;
const offset = isSmall ? 0 : 2;
const end = isSmall ? 0 : 1;
const startPage = Math.max( start, currentPage - offset );
const endPage = Math.min( totalPages - end, currentPage + offset );

if ( startPage > 2 && ! isSmall ) {
pageNumbers.push( '...' );
}

for ( let i = startPage; i <= endPage; i++ ) {
pageNumbers.push( i );
}

if ( endPage < totalPages - 1 && ! isSmall ) {
pageNumbers.push( '...' );
}

if ( totalPages > 1 && ! isSmall ) {
pageNumbers.push( totalPages );
}

return pageNumbers;
}, [ currentPage, totalPages, isSmall ] );

return (
<>
{ children( { currentItems } ) }
{ totalPages > 1 && (
<div className={ styles[ 'pagination-container' ] }>
{ isSmall && (
<Button
onClick={ handleFirstPageClick }
disabled={ currentPage === 1 }
variant={ 'secondary' }
>
{ 1 }
</Button>
) }
<Button
onClick={ handlePreviousPageClick }
disabled={ currentPage === 1 }
variant={ 'secondary' }
>
{ '<' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: could use an icon here from @wordpress/icons

</Button>
{ getPageNumbers().map( ( pageNumber, index ) =>
typeof pageNumber === 'number' ? (
<PaginationButton
key={ index }
pageNumber={ pageNumber }
currentPage={ currentPage }
onPageChange={ onPageChange }
/>
) : (
<span key={ index } className={ styles.ellipsis }>
{ pageNumber }
</span>
)
) }
<Button
onClick={ handleNextPageClick }
disabled={ currentPage === totalPages }
variant={ 'secondary' }
>
{ '>' }
</Button>
{ isSmall && (
<Button
onClick={ handleLastPageClick }
disabled={ currentPage === totalPages }
variant={ 'secondary' }
>
{ totalPages }
</Button>
) }
</div>
) }
</>
);
};

export default Pagination;
103 changes: 54 additions & 49 deletions projects/plugins/protect/src/js/components/threats-list/paid-list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { STORE_ID } from '../../state/store';
import DiffViewer from '../diff-viewer';
import MarkedLines from '../marked-lines';
import PaidAccordion, { PaidAccordionItem } from '../paid-accordion';
import Pagination from './pagination';
import styles from './styles.module.scss';

const ThreatAccordionItem = ( {
Expand Down Expand Up @@ -177,62 +178,66 @@ const PaidList = ( { list, hideAutoFixColumn = false } ) => {
return (
<>
{ ! isSmall && (
<div className={ styles[ 'accordion-heading' ] }>
<div className={ styles[ 'accordion-header' ] }>
<span>{ __( 'Details', 'jetpack-protect' ) }</span>
<span>{ __( 'Severity', 'jetpack-protect' ) }</span>
{ ! hideAutoFixColumn && <span>{ __( 'Auto-fix', 'jetpack-protect' ) }</span> }
<span></span>
</div>
) }
<PaidAccordion>
{ list.map(
( {
context,
description,
diff,
filename,
firstDetected,
fixedIn,
fixedOn,
icon,
fixable,
id,
label,
name,
severity,
source,
table,
title,
type,
version,
status,
} ) => (
<ThreatAccordionItem
context={ context }
description={ description }
diff={ diff }
filename={ filename }
firstDetected={ firstDetected }
fixedIn={ fixedIn }
fixedOn={ fixedOn }
icon={ icon }
fixable={ fixable }
id={ id }
key={ id }
label={ label }
name={ name }
severity={ severity }
source={ source }
table={ table }
title={ title }
type={ type }
version={ version }
status={ status }
hideAutoFixColumn={ hideAutoFixColumn }
/>
)
<Pagination list={ list }>
{ ( { currentItems } ) => (
<PaidAccordion>
{ currentItems.map(
( {
context,
description,
diff,
filename,
firstDetected,
fixedIn,
fixedOn,
icon,
fixable,
id,
label,
name,
severity,
source,
table,
title,
type,
version,
status,
} ) => (
<ThreatAccordionItem
context={ context }
description={ description }
diff={ diff }
filename={ filename }
firstDetected={ firstDetected }
fixedIn={ fixedIn }
fixedOn={ fixedOn }
icon={ icon }
fixable={ fixable }
id={ id }
key={ id }
label={ label }
name={ name }
severity={ severity }
source={ source }
table={ table }
title={ title }
type={ type }
version={ version }
status={ status }
hideAutoFixColumn={ hideAutoFixColumn }
/>
)
) }
</PaidAccordion>
) }
</PaidAccordion>
</Pagination>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}
}

.accordion-heading {
.accordion-header {
display: grid;
grid-template-columns: repeat( 9, 1fr );
background-color: white;
Expand Down Expand Up @@ -99,3 +99,15 @@
}
}
}

.pagination-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: calc( var( --spacing-base ) * 2 ); // 16px
gap: var( --spacing-base ); // 8px
}

.ellipsis {
padding: var( --spacing-base ); // 8px
}