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

Exposing internal state via Kedro Viz React component props #1969

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 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: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Display published URLs. (#1907)
- Conditionally move session store and stats file to .viz directory. (#1915)
- Refactor namespace pipelines. (#1897)
- Expose internal Redux state via Kedro-Viz React component props `options`. (#1969)

Check warning on line 17 in RELEASE.md

View workflow job for this annotation

GitHub Actions / vale

[vale] RELEASE.md#L17

[Kedro-viz.words] Use 'with' or 'through' instead of 'via'.
Raw output
{"message": "[Kedro-viz.words] Use 'with' or 'through' instead of 'via'.", "location": {"path": "RELEASE.md", "range": {"start": {"line": 17, "column": 31}}}, "severity": "WARNING"}
jitu5 marked this conversation as resolved.
Show resolved Hide resolved

## Bug fixes and other changes

Expand Down
14 changes: 14 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,17 @@ export function updateRunNotes(notes, runId) {
runId,
};
}

export const UPDATE_STATE_FROM_OPTIONS_PROPS =
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
'UPDATE_STATE_FROM_OPTIONS_PROPS';

/**
* Update state with latest options props coming from the react component
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} updatedProps
*/
export const updateStateFromOptionsProps = (updatedProps) => {
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
return {
type: UPDATE_STATE_FROM_OPTIONS_PROPS,
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
payload: updatedProps,
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
};
};
69 changes: 45 additions & 24 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { Provider } from 'react-redux';
import 'what-input';
import configureStore from '../../store';
import { resetData } from '../../actions';
import { resetData, updateStateFromOptionsProps } from '../../actions';
import { loadInitialPipelineData } from '../../actions/pipelines';
import Wrapper from '../wrapper';
import getInitialState, {
Expand Down Expand Up @@ -38,6 +38,9 @@ class App extends React.Component {
if (prevProps.data !== this.props.data) {
ravi-kumar-pilla marked this conversation as resolved.
Show resolved Hide resolved
this.updatePipelineData();
}
if (prevProps.options !== this.props.options) {
ravi-kumar-pilla marked this conversation as resolved.
Show resolved Hide resolved
this.store.dispatch(updateStateFromOptionsProps(this.props.options));
}
}

/**
Expand Down Expand Up @@ -85,29 +88,47 @@ App.propTypes = {
tags: PropTypes.array,
}),
]),
/**
* Specify the theme: Either 'light' or 'dark'.
* If set, this will override the localStorage value.
*/
theme: PropTypes.oneOf(['dark', 'light']),
/**
* Override visibility of various features, e.g. icon buttons
*/
visible: PropTypes.shape({
labelBtn: PropTypes.bool,
layerBtn: PropTypes.bool,
exportBtn: PropTypes.bool,
pipelineBtn: PropTypes.bool,
sidebar: PropTypes.bool,
}),
/**
* Determines if certain elements are displayed, e.g global tool bar, sidebar
*/
display: PropTypes.shape({
globalToolbar: PropTypes.bool,
sidebar: PropTypes.bool,
miniMap: PropTypes.bool,
expandAllPipelines: PropTypes.bool,
options: PropTypes.shape({
/**
* Specify the theme: Either 'light' or 'dark'.
* If set, this will override the localStorage value.
*/
theme: PropTypes.oneOf(['dark', 'light']),
/**
* Override visibility of various features, e.g. icon buttons
*/
visible: PropTypes.shape({
labelBtn: PropTypes.bool,
layerBtn: PropTypes.bool,
exportBtn: PropTypes.bool,
pipelineBtn: PropTypes.bool,
sidebar: PropTypes.bool,
}),
/**
* Determines if certain elements are displayed, e.g global tool bar, sidebar
*/
display: PropTypes.shape({
globalToolbar: PropTypes.bool,
sidebar: PropTypes.bool,
miniMap: PropTypes.bool,
expandAllPipelines: PropTypes.bool,
}),
/**
* Override the default enabled/disabled tags
*/
tag: PropTypes.shape({
enabled: PropTypes.objectOf(PropTypes.bool),
}),
/**
* Override the default enabled/disabled node types
*/
nodeType: PropTypes.shape({
disabled: PropTypes.shape({
parameters: PropTypes.bool,
task: PropTypes.bool,
data: PropTypes.bool,
}),
}),
}),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { useGeneratePathname } from '../../utils/hooks/use-generate-pathname';
*/
export const FlowchartPrimaryToolbar = ({
disableLayerBtn,
displaySidebar,
onToggleExportModal,
onToggleLayers,
onToggleSidebar,
Expand All @@ -46,11 +45,7 @@ export const FlowchartPrimaryToolbar = ({

return (
<>
<PrimaryToolbar
displaySidebar={displaySidebar}
onToggleSidebar={onToggleSidebar}
visible={visible}
>
<PrimaryToolbar onToggleSidebar={onToggleSidebar} visible={visible}>
<IconButton
active={textLabels}
ariaLabel={`${textLabels ? 'Hide' : 'Show'} text labels`}
Expand Down Expand Up @@ -106,7 +101,6 @@ export const FlowchartPrimaryToolbar = ({

export const mapStateToProps = (state) => ({
disableLayerBtn: !state.layer.ids.length,
displaySidebar: state.display.sidebar,
textLabels: state.textLabels,
visible: state.visible,
visibleLayers: Boolean(getVisibleLayerIDs(state).length),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('PrimaryToolbar', () => {
pipelineBtn: false,
};
const wrapper = setup.mount(<ConnectedFlowchartPrimaryToolbar />, {
visible,
options: { visible },
});
expect(wrapper.find('.pipeline-icon-toolbar__button').length).toBe(1);
});
Expand All @@ -36,7 +36,7 @@ describe('PrimaryToolbar', () => {
labelBtn: false,
};
const wrapper = setup.mount(<ConnectedFlowchartPrimaryToolbar />, {
visible,
options: { visible },
});
expect(wrapper.find('.pipeline-icon-toolbar__button').length).toBe(4);
});
Expand Down Expand Up @@ -71,7 +71,6 @@ describe('PrimaryToolbar', () => {
disableLayerBtn: expect.any(Boolean),
textLabels: expect.any(Boolean),
expandedPipelines: expect.any(Boolean),
displaySidebar: true,
visible: expect.objectContaining({
exportBtn: expect.any(Boolean),
exportModal: expect.any(Boolean),
Expand Down
11 changes: 7 additions & 4 deletions src/components/flowchart-wrapper/flowchart-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import './flowchart-wrapper.scss';
*/
export const FlowChartWrapper = ({
fullNodeNames,
displaySidebar,
graph,
loading,
metadataVisible,
Expand Down Expand Up @@ -308,7 +309,7 @@ export const FlowChartWrapper = ({
if (isInvalidUrl) {
return (
<div className="kedro-pipeline">
<Sidebar />
{displaySidebar && <Sidebar />}
<MetaData />
<PipelineWarning
errorMessage={errorMessage}
Expand All @@ -320,7 +321,7 @@ export const FlowChartWrapper = ({
} else {
return (
<div className="kedro-pipeline">
<Sidebar />
{displaySidebar && <Sidebar />}
<MetaData />
<div className="pipeline-wrapper">
<PipelineWarning />
Expand All @@ -330,7 +331,7 @@ export const FlowChartWrapper = ({
'pipeline-wrapper__go-back-btn--show':
goBackToExperimentTracking?.showGoBackBtn,
'pipeline-wrapper__go-back-btn--show-sidebar-visible':
sidebarVisible,
sidebarVisible || displaySidebar,
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
'pipeline-wrapper__go-back-btn--show-metadata-visible':
metadataVisible,
})}
Expand All @@ -342,7 +343,8 @@ export const FlowChartWrapper = ({
</div>
<div
className={classnames('pipeline-wrapper__loading', {
'pipeline-wrapper__loading--sidebar-visible': sidebarVisible,
'pipeline-wrapper__loading--sidebar-visible':
sidebarVisible || displaySidebar,
})}
>
<LoadingIcon visible={loading} />
Expand All @@ -358,6 +360,7 @@ export const FlowChartWrapper = ({

export const mapStateToProps = (state) => ({
fullNodeNames: getNodeFullName(state),
displaySidebar: state.display.sidebar,
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
graph: state.graph,
loading: isLoading(state),
metadataVisible: getVisibleMetaSidebar(state),
Expand Down
11 changes: 9 additions & 2 deletions src/components/flowchart/flowchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,13 @@ export class FlowChart extends Component {
* Render React elements
*/
render() {
const { chartSize, layers, visibleGraph, displayGlobalToolbar } =
this.props;
const {
chartSize,
layers,
visibleGraph,
displayGlobalToolbar,
displaySidebar,
} = this.props;
const { outerWidth = 0, outerHeight = 0 } = chartSize;

return (
Expand Down Expand Up @@ -657,6 +662,7 @@ export class FlowChart extends Component {
'pipeline-flowchart__layer-names--visible': layers.length,
'pipeline-flowchart__layer-names--no-global-toolbar':
!displayGlobalToolbar,
'pipeline-flowchart__layer-names--no-sidebar': !displaySidebar,
})}
ref={this.layerNamesRef}
/>
Expand Down Expand Up @@ -690,6 +696,7 @@ export const mapStateToProps = (state, ownProps) => ({
chartSize: getChartSize(state),
chartZoom: getChartZoom(state),
displayGlobalToolbar: state.display.globalToolbar,
displaySidebar: state.display.sidebar,
edges: state.graph.edges || emptyEdges,
focusMode: state.visible.modularPipelineFocusMode,
graphSize: state.graph.size || emptyGraphSize,
Expand Down
1 change: 1 addition & 0 deletions src/components/flowchart/flowchart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ describe('FlowChart', () => {
inputOutputDataEdges: expect.any(Object),
focusMode: expect.any(Object),
displayGlobalToolbar: expect.any(Boolean),
displaySidebar: expect.any(Boolean),
};
expect(mapStateToProps(mockState.spaceflights)).toEqual(expectedResult);
});
Expand Down
4 changes: 4 additions & 0 deletions src/components/flowchart/styles/_layers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
left: -#{variables.$global-toolbar-width};
}

&--no-sidebar {
left: 0;
}

@media print {
display: none;
}
Expand Down
22 changes: 20 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import node from './nodes';
import nodeType from './node-type';
import pipeline from './pipeline';
import tag from './tags';
import merge from 'lodash/merge';
import modularPipeline from './modular-pipelines';
import visible from './visible';
import {
Expand All @@ -21,6 +22,7 @@ import {
UPDATE_CHART_SIZE,
UPDATE_ZOOM,
TOGGLE_EXPAND_ALL_PIPELINES,
UPDATE_STATE_FROM_OPTIONS_PROPS,
} from '../actions';
import { TOGGLE_PARAMETERS_HOVERED } from '../actions';

Expand Down Expand Up @@ -53,6 +55,19 @@ function resetDataReducer(state = {}, action) {
return state;
}

/**
* Update state from options props coming form react component
* @param {Object} state Complete app state
* @param {Object} action Redux action
* @return {Object} Updated state
*/
function updateStateFromPropsReducer(state = {}, action) {
jitu5 marked this conversation as resolved.
Show resolved Hide resolved
if (action.type === UPDATE_STATE_FROM_OPTIONS_PROPS) {
return merge({}, state, action.payload);
Huongg marked this conversation as resolved.
Show resolved Hide resolved
}
return state;
}

const combinedReducer = combineReducers({
// These props have their own reducers in other files
flags,
Expand Down Expand Up @@ -103,7 +118,10 @@ const combinedReducer = combineReducers({
),
});

const rootReducer = (state, action) =>
combinedReducer(resetDataReducer(state, action), action);
const rootReducer = (state, action) => {
let newState = resetDataReducer(state, action);
newState = updateStateFromPropsReducer(newState, action);
return combinedReducer(newState, action);
};

export default rootReducer;
29 changes: 26 additions & 3 deletions src/selectors/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {

const getSizeWarningFlag = (state) => state.flags.sizewarning;
const getVisibleSidebar = (state) => state.visible.sidebar;
const getDisplayGlobalToolbar = (state) => state.display.globalToolbar;
ravi-kumar-pilla marked this conversation as resolved.
Show resolved Hide resolved
const getDisplaySidebar = (state) => state.display.sidebar;
const getVisibleCode = (state) => state.visible.code;
const getIgnoreLargeWarning = (state) => state.ignoreLargeWarning;
const getGraphHasNodes = (state) => Boolean(state.graph?.nodes?.length);
Expand Down Expand Up @@ -70,15 +72,36 @@ export const getSidebarWidth = (visible, { open, closed }) =>
* and add some useful new ones
*/
export const getChartSize = createSelector(
[getVisibleSidebar, getVisibleMetaSidebar, getVisibleCode, getChartSizeState],
(visibleSidebar, visibleMetaSidebar, visibleCodeSidebar, chartSize) => {
[
getVisibleSidebar,
getVisibleMetaSidebar,
getVisibleCode,
getChartSizeState,
getDisplaySidebar,
getDisplayGlobalToolbar,
],
(
visibleSidebar,
visibleMetaSidebar,
visibleCodeSidebar,
chartSize,
displaySidebar,
displayGlobalToolbar
) => {
const { left, top, width, height } = chartSize;
if (!width || !height) {
return {};
}

// Determine if the sidebar is visible and open
const shouldDisplaySidebar = displaySidebar && visibleSidebar;
jitu5 marked this conversation as resolved.
Show resolved Hide resolved

// Get the actual sidebar width
const sidebarWidthActual = getSidebarWidth(visibleSidebar, sidebarWidth);
const sidebarWidthActual =
displaySidebar || displayGlobalToolbar
? getSidebarWidth(shouldDisplaySidebar, sidebarWidth)
: 0;

const metaSidebarWidthActual = getSidebarWidth(
visibleMetaSidebar,
metaSidebarWidth
Expand Down
Loading
Loading