summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/actions/components.js
blob: f21c382279293334f8f7f086f5c3fd8704be761a (plain)
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
const ACTION_TYPES = require('./TYPES');

/**
 * Find all components matching a descriptor
 * @param {List<ComponentDescriptor>} state
 * @param {String} matching.role
 */
function findMatchingComponents(state, matching) {
    return state
    .filter(({descriptor}) => {
        if (matching.role && matching.role !== descriptor.role) {
            return false;
        }

        return true;
    })
    .map(component => component.Component);
}

/**
 * Register a new component
 * @param  {React.Class} Component
 * @param  {Descriptor} descriptor
 * @return {Action}
 */
function registerComponent(Component, descriptor) {
    return {
        type: ACTION_TYPES.REGISTER_COMPONENT,
        Component,
        descriptor
    };
}

module.exports = {
    findMatchingComponents,
    registerComponent
};