-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Try patterns server side pagination #52396
Changes from all commits
57666b7
88b88ab
b7d8889
dbe0487
73afc1c
71dd486
e410893
6028d7a
ce8a398
b6c484e
10d7bdb
07c92f6
a5a9ae7
e6068c1
4e70212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalVStack as VStack, | ||
__experimentalHeading as Heading, | ||
__experimentalText as Text, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import usePatternCategories from '../sidebar-navigation-screen-patterns/use-pattern-categories'; | ||
import { | ||
USER_PATTERN_CATEGORY, | ||
USER_PATTERNS, | ||
TEMPLATE_PARTS, | ||
PATTERNS, | ||
} from './utils'; | ||
|
||
export default function PatternsHeader( { | ||
categoryId, | ||
type, | ||
titleId, | ||
descriptionId, | ||
} ) { | ||
const { patternCategories } = usePatternCategories(); | ||
const templatePartAreas = useSelect( | ||
( select ) => | ||
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(), | ||
[] | ||
); | ||
|
||
let title, description; | ||
if ( categoryId === USER_PATTERN_CATEGORY && type === USER_PATTERNS ) { | ||
title = __( 'My Patterns' ); | ||
description = __( 'Patterns that are kept in sync across your site.' ); // TODO | ||
} else if ( type === TEMPLATE_PARTS ) { | ||
const templatePartArea = templatePartAreas.find( | ||
( area ) => area.area === categoryId | ||
); | ||
title = templatePartArea?.label; | ||
description = templatePartArea?.description; | ||
} else if ( type === PATTERNS ) { | ||
const patternCategory = patternCategories.find( | ||
( category ) => category.name === categoryId | ||
); | ||
title = patternCategory?.label; | ||
description = patternCategory?.description; | ||
} | ||
|
||
if ( ! title ) return null; | ||
|
||
return ( | ||
<VStack className="edit-site-patterns__section-header"> | ||
<Heading as="h2" level={ 4 } id={ titleId }> | ||
{ title } | ||
</Heading> | ||
{ description ? ( | ||
<Text variant="muted" as="p" id={ descriptionId }> | ||
{ description } | ||
</Text> | ||
) : null } | ||
</VStack> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
useLayoutEffect, | ||
useEffect, | ||
useRef, | ||
useState, | ||
startTransition, | ||
} from '@wordpress/element'; | ||
import { __experimentalHStack as HStack, Button } from '@wordpress/components'; | ||
|
||
export default function PatternsPagination( { | ||
patterns, | ||
page, | ||
setPage, | ||
getTotalPages, | ||
} ) { | ||
const [ totalPages, setTotalPages ] = useState( page ); | ||
const getTotalPagesRef = useRef( getTotalPages ); | ||
useLayoutEffect( () => { | ||
getTotalPagesRef.current = getTotalPages; | ||
} ); | ||
|
||
// Refetch total pages when `patterns` changes. | ||
// This is not a good indicator of when to refetch the total pages, | ||
// but the only one we have for now. | ||
useEffect( () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not fine-tuned. It currently will fire a lot of requests since we're not sure when to fetch this info. A more elegant solution will be baking this into |
||
const abortController = new AbortController(); | ||
const signal = abortController.signal; | ||
getTotalPagesRef | ||
.current( { signal } ) | ||
.then( ( pages ) => setTotalPages( pages ) ) | ||
.catch( () => setTotalPages( 1 ) ); | ||
return () => { | ||
abortController.abort(); | ||
}; | ||
}, [ patterns ] ); | ||
|
||
const pages = Array.from( | ||
{ length: totalPages }, | ||
( _, index ) => index + 1 | ||
); | ||
|
||
return ( | ||
<HStack spacing={ 2 } alignment="center"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to revisit the styling and accessibility 😅. |
||
{ pages.map( ( p ) => | ||
p === page ? ( | ||
<u aria-current="page" key={ p }> | ||
{ p } | ||
</u> | ||
) : ( | ||
<Button | ||
key={ p } | ||
variant="link" | ||
onClick={ () => { | ||
startTransition( () => { | ||
setPage( p ); | ||
} ); | ||
} } | ||
> | ||
{ p } | ||
</Button> | ||
) | ||
) } | ||
</HStack> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch adds the ability to pass
sync_status
query parameter to the/wp/v2/blocks
api. Allowed values for now arefully
andunsynced
.