-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Lens] Add "no data" popover #69147
[Lens] Add "no data" popover #69147
Changes from all commits
da0a99d
c8ed80a
eb163b1
16ccebc
6e15498
3bb8044
7845804
23a3615
cacdee4
a28b3a2
024d1ac
ef9d5f1
b0d7dd7
336e555
b9fb0e0
fc24f3b
102ef4c
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mountWithIntl as mount } from 'test_utils/enzyme_helpers'; | ||
import { NoDataPopover } from './no_data_popover'; | ||
import { EuiTourStep } from '@elastic/eui'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
describe('NoDataPopover', () => { | ||
const createMockStorage = () => ({ | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
remove: jest.fn(), | ||
clear: jest.fn(), | ||
}); | ||
|
||
it('should hide popover if showNoDataPopover is set to false', () => { | ||
const Child = () => <span />; | ||
const instance = mount( | ||
<NoDataPopover storage={createMockStorage()} showNoDataPopover={false}> | ||
<Child /> | ||
</NoDataPopover> | ||
); | ||
expect(instance.find(EuiTourStep).prop('isStepOpen')).toBe(false); | ||
expect(instance.find(EuiTourStep).find(Child)).toHaveLength(1); | ||
}); | ||
|
||
it('should hide popover if showNoDataPopover is set to true, but local storage flag is set', () => { | ||
const child = <span />; | ||
const storage = createMockStorage(); | ||
storage.get.mockReturnValue(true); | ||
const instance = mount( | ||
<NoDataPopover storage={storage} showNoDataPopover={false}> | ||
{child} | ||
</NoDataPopover> | ||
); | ||
expect(instance.find(EuiTourStep).prop('isStepOpen')).toBe(false); | ||
}); | ||
|
||
it('should render popover if showNoDataPopover is set to true and local storage flag is not set', () => { | ||
const child = <span />; | ||
const instance = mount( | ||
<NoDataPopover storage={createMockStorage()} showNoDataPopover={true}> | ||
{child} | ||
</NoDataPopover> | ||
); | ||
expect(instance.find(EuiTourStep).prop('isStepOpen')).toBe(true); | ||
}); | ||
|
||
it('should hide popover if it is closed', async () => { | ||
const props = { | ||
children: <span />, | ||
showNoDataPopover: true, | ||
storage: createMockStorage(), | ||
}; | ||
const instance = mount(<NoDataPopover {...props} />); | ||
act(() => { | ||
instance.find(EuiTourStep).prop('closePopover')!(); | ||
}); | ||
instance.setProps({ ...props }); | ||
expect(instance.find(EuiTourStep).prop('isStepOpen')).toBe(false); | ||
}); | ||
|
||
it('should set local storage flag and hide on closing with button', () => { | ||
const props = { | ||
children: <span />, | ||
showNoDataPopover: true, | ||
storage: createMockStorage(), | ||
}; | ||
const instance = mount(<NoDataPopover {...props} />); | ||
act(() => { | ||
instance.find(EuiTourStep).prop('footerAction')!.props.onClick(); | ||
}); | ||
instance.setProps({ ...props }); | ||
expect(props.storage.set).toHaveBeenCalledWith(expect.any(String), true); | ||
expect(instance.find(EuiTourStep).prop('isStepOpen')).toBe(false); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { ReactElement, useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { EuiButtonEmpty, EuiText, EuiTourStep } from '@elastic/eui'; | ||
import { IStorageWrapper } from 'src/plugins/kibana_utils/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
const NO_DATA_POPOVER_STORAGE_KEY = 'data.noDataPopover'; | ||
|
||
export function NoDataPopover({ | ||
showNoDataPopover, | ||
storage, | ||
children, | ||
}: { | ||
showNoDataPopover?: boolean; | ||
storage: IStorageWrapper; | ||
children: ReactElement; | ||
}) { | ||
const [noDataPopoverDismissed, setNoDataPopoverDismissed] = useState(() => | ||
Boolean(storage.get(NO_DATA_POPOVER_STORAGE_KEY)) | ||
); | ||
const [noDataPopoverVisible, setNoDataPopoverVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
if (showNoDataPopover && !noDataPopoverDismissed) { | ||
setNoDataPopoverVisible(true); | ||
} | ||
}, [noDataPopoverDismissed, showNoDataPopover]); | ||
|
||
return ( | ||
<EuiTourStep | ||
onFinish={() => {}} | ||
closePopover={() => { | ||
setNoDataPopoverVisible(false); | ||
}} | ||
content={ | ||
<EuiText size="s"> | ||
<p style={{ maxWidth: 300 }}> | ||
{i18n.translate('data.noDataPopover.content', { | ||
defaultMessage: | ||
"This time range doesn't contain any data. Increase or adjust the time range to see more fields and create charts", | ||
})} | ||
</p> | ||
</EuiText> | ||
} | ||
minWidth={300} | ||
anchorPosition="downCenter" | ||
step={1} | ||
stepsTotal={1} | ||
isStepOpen={noDataPopoverVisible} | ||
subtitle={i18n.translate('data.noDataPopover.title', { defaultMessage: 'Tip' })} | ||
title="" | ||
footerAction={ | ||
<EuiButtonEmpty | ||
size="s" | ||
data-test-subj="noDataPopoverDismissButton" | ||
onClick={() => { | ||
storage.set(NO_DATA_POPOVER_STORAGE_KEY, true); | ||
setNoDataPopoverDismissed(true); | ||
setNoDataPopoverVisible(false); | ||
}} | ||
> | ||
{i18n.translate('data.noDataPopover.dismissAction', { | ||
defaultMessage: "Don't show again", | ||
})} | ||
</EuiButtonEmpty> | ||
} | ||
> | ||
<div | ||
onFocus={() => { | ||
setNoDataPopoverVisible(false); | ||
}} | ||
> | ||
{children} | ||
</div> | ||
</EuiTourStep> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ import { | |
} from '../../../../../src/plugins/data/public'; | ||
|
||
interface State { | ||
indicateNoData: boolean; | ||
isLoading: boolean; | ||
isSaveModalVisible: boolean; | ||
indexPatternsForTopNav: IndexPatternInstance[]; | ||
|
@@ -97,9 +98,27 @@ export function App({ | |
toDate: currentRange.to, | ||
}, | ||
filters: [], | ||
indicateNoData: false, | ||
}; | ||
}); | ||
|
||
const showNoDataPopover = useCallback(() => { | ||
setState((prevState) => ({ ...prevState, indicateNoData: true })); | ||
}, [setState]); | ||
|
||
useEffect(() => { | ||
if (state.indicateNoData) { | ||
setState((prevState) => ({ ...prevState, indicateNoData: false })); | ||
} | ||
}, [ | ||
setState, | ||
state.indicateNoData, | ||
state.query, | ||
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. Why did you have to list attributes that are not used explicitly in the effect? Is it because of prevState? 🤔 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. We want to show this popover only on first load - if the user starts to change the configuration, they most likely now how to get data (e.g. by changing the index pattern because the wrong one was pre-selected). Then we don't need the popover anymore. This is just to be extra safe (as the popover is also hidden when the user interacts with the UI outside of it) 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. Might be nice adding a comment on this as this is not trivial behavior |
||
state.filters, | ||
state.dateRange, | ||
state.indexPatternsForTopNav, | ||
]); | ||
|
||
const { lastKnownDoc } = state; | ||
|
||
const isSaveable = | ||
|
@@ -458,6 +477,7 @@ export function App({ | |
query={state.query} | ||
dateRangeFrom={state.dateRange.fromDate} | ||
dateRangeTo={state.dateRange.toDate} | ||
indicateNoData={state.indicateNoData} | ||
/> | ||
</div> | ||
|
||
|
@@ -472,6 +492,7 @@ export function App({ | |
savedQuery: state.savedQuery, | ||
doc: state.persistedDoc, | ||
onError, | ||
showNoDataPopover, | ||
onChange: ({ filterableIndexPatterns, doc }) => { | ||
if (!_.isEqual(state.persistedDoc, doc)) { | ||
setState((s) => ({ ...s, lastKnownDoc: doc })); | ||
|
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.
@flash1293 Minor nit, can you change/add these props: