-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
use-pattern-categories.js
111 lines (100 loc) · 3.14 KB
/
use-pattern-categories.js
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
101
102
103
104
105
106
107
108
109
110
111
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import useDefaultPatternCategories from './use-default-pattern-categories';
import useThemePatterns from './use-theme-patterns';
import usePatterns from '../page-patterns/use-patterns';
import {
PATTERN_TYPES,
PATTERN_DEFAULT_CATEGORY,
PATTERN_USER_CATEGORY,
TEMPLATE_PART_AREA_DEFAULT_CATEGORY,
} from '../../utils/constants';
export default function usePatternCategories() {
const defaultCategories = useDefaultPatternCategories();
defaultCategories.push( {
name: TEMPLATE_PART_AREA_DEFAULT_CATEGORY,
label: __( 'Uncategorized' ),
} );
const themePatterns = useThemePatterns();
const { patterns: userPatterns, categories: userPatternCategories } =
usePatterns( PATTERN_TYPES.user );
const patternCategories = useMemo( () => {
const categoryMap = {};
const categoriesWithCounts = [];
// Create a map for easier counting of patterns in categories.
defaultCategories.forEach( ( category ) => {
if ( ! categoryMap[ category.name ] ) {
categoryMap[ category.name ] = { ...category, count: 0 };
}
} );
userPatternCategories.forEach( ( category ) => {
if ( ! categoryMap[ category.name ] ) {
categoryMap[ category.name ] = { ...category, count: 0 };
}
} );
// Update the category counts to reflect theme registered patterns.
themePatterns.forEach( ( pattern ) => {
pattern.categories?.forEach( ( category ) => {
if ( categoryMap[ category ] ) {
categoryMap[ category ].count += 1;
}
} );
// If the pattern has no categories, add it to uncategorized.
if ( ! pattern.categories?.length ) {
categoryMap.uncategorized.count += 1;
}
} );
// Update the category counts to reflect user registered patterns.
userPatterns.forEach( ( pattern ) => {
pattern.categories?.forEach( ( category ) => {
if ( categoryMap[ category ] ) {
categoryMap[ category ].count += 1;
}
} );
// If the pattern has no categories, add it to uncategorized.
if ( ! pattern.categories?.length ) {
categoryMap.uncategorized.count += 1;
}
} );
// Filter categories so we only have those containing patterns.
[ ...defaultCategories, ...userPatternCategories ].forEach(
( category ) => {
if (
categoryMap[ category.name ].count &&
! categoriesWithCounts.find(
( cat ) => cat.name === category.name
)
) {
categoriesWithCounts.push( categoryMap[ category.name ] );
}
}
);
const sortedCategories = categoriesWithCounts.sort( ( a, b ) =>
a.label.localeCompare( b.label )
);
sortedCategories.unshift( {
name: PATTERN_USER_CATEGORY,
label: __( 'My patterns' ),
count: userPatterns.length,
} );
sortedCategories.unshift( {
name: PATTERN_DEFAULT_CATEGORY,
label: __( 'All patterns' ),
description: __( 'A list of all patterns from all sources.' ),
count: themePatterns.length + userPatterns.length,
} );
return sortedCategories;
}, [
defaultCategories,
themePatterns,
userPatternCategories,
userPatterns,
] );
return { patternCategories, hasPatterns: !! patternCategories.length };
}