diff --git a/blocks/query/edit.tsx b/blocks/query/edit.tsx index 1d36a0b8..279c97a8 100644 --- a/blocks/query/edit.tsx +++ b/blocks/query/edit.tsx @@ -63,6 +63,7 @@ export default function Edit({ orderby = 'date', moveData = {}, }, + clientId, setAttributes, }: EditProps) { const { @@ -79,7 +80,12 @@ export default function Edit({ } // @ts-ignore - const [isPostDeduplicating, postTypeObject, uniquePinnedPosts] = useSelect( + const [ + isPostDeduplicating, + postTypeObject, + uniquePinnedPosts, + getBlockIndexFunction, + ] = useSelect( (select) => { // @ts-ignore const editor = select('core/editor'); @@ -89,15 +95,21 @@ export default function Edit({ // @ts-ignore const meta = editor.getEditedPostAttribute('meta'); + const blockEditor = select('core/block-editor'); + const { getBlockIndex } = blockEditor; + return [ // It's possible for usePostMetaValue() to run here before useEntityProp() is available. Boolean(meta?.wp_curate_deduplication), // @ts-ignore type ? select('core').getPostType(type) : null, Boolean(meta?.wp_curate_unique_pinned_posts), + getBlockIndex, ]; }, + [], ); + const blockIndex = getBlockIndexFunction(clientId); const debouncedSearchTerm = useDebounce(searchTerm ?? '', 500); @@ -142,7 +154,7 @@ export default function Edit({ // The query is passed via context to the core/post-template block. useEffect(() => { if (data && !error) { - mainDedupe(['wp-curate/query', 'wp-curate/subquery']); + mainDedupe(); } }, [ manualPostIds, @@ -155,6 +167,7 @@ export default function Edit({ uniquePinnedPosts, data, error, + blockIndex, ]); // Make sure all the manual posts are still valid. diff --git a/blocks/query/types.ts b/blocks/query/types.ts index 3c97d8c1..82175746 100644 --- a/blocks/query/types.ts +++ b/blocks/query/types.ts @@ -25,6 +25,7 @@ interface EditProps { }; uniqueId?: string; }; + clientId: string; setAttributes: (attributes: any) => void; context: { postId: number; diff --git a/blocks/subquery/edit.tsx b/blocks/subquery/edit.tsx index af4d04df..ee49eb82 100644 --- a/blocks/subquery/edit.tsx +++ b/blocks/subquery/edit.tsx @@ -185,7 +185,7 @@ export default function Edit({ if (!backfillPostsString.length) { return; } - mainDedupe(['wp-curate/subquery']); + mainDedupe(); }, [ backfillPostsString, deduplication, @@ -203,7 +203,7 @@ export default function Edit({ if (index !== 0) { return; } - mainDedupe(['wp-curate/query', 'wp-curate/subquery']); + mainDedupe(); }, [ index, manualPostIds, diff --git a/services/deduplicate/index.ts b/services/deduplicate/index.ts index ea20aaef..b18dd2cf 100644 --- a/services/deduplicate/index.ts +++ b/services/deduplicate/index.ts @@ -81,7 +81,7 @@ const getQueryBlocks = (blocks: Block[], blockNames: string[], out: Block[]) => * This is the main function to update all pinned posts. Call it whenever a pinned post * changes or the query settings change. */ -export function mainDedupe(blockNames: string[] = ['wp-curate/query']) { +export function mainDedupe() { if (running) { // Only one run at a time, but mark that another run has been requested. redo = true; @@ -106,17 +106,13 @@ export function mainDedupe(blockNames: string[] = ['wp-curate/query']) { } = select('core/editor').getEditedPostAttribute('meta') || {}; const queryBlocks: Block[] = []; - // Loop through all blocks and find all query blocks. - getQueryBlocks(blocks, blockNames, queryBlocks); - - const allQueryBlocks: Block[] = []; - getQueryBlocks(blocks, ['wp-curate/query', 'wp-curate/subquery'], allQueryBlocks); + getQueryBlocks(blocks, ['wp-curate/query', 'wp-curate/subquery'], queryBlocks); /** * This block of code is responsible for enforcing the unique pinned posts setting in the editor. */ if (wpCurateUniquePinnedPosts) { - allQueryBlocks.forEach((queryBlock) => { + queryBlocks.forEach((queryBlock) => { queryBlock?.attributes?.posts?.forEach((post) => { if (post) { deduplicate(post); @@ -125,21 +121,6 @@ export function mainDedupe(blockNames: string[] = ['wp-curate/query']) { }); } - if (queryBlocks.length !== allQueryBlocks.length) { - allQueryBlocks.forEach((queryBlock) => { - if (queryBlock.attributes.query) { - const posts: string[] | undefined = queryBlock.attributes.query?.include?.toString().split(','); - if (posts) { - posts.forEach((post) => { - if (post) { - deduplicate(post); - } - }); - } - } - }); - } - // Loop through all query blocks and set backfilled posts in the open slots. queryBlocks.forEach((queryBlock) => { const { attributes } = queryBlock;