-
Notifications
You must be signed in to change notification settings - Fork 11
/
useSelectAll.ts
48 lines (39 loc) · 1.08 KB
/
useSelectAll.ts
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
import { useCallback } from 'react';
import { useDispatch, useStore } from '../states/store';
interface Options {
items?: boolean;
labels?: boolean;
}
export function useSelectAllEntities(): ( options: Options ) => void {
const dispatch = useDispatch();
const store = useStore();
const selectAllEntities = useCallback(
( options: Options ): void => {
const state = store.getState();
dispatch( {
type: 'Timeline/SelectItems',
items: [],
} );
if ( options.items ) {
const items = Object.entries( state.automaton.channels ).map(
( [ channel, { items } ] ) => (
Object.keys( items ).map( ( id ) => ( { id, channel } ) )
)
).flat();
dispatch( {
type: 'Timeline/SelectItemsAdd',
items,
} );
}
if ( options.labels ) {
const labels = Object.keys( state.automaton.labels );
dispatch( {
type: 'Timeline/SelectLabelsAdd',
labels,
} );
}
},
[ dispatch, store ]
);
return selectAllEntities;
}