-
Notifications
You must be signed in to change notification settings - Fork 888
/
workspace.tsx
166 lines (148 loc) · 5.72 KB
/
workspace.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { i18n } from '@osd/i18n';
import { EuiEmptyPrompt, EuiFlexItem, EuiIcon, EuiPanel, EuiText } from '@elastic/eui';
import React, { useState, useMemo, useEffect, useLayoutEffect } from 'react';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { IExpressionLoaderParams } from '../../../../expressions/public';
import { VisBuilderServices } from '../../types';
import { validateSchemaState, validateAggregations } from '../utils/validations';
import { useTypedDispatch, useTypedSelector, setUIStateState } from '../utils/state_management';
import { useAggs, useVisualizationType } from '../utils/use';
import { PersistedState } from '../../../../visualizations/public';
import { VISBUILDER_ENABLE_VEGA_SETTING } from '../../../common/constants';
import hand_field from '../../assets/hand_field.svg';
import fields_bg from '../../assets/fields_bg.svg';
import './workspace.scss';
import { handleVisEvent } from '../utils/handle_vis_event';
export const WorkspaceUI = () => {
const {
services: {
expressions: { ReactExpressionRenderer },
notifications: { toasts },
data,
uiActions,
uiSettings,
},
} = useOpenSearchDashboards<VisBuilderServices>();
const { toExpression, ui } = useVisualizationType();
const { aggConfigs, indexPattern } = useAggs();
const [expression, setExpression] = useState<string>();
const [searchContext, setSearchContext] = useState<IExpressionLoaderParams['searchContext']>({
query: data.query.queryString.getQuery(),
filters: data.query.filterManager.getFilters(),
timeRange: data.query.timefilter.timefilter.getTime(),
});
const useVega = uiSettings.get(VISBUILDER_ENABLE_VEGA_SETTING);
const rootState = useTypedSelector((state) => state);
const dispatch = useTypedDispatch();
// Visualizations require the uiState object to persist even when the expression changes
// eslint-disable-next-line react-hooks/exhaustive-deps
const uiState = useMemo(() => new PersistedState(rootState.ui), []);
useEffect(() => {
if (rootState.metadata.editor.state === 'loaded') {
uiState.setSilent(rootState.ui);
}
// To update uiState once saved object data is loaded
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [rootState.metadata.editor.state, uiState]);
useEffect(() => {
uiState.on('change', (args) => {
// Store changes to UI state
dispatch(setUIStateState(uiState.toJSON()));
});
}, [dispatch, uiState]);
useEffect(() => {
async function loadExpression() {
const schemas = ui.containerConfig.data.schemas;
const noAggs = (aggConfigs?.aggs?.length ?? 0) === 0;
const schemaValidation = validateSchemaState(schemas, rootState.visualization);
const aggValidation = validateAggregations(aggConfigs?.aggs || []);
if (!aggValidation.valid || !schemaValidation.valid) {
setExpression(undefined);
if (noAggs) return; // don't show error when there are no active aggregations
const err = schemaValidation.errorMsg || aggValidation.errorMsg;
if (err)
toasts.addWarning({
id: 'vb_expression_validation',
title: err,
});
return;
}
const exp = await toExpression(rootState, searchContext, useVega);
setExpression(exp);
}
loadExpression();
}, [
rootState,
toExpression,
toasts,
ui.containerConfig.data.schemas,
searchContext,
aggConfigs,
useVega,
]);
useLayoutEffect(() => {
const subscription = data.query.state$.subscribe(({ state }) => {
setSearchContext({
query: state.query,
timeRange: state.time,
filters: state.filters,
});
});
return () => {
subscription.unsubscribe();
};
}, [data.query.state$]);
return (
<section className="vbWorkspace">
<EuiPanel className="vbCanvas" data-test-subj="visualizationLoader">
{expression ? (
<ReactExpressionRenderer
expression={expression}
searchContext={searchContext}
uiState={uiState}
onEvent={(event) => handleVisEvent(event, uiActions, indexPattern?.timeFieldName)}
/>
) : (
<EuiFlexItem className="vbWorkspace__empty" data-test-subj="emptyWorkspace">
<EuiEmptyPrompt
title={
<h2>
{i18n.translate('visBuilder.workSpace.empty.title', {
defaultMessage: 'Add a field to start',
})}
</h2>
}
body={
<>
<EuiText size="s">
<p>
{i18n.translate('visBuilder.workSpace.empty.description', {
defaultMessage:
'Drag a field to the configuration panel to generate a visualization.',
})}
</p>
</EuiText>
<div className="vbWorkspace__container">
<EuiIcon className="vbWorkspace__fieldSvg" type={fields_bg} size="original" />
<EuiIcon
className="vbWorkspace__handFieldSvg"
type={hand_field}
size="original"
/>
</div>
</>
}
/>
</EuiFlexItem>
)}
</EuiPanel>
</section>
);
};
// The app uses EuiResizableContainer that triggers a rerender for every mouseover action.
// To prevent this child component from unnecessarily rerendering in that instance, it needs to be memoized
export const Workspace = React.memo(WorkspaceUI);