Skip to content
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

[7.x] [Lens] Use rules of hooks with linting (#65593) #65688

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ module.exports = {
files: ['x-pack/plugins/lens/**/*.{js,ts,tsx}'],
rules: {
'react-hooks/exhaustive-deps': 'off',
'react-hooks/rules-of-hooks': 'off',
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export function LayerPanel(
}
) {
const dragDropContext = useContext(DragContext);
const [popoverState, setPopoverState] = useState<DimensionPopoverState>({
isOpen: false,
openId: null,
addingToGroupId: null,
});

const { framePublicAPI, layerId, activeVisualization, isOnlyLayer, onRemoveLayer } = props;
const datasourcePublicAPI = framePublicAPI.datasourceLayers[layerId];
if (!datasourcePublicAPI) {
Expand Down Expand Up @@ -74,12 +80,6 @@ export function LayerPanel(
dateRange: props.framePublicAPI.dateRange,
};

const [popoverState, setPopoverState] = useState<DimensionPopoverState>({
isOpen: false,
openId: null,
addingToGroupId: null,
});

const { groups } = activeVisualization.getConfiguration(layerVisualizationConfigProps);
const isEmptyLayer = !groups.some(d => d.accessors.length > 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ export function InnerWorkspacePanel({
framePublicAPI.filters,
]);

useEffect(() => {
// reset expression error if component attempts to run it again
if (expression && localState.expressionBuildError) {
setLocalState(s => ({
...s,
expressionBuildError: undefined,
}));
}
}, [expression]);

function onDrop() {
if (suggestionForDraggedField) {
trackUiEvent('drop_onto_workspace');
Expand Down Expand Up @@ -174,16 +184,6 @@ export function InnerWorkspacePanel({
}

function renderVisualization() {
useEffect(() => {
// reset expression error if component attempts to run it again
if (expression && localState.expressionBuildError) {
setLocalState(s => ({
...s,
expressionBuildError: undefined,
}));
}
}, [expression]);

if (expression === null) {
return renderEmptyWorkspace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,17 @@ describe('IndexPattern Data Panel', () => {

it('should render a warning if there are no index patterns', () => {
const wrapper = shallowWithIntl(
<InnerIndexPatternDataPanel {...defaultProps} currentIndexPatternId="" indexPatterns={{}} />
<IndexPatternDataPanel
{...defaultProps}
state={{
...initialState,
currentIndexPatternId: '',
indexPatterns: {},
}}
setState={jest.fn()}
dragDropContext={{ dragging: {}, setDragging: () => {} }}
changeIndexPattern={jest.fn()}
/>
);
expect(wrapper.find('[data-test-subj="indexPattern-no-indexpatterns"]')).toHaveLength(1);
});
Expand Down
87 changes: 43 additions & 44 deletions x-pack/plugins/lens/public/indexpattern_datasource/datapanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,49 @@ export function IndexPatternDataPanel({
indexPatternList.map(x => `${x.title}:${x.timeFieldName}`).join(','),
]}
/>
<MemoizedDataPanel
currentIndexPatternId={currentIndexPatternId}
indexPatternRefs={indexPatternRefs}
indexPatterns={indexPatterns}
query={query}
dateRange={dateRange}
filters={filters}
dragDropContext={dragDropContext}
showEmptyFields={state.showEmptyFields}
onToggleEmptyFields={onToggleEmptyFields}
core={core}
data={data}
onChangeIndexPattern={onChangeIndexPattern}
existingFields={state.existingFields}
/>

{Object.keys(indexPatterns).length === 0 ? (
<EuiFlexGroup
gutterSize="m"
className="lnsInnerIndexPatternDataPanel"
direction="column"
responsive={false}
>
<EuiFlexItem grow={null}>
<EuiCallOut
data-test-subj="indexPattern-no-indexpatterns"
title={i18n.translate('xpack.lens.indexPattern.noPatternsLabel', {
defaultMessage: 'No index patterns',
})}
color="warning"
iconType="alert"
>
<p>
<FormattedMessage
id="xpack.lens.indexPattern.noPatternsDescription"
defaultMessage="Please create an index pattern or switch to another data source"
/>
</p>
</EuiCallOut>
</EuiFlexItem>
</EuiFlexGroup>
) : (
<MemoizedDataPanel
currentIndexPatternId={currentIndexPatternId}
indexPatternRefs={indexPatternRefs}
indexPatterns={indexPatterns}
query={query}
dateRange={dateRange}
filters={filters}
dragDropContext={dragDropContext}
showEmptyFields={state.showEmptyFields}
onToggleEmptyFields={onToggleEmptyFields}
core={core}
data={data}
onChangeIndexPattern={onChangeIndexPattern}
existingFields={state.existingFields}
/>
)}
</>
);
}
Expand Down Expand Up @@ -194,35 +222,6 @@ export const InnerIndexPatternDataPanel = function InnerIndexPatternDataPanel({
onChangeIndexPattern: (newId: string) => void;
existingFields: IndexPatternPrivateState['existingFields'];
}) {
if (Object.keys(indexPatterns).length === 0) {
return (
<EuiFlexGroup
gutterSize="m"
className="lnsInnerIndexPatternDataPanel"
direction="column"
responsive={false}
>
<EuiFlexItem grow={null}>
<EuiCallOut
data-test-subj="indexPattern-no-indexpatterns"
title={i18n.translate('xpack.lens.indexPattern.noPatternsLabel', {
defaultMessage: 'No index patterns',
})}
color="warning"
iconType="alert"
>
<p>
<FormattedMessage
id="xpack.lens.indexPattern.noPatternsDescription"
defaultMessage="Please create an index pattern or switch to another data source"
/>
</p>
</EuiCallOut>
</EuiFlexItem>
</EuiFlexGroup>
);
}

const [localState, setLocalState] = useState<DataPanelState>({
nameFilter: '',
typeFilter: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function BucketNestingEditor({
defaultMessage: 'Entire data set',
}),
},
...aggColumns,
...aggColumns.map(({ value, text }) => ({ value, text })),
]}
value={prevColumn}
onChange={e => setColumns(nestColumn(layer.columnOrder, e.target.value, columnId))}
Expand Down
31 changes: 15 additions & 16 deletions x-pack/plugins/lens/public/indexpattern_datasource/field_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,6 @@ function FieldItemPopoverContents(props: State & FieldItemProps) {

const IS_DARK_THEME = core.uiSettings.get('theme:darkMode');
const chartTheme = IS_DARK_THEME ? EUI_CHARTS_THEME_DARK.theme : EUI_CHARTS_THEME_LIGHT.theme;

if (props.isLoading) {
return <EuiLoadingSpinner />;
} else if (
(!props.histogram || props.histogram.buckets.length === 0) &&
(!props.topValues || props.topValues.buckets.length === 0)
) {
return (
<EuiText size="s">
{i18n.translate('xpack.lens.indexPattern.fieldStatsNoData', {
defaultMessage: 'No data to display.',
})}
</EuiText>
);
}

let histogramDefault = !!props.histogram;

const totalValuesCount =
Expand Down Expand Up @@ -309,6 +293,21 @@ function FieldItemPopoverContents(props: State & FieldItemProps) {

let title = <></>;

if (props.isLoading) {
return <EuiLoadingSpinner />;
} else if (
(!props.histogram || props.histogram.buckets.length === 0) &&
(!props.topValues || props.topValues.buckets.length === 0)
) {
return (
<EuiText size="s">
{i18n.translate('xpack.lens.indexPattern.fieldStatsNoData', {
defaultMessage: 'No data to display.',
})}
</EuiText>
);
}

if (histogram && histogram.buckets.length && topValues && topValues.buckets.length) {
title = (
<EuiButtonGroup
Expand Down