This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathutils.tsx
100 lines (89 loc) · 2.67 KB
/
utils.tsx
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
92
93
94
95
96
97
98
99
100
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as WP_BLOCKS_STORE } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { QUERY_LOOP_ID } from './constants';
import {
ProductQueryBlock,
ProductQueryBlockQuery,
QueryVariation,
} from './types';
/**
* Creates an array that is the symmetric difference of the given arrays
*/
export function ArrayXOR< T extends Array< unknown > >( a: T, b: T ) {
return a.filter( ( el ) => ! b.includes( el ) );
}
/**
* Identifies if a block is a Query block variation from our conventions
*
* We are extending Gutenberg's core Query block with our variations, and
* also adding extra namespaced attributes. If those namespaced attributes
* are present, we can be fairly sure it is our own registered variation.
*/
export function isWooQueryBlockVariation( block: ProductQueryBlock ) {
return (
block.name === QUERY_LOOP_ID &&
Object.values( QueryVariation ).includes(
block.attributes.namespace as QueryVariation
)
);
}
/**
* Sets the new query arguments of a Product Query block
*
* Shorthand for setting new nested query parameters.
*/
export function setQueryAttribute(
block: ProductQueryBlock,
queryParams: Partial< ProductQueryBlockQuery >
) {
const { query } = block.attributes;
block.setAttributes( {
query: {
...query,
...queryParams,
},
} );
}
// This is a feature flag to enable the custom inherit Global Query implementation.
// This is not intended to be a permanent feature flag, but rather a temporary.
// https://github.com/woocommerce/woocommerce-blocks/pull/7382
export const isCustomInheritGlobalQueryImplementationEnabled = false;
export function isWooInheritQueryEnabled(
attributes: ProductQueryBlock[ 'attributes' ]
) {
return isCustomInheritGlobalQueryImplementationEnabled
? attributes.query.__woocommerceInherit
: attributes.query.inherit;
}
/**
* Hook that returns the query properties' names defined by the active
* block variation, to determine which block inspector controls to show.
*
* @param {Object} attributes Block attributes.
* @return {string[]} An array of the controls keys.
*/
export function useAllowedControls(
attributes: ProductQueryBlock[ 'attributes' ]
) {
const isSiteEditor = useSelect( 'core/edit-site' ) !== undefined;
const controls = useSelect(
( select ) =>
select( WP_BLOCKS_STORE ).getActiveBlockVariation(
QUERY_LOOP_ID,
attributes
)?.allowedControls,
[ attributes ]
);
if ( ! isSiteEditor ) {
return controls.filter( ( control ) => control !== 'wooInherit' );
}
return isWooInheritQueryEnabled( attributes )
? controls.filter( ( control ) => control === 'wooInherit' )
: controls;
}