-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.tsx
212 lines (192 loc) · 5.76 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { createContext, useCallback, useContext, useReducer } from 'react';
import { noop } from 'lodash/fp';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { FilterManager } from '../../../../../../../src/plugins/data/public/query/filter_manager';
import { SubsetTimelineModel } from '../../store/timeline/model';
import * as i18n from '../../../common/components/events_viewer/translations';
import * as i18nF from '../timeline/footer/translations';
import { timelineDefaults as timelineDefaultModel } from '../../store/timeline/defaults';
interface ManageTimelineInit {
documentType?: string;
defaultModel?: SubsetTimelineModel;
filterManager?: FilterManager;
footerText?: string;
id: string;
loadingText?: string;
selectAll?: boolean;
queryFields?: string[];
title?: string;
unit?: (totalCount: number) => string;
}
interface ManageTimeline {
documentType: string;
defaultModel: SubsetTimelineModel;
filterManager?: FilterManager;
footerText: string;
id: string;
isLoading: boolean;
loadingText: string;
queryFields: string[];
selectAll: boolean;
title: string;
unit: (totalCount: number) => string;
}
interface ManageTimelineById {
[id: string]: ManageTimeline;
}
const initManageTimeline: ManageTimelineById = {};
type ActionManageTimeline =
| {
type: 'INITIALIZE_TIMELINE';
id: string;
payload: ManageTimelineInit;
}
| {
type: 'SET_IS_LOADING';
id: string;
payload: boolean;
}
| {
type: 'SET_SELECT_ALL';
id: string;
payload: boolean;
};
export const getTimelineDefaults = (id: string) => ({
defaultModel: timelineDefaultModel,
loadingText: i18n.LOADING_EVENTS,
footerText: i18nF.TOTAL_COUNT_OF_EVENTS,
documentType: i18nF.TOTAL_COUNT_OF_EVENTS,
selectAll: false,
id,
isLoading: false,
queryFields: [],
title: i18n.EVENTS,
unit: (n: number) => i18n.UNIT(n),
});
const reducerManageTimeline = (
state: ManageTimelineById,
action: ActionManageTimeline
): ManageTimelineById => {
switch (action.type) {
case 'INITIALIZE_TIMELINE':
return {
...state,
[action.id]: {
...getTimelineDefaults(action.id),
...state[action.id],
...action.payload,
},
} as ManageTimelineById;
case 'SET_SELECT_ALL':
return {
...state,
[action.id]: {
...state[action.id],
selectAll: action.payload,
},
} as ManageTimelineById;
case 'SET_IS_LOADING':
return {
...state,
[action.id]: {
...state[action.id],
isLoading: action.payload,
},
} as ManageTimelineById;
default:
return state;
}
};
export interface UseTimelineManager {
getManageTimelineById: (id: string) => ManageTimeline;
getTimelineFilterManager: (id: string) => FilterManager | undefined;
initializeTimeline: (newTimeline: ManageTimelineInit) => void;
isManagedTimeline: (id: string) => boolean;
setIsTimelineLoading: (isLoadingArgs: { id: string; isLoading: boolean }) => void;
setSelectAll: (selectAllArgs: { id: string; selectAll: boolean }) => void;
}
export const useTimelineManager = (
manageTimelineForTesting?: ManageTimelineById
): UseTimelineManager => {
const [state, dispatch] = useReducer<
(state: ManageTimelineById, action: ActionManageTimeline) => ManageTimelineById
>(reducerManageTimeline, manageTimelineForTesting ?? initManageTimeline);
const initializeTimeline = useCallback((newTimeline: ManageTimelineInit) => {
dispatch({
type: 'INITIALIZE_TIMELINE',
id: newTimeline.id,
payload: newTimeline,
});
}, []);
const setIsTimelineLoading = useCallback(
({ id, isLoading }: { id: string; isLoading: boolean }) => {
dispatch({
type: 'SET_IS_LOADING',
id,
payload: isLoading,
});
},
[]
);
const setSelectAll = useCallback(({ id, selectAll }: { id: string; selectAll: boolean }) => {
dispatch({
type: 'SET_SELECT_ALL',
id,
payload: selectAll,
});
}, []);
const getTimelineFilterManager = useCallback(
(id: string): FilterManager | undefined => state[id]?.filterManager,
[state]
);
const getManageTimelineById = useCallback(
(id: string): ManageTimeline => {
if (state[id] != null) {
return state[id];
}
initializeTimeline({ id });
return getTimelineDefaults(id);
},
[initializeTimeline, state]
);
const isManagedTimeline = useCallback((id: string): boolean => state[id] != null, [state]);
return {
getManageTimelineById,
getTimelineFilterManager,
initializeTimeline,
isManagedTimeline,
setIsTimelineLoading,
setSelectAll,
};
};
const init = {
getManageTimelineById: (id: string) => getTimelineDefaults(id),
getTimelineFilterManager: () => undefined,
initializeTimeline: () => noop,
isManagedTimeline: () => false,
setIsTimelineLoading: () => noop,
setSelectAll: () => noop,
};
const ManageTimelineContext = createContext<UseTimelineManager>(init);
export const useManageTimeline = () => useContext(ManageTimelineContext);
interface ManageGlobalTimelineProps {
children: React.ReactNode;
manageTimelineForTesting?: ManageTimelineById;
}
export const ManageGlobalTimeline = ({
children,
manageTimelineForTesting,
}: ManageGlobalTimelineProps) => {
const timelineManager = useTimelineManager(manageTimelineForTesting);
return (
<ManageTimelineContext.Provider value={timelineManager}>
{children}
</ManageTimelineContext.Provider>
);
};