-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathblock-health.js
More file actions
91 lines (74 loc) · 2.02 KB
/
Copy pathblock-health.js
File metadata and controls
91 lines (74 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Tracks Otter blocks that fail to render in the editor.
*/
/**
* External dependencies
*/
import { debounce } from 'lodash';
/**
* WordPress dependencies
*/
import { select, subscribe } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { getTrackedBlockNames } from './shared.js';
// ponytail: session-only dedup; sessionStorage if reopen inflation matters.
const reportedSlugs = new Set();
/**
* Walk the block tree and collect slugs for broken Otter blocks.
*
* @param {Array} blocks Blocks to inspect.
* @param {string[]} tracked Otter block names to watch.
* @param {Set<string>} acc Accumulator for errored slugs.
* @return {Set<string>}
*/
const collectErroredSlugs = ( blocks, tracked, acc ) => {
blocks.forEach( block => {
if ( false === block.isValid && tracked.includes( block.name ) ) {
acc.add( block.name );
}
if ( 'core/missing' === block.name ) {
const originalName = block.attributes?.originalName;
if ( originalName && tracked.includes( originalName ) ) {
acc.add( originalName );
}
}
if ( block.innerBlocks?.length ) {
collectErroredSlugs( block.innerBlocks, tracked, acc );
}
});
return acc;
};
/**
* Report newly detected broken Otter blocks.
*/
const checkBlockHealth = () => {
const trackedBlockNames = getTrackedBlockNames();
if ( 0 === trackedBlockNames.length ) {
return;
}
const { getBlocks } = select( blockEditorStore );
const errored = collectErroredSlugs(
getBlocks(),
trackedBlockNames,
new Set()
);
errored.forEach( slug => {
if ( reportedSlugs.has( slug ) ) {
return;
}
reportedSlugs.add( slug );
window.oTrk?.set( `block-health-${ slug }`, {
feature: 'block-health',
featureComponent: 'render-error',
featureValue: slug
});
});
};
export const startBlockHealth = () => {
if ( ! select( blockEditorStore ) || ! select( 'core/blocks' ) ) {
return;
}
setTimeout( () => {
subscribe( debounce( checkBlockHealth, 1000 ), blockEditorStore );
}, 1000 );
};