-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VisBuilder] 2 way communication using UI actions and bug fixes #3732
Changes from all commits
657d593
c71fc22
ed20b43
61aeccb
f4f2cf0
5c61952
a4dbecf
cb25986
c709fbf
ec7f646
557a2fb
bac1c9f
259de2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import { i18n } from '@osd/i18n'; | ||
import { EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem, EuiIcon, EuiPanel } from '@elastic/eui'; | ||
import React, { FC, useState, useMemo, useEffect, useLayoutEffect } from 'react'; | ||
import React, { useState, useMemo, useEffect, useLayoutEffect } from 'react'; | ||
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; | ||
import { IExpressionLoaderParams } from '../../../../expressions/public'; | ||
import { VisBuilderServices } from '../../types'; | ||
|
@@ -19,17 +19,19 @@ import fields_bg from '../../assets/fields_bg.svg'; | |
|
||
import './workspace.scss'; | ||
import { ExperimentalInfo } from './experimental_info'; | ||
import { handleVisEvent } from '../utils/handle_vis_event'; | ||
|
||
export const Workspace: FC = ({ children }) => { | ||
export const WorkspaceUI = () => { | ||
const { | ||
services: { | ||
expressions: { ReactExpressionRenderer }, | ||
notifications: { toasts }, | ||
data, | ||
uiActions, | ||
}, | ||
} = useOpenSearchDashboards<VisBuilderServices>(); | ||
const { toExpression, ui } = useVisualizationType(); | ||
const { aggConfigs } = useAggs(); | ||
const { aggConfigs, indexPattern } = useAggs(); | ||
const [expression, setExpression] = useState<string>(); | ||
const [searchContext, setSearchContext] = useState<IExpressionLoaderParams['searchContext']>({ | ||
query: data.query.queryString.getQuery(), | ||
|
@@ -44,15 +46,17 @@ export const Workspace: FC = ({ children }) => { | |
async function loadExpression() { | ||
const schemas = ui.containerConfig.data.schemas; | ||
|
||
const noAggs = aggConfigs?.aggs?.length === 0; | ||
const noAggs = (aggConfigs?.aggs?.length ?? 0) === 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opinion - this reads worse to me than
Also, for conditionals like this I'd prefer |
||
const schemaValidation = validateSchemaState(schemas, rootState.visualization); | ||
const aggValidation = validateAggregations(aggConfigs?.aggs || []); | ||
|
||
if (noAggs || !aggValidation.valid || !schemaValidation.valid) { | ||
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(err); | ||
setExpression(undefined); | ||
|
||
return; | ||
} | ||
|
@@ -91,6 +95,7 @@ export const Workspace: FC = ({ children }) => { | |
expression={expression} | ||
searchContext={searchContext} | ||
uiState={uiState} | ||
onEvent={(event) => handleVisEvent(event, uiActions, indexPattern?.timeFieldName)} | ||
/> | ||
) : ( | ||
<EuiFlexItem className="vbWorkspace__empty" data-test-subj="emptyWorkspace"> | ||
|
@@ -127,3 +132,7 @@ export const Workspace: FC = ({ children }) => { | |
</section> | ||
); | ||
}; | ||
|
||
// The app uses EuiResizableContainer that triggers a rerender for ever mouseover action. | ||
// To prevent this child component from unnecessarily rerendering in that instance, it needs to be memoized | ||
export const Workspace = React.memo(WorkspaceUI); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ExpressionRendererEvent } from '../../../../expressions/public'; | ||
import { VIS_EVENT_TO_TRIGGER } from '../../../../visualizations/public'; | ||
import { handleVisEvent } from './handle_vis_event'; | ||
import { uiActionsPluginMock } from '../../../../ui_actions/public/mocks'; | ||
import { Action, ActionType, createAction } from '../../../../ui_actions/public'; | ||
|
||
const executeFn = jest.fn(); | ||
|
||
function createTestAction<C extends object>( | ||
type: string, | ||
checkCompatibility: (context: C) => boolean, | ||
autoExecutable = true | ||
): Action<object> { | ||
return createAction({ | ||
type: type as ActionType, | ||
id: type, | ||
isCompatible: (context: C) => Promise.resolve(checkCompatibility(context)), | ||
execute: (context) => { | ||
return executeFn(context); | ||
}, | ||
shouldAutoExecute: () => Promise.resolve(autoExecutable), | ||
}); | ||
} | ||
|
||
let uiActions: ReturnType<typeof uiActionsPluginMock.createPlugin>; | ||
|
||
describe('handleVisEvent', () => { | ||
beforeEach(() => { | ||
uiActions = uiActionsPluginMock.createPlugin(); | ||
|
||
executeFn.mockClear(); | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
test('should trigger the correct event', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'filter', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test1', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.filter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
data: { timeFieldName }, | ||
}) | ||
); | ||
}); | ||
|
||
test('should trigger the default trigger when not found', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'test', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test2', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.filter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
data: { timeFieldName }, | ||
}) | ||
); | ||
}); | ||
|
||
test('should have the correct context for `applyfilter`', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'applyFilter', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test3', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.applyFilter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
timeFieldName, | ||
}) | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ExpressionRendererEvent } from '../../../../expressions/public'; | ||
import { VIS_EVENT_TO_TRIGGER } from '../../../../visualizations/public'; | ||
import { UiActionsStart } from '../../../../ui_actions/public'; | ||
|
||
export const handleVisEvent = async ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question, do we need to add error handling here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do see error handling being added here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm i am mostly thinking if it's possible that |
||
event: ExpressionRendererEvent, | ||
uiActions: UiActionsStart, | ||
timeFieldName?: string | ||
) => { | ||
const triggerId = VIS_EVENT_TO_TRIGGER[event.name] ?? VIS_EVENT_TO_TRIGGER.filter; | ||
const isApplyFilter = triggerId === VIS_EVENT_TO_TRIGGER.applyFilter; | ||
const dataContext = { | ||
timeFieldName, | ||
...event.data, | ||
}; | ||
const context = isApplyFilter ? dataContext : { data: dataContext }; | ||
|
||
await uiActions.getTrigger(triggerId).exec(context); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.