-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathglobal_state_context.tsx
61 lines (52 loc) · 1.88 KB
/
global_state_context.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
/*
* 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 } from 'react';
import { GlobalState } from '../../url_state';
import { MonitoringStartPluginDependencies, MonitoringStartServices } from '../../types';
import { TimeRange, RefreshInterval } from '../../../../../../src/plugins/data/public';
import { Legacy } from '../../legacy_shims';
interface GlobalStateProviderProps {
query: MonitoringStartPluginDependencies['data']['query'];
toasts: MonitoringStartServices['notifications']['toasts'];
}
export interface State {
[key: string]: unknown;
cluster_uuid?: string;
ccs?: any;
inSetupMode?: boolean;
save?: () => void;
time?: TimeRange;
refreshInterval?: RefreshInterval;
}
export const GlobalStateContext = createContext({} as State);
export const GlobalStateProvider: React.FC<GlobalStateProviderProps> = ({
query,
toasts,
children,
}) => {
const localState: State = {};
const state = new GlobalState(query, toasts, localState as { [key: string]: unknown });
const initialState: any = state.getState();
for (const key in initialState) {
if (!initialState.hasOwnProperty(key)) {
continue;
}
localState[key] = initialState[key];
}
localState.refreshInterval = { value: 10000, pause: false };
localState.save = () => {
const newState = { ...localState };
delete newState.save;
state.setState(newState);
};
const { value, pause } = Legacy.shims.timefilter.getRefreshInterval();
if (!value && pause) {
Legacy.shims.timefilter.setRefreshInterval(localState.refreshInterval);
localState.save?.();
}
return <GlobalStateContext.Provider value={localState}>{children}</GlobalStateContext.Provider>;
};