Skip to content

Commit

Permalink
[Canvas] SidebarContent refactor. (elastic#110051)
Browse files Browse the repository at this point in the history
* Refactored sidebar.

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Kuznietsov and kibanamachine authored Sep 8, 2021
1 parent 8956cad commit 6a958a5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ElementSettings as Component } from './element_settings.component';
import { State, PositionedElement } from '../../../../types';

interface Props {
selectedElementId: string;
selectedElementId: string | null;
}

const mapStateToProps = (state: State, { selectedElementId }: Props): StateProps => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import React, { FunctionComponent } from 'react';
// @ts-expect-error unconverted component
import { SidebarContent } from './sidebar_content';

interface Props {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export { SidebarContent } from './sidebar_content';
export { SidebarContent as SidebarContentComponent } from './sidebar_content.component';
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
*/

import React, { Fragment } from 'react';
import { connect } from 'react-redux';
import { compose, branch, renderComponent } from 'recompose';
import { EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
// @ts-expect-error unconverted component
import { SidebarHeader } from '../../sidebar_header';
import { MultiElementSettings } from '../multi_element_settings';
import { GroupSettings } from '../group_settings';
import { GlobalConfig } from '../global_config';
import { ElementSettings } from '../element_settings';

import { getSelectedToplevelNodes, getSelectedElementId } from '../../state/selectors/workpad';
import { SidebarHeader } from '../sidebar_header';
import { MultiElementSettings } from './multi_element_settings';
import { GroupSettings } from './group_settings';
import { GlobalConfig } from './global_config';
import { ElementSettings } from './element_settings';
interface SidebarContentProps {
commit?: Function;
selectedElementId: string | null;
selectedToplevelNodes: string[];
}

const strings = {
getGroupedElementSidebarTitle: () =>
Expand All @@ -43,51 +46,46 @@ const strings = {
}),
};

const mapStateToProps = (state) => ({
selectedToplevelNodes: getSelectedToplevelNodes(state),
selectedElementId: getSelectedElementId(state),
});

const MultiElementSidebar = () => (
const MultiElementSidebar: React.FC = () => (
<Fragment>
<SidebarHeader title={strings.getMultiElementSidebarTitle()} />
<EuiSpacer />
<MultiElementSettings />
</Fragment>
);

const GroupedElementSidebar = () => (
const GroupedElementSidebar: React.FC = () => (
<Fragment>
<SidebarHeader title={strings.getGroupedElementSidebarTitle()} groupIsSelected />
<EuiSpacer />
<GroupSettings />
</Fragment>
);

const SingleElementSidebar = ({ selectedElementId }) => (
const SingleElementSidebar: React.FC<{ selectedElementId: string | null }> = ({
selectedElementId,
}) => (
<Fragment>
<SidebarHeader title={strings.getSingleElementSidebarTitle()} showLayerControls />
<ElementSettings selectedElementId={selectedElementId} />
</Fragment>
);

const branches = [
// multiple elements are selected
branch(
({ selectedToplevelNodes }) => selectedToplevelNodes.length > 1,
renderComponent(MultiElementSidebar)
),
// a single, grouped element is selected
branch(
({ selectedToplevelNodes }) =>
selectedToplevelNodes.length === 1 && selectedToplevelNodes[0].includes('group'),
renderComponent(GroupedElementSidebar)
),
// a single element is selected
branch(
({ selectedToplevelNodes }) => selectedToplevelNodes.length === 1,
renderComponent(SingleElementSidebar)
),
];
export const SidebarContent: React.FC<SidebarContentProps> = ({
selectedToplevelNodes,
selectedElementId,
}) => {
if (selectedToplevelNodes.length > 1) {
return <MultiElementSidebar />;
}

if (selectedToplevelNodes.length === 1 && selectedToplevelNodes[0].includes('group')) {
return <GroupedElementSidebar />;
}

export const SidebarContent = compose(connect(mapStateToProps), ...branches)(GlobalConfig);
if (selectedToplevelNodes.length === 1) {
return <SingleElementSidebar selectedElementId={selectedElementId} />;
}

return <GlobalConfig />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 from 'react';
import { useSelector } from 'react-redux';
import { getSelectedToplevelNodes, getSelectedElementId } from '../../../state/selectors/workpad';
import { State } from '../../../../types';
import { SidebarContent as Component } from './sidebar_content.component';

interface SidebarContentProps {
commit?: Function;
}

export const SidebarContent: React.FC<SidebarContentProps> = ({ commit }) => {
const selectedToplevelNodes = useSelector<State, string[]>((state) =>
getSelectedToplevelNodes(state)
);

const selectedElementId = useSelector<State, string | null>((state) =>
getSelectedElementId(state)
);

return (
<Component
commit={commit}
selectedToplevelNodes={selectedToplevelNodes}
selectedElementId={selectedElementId}
/>
);
};

0 comments on commit 6a958a5

Please sign in to comment.