18128: Improves process panel.
[arvados-workbench2.git] / src / components / multi-panel-view / multi-panel-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { ReactElement, ReactNode, useState } from 'react';
6 import { Button, Grid, StyleRulesCallback, Tooltip, withStyles, WithStyles } from "@material-ui/core";
7 import { GridProps } from '@material-ui/core/Grid';
8 import { isArray } from 'lodash';
9 import { DefaultView } from 'components/default-view/default-view';
10 import { InfoIcon, InvisibleIcon, VisibleIcon } from 'components/icon/icon';
11 import { ReactNodeArray } from 'prop-types';
12 import classNames from 'classnames';
13
14 type CssRules = 'button' | 'buttonIcon';
15
16 const styles: StyleRulesCallback<CssRules> = theme => ({
17     button: {
18         padding: '2px 5px',
19         marginRight: '5px',
20     },
21     buttonIcon: {
22         boxShadow: 'none',
23         padding: '2px 0px 2px 5px',
24         fontSize: '1rem'
25     },
26 });
27
28 interface MPVHideablePanelDataProps {
29     name: string;
30     visible: boolean;
31     maximized: boolean;
32     children: ReactNode;
33 }
34
35 interface MPVHideablePanelActionProps {
36     doHidePanel: () => void;
37     doMaximizePanel: () => void;
38 }
39
40 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
41
42 const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, ...props}: MPVHideablePanelProps) =>
43     visible
44     ? <>
45         {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel,panelName: name, panelMaximized: maximized })}
46     </>
47     : null;
48
49 interface MPVPanelDataProps {
50     panelName?: string;
51     panelMaximized?: boolean;
52 }
53
54 interface MPVPanelActionProps {
55     doHidePanel?: () => void;
56     doMaximizePanel?: () => void;
57 }
58
59 // Props received by panel implementors
60 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
61
62 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
63
64 // Grid item compatible component for layout and MPV props passing
65 export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName, panelMaximized, ...props}: MPVPanelContentProps) =>
66     <Grid item {...props}>
67         {React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })}
68     </Grid>;
69
70 export interface MPVPanelState {
71     name: string;
72     visible?: boolean;
73 }
74 interface MPVContainerDataProps {
75     panelStates?: MPVPanelState[];
76 }
77 type MPVContainerProps = MPVContainerDataProps & GridProps;
78
79 // Grid container compatible component that also handles panel toggling.
80 const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVContainerProps & WithStyles<CssRules>) => {
81     if (children === undefined || children === null || children === {}) {
82         children = [];
83     } else if (!isArray(children)) {
84         children = [children];
85     }
86     const visibility = (children as ReactNodeArray).map((_, idx) =>
87         !!!panelStates || // if panelStates wasn't passed, default to all visible panels
88             (panelStates[idx] &&
89                 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
90     const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
91
92     let panels: JSX.Element[] = [];
93     let toggles: JSX.Element[] = [];
94
95     if (isArray(children)) {
96         for (let idx = 0; idx < children.length; idx++) {
97             const toggleFn = (idx: number) => () => {
98                 setPanelVisibility([
99                     ...panelVisibility.slice(0, idx),
100                     !panelVisibility[idx],
101                     ...panelVisibility.slice(idx+1)
102                 ])
103             };
104             const maximizeFn = (idx: number) => () => {
105                 // Maximize X == hide all but X
106                 setPanelVisibility([
107                     ...panelVisibility.slice(0, idx).map(() => false),
108                     true,
109                     ...panelVisibility.slice(idx+1).map(() => false),
110                 ])
111             };
112             const toggleIcon = panelVisibility[idx]
113                 ? <VisibleIcon className={classNames(classes.buttonIcon)} />
114                 : <InvisibleIcon className={classNames(classes.buttonIcon)}/>
115             const panelName = panelStates === undefined
116                 ? `Panel ${idx+1}`
117                 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
118             const toggleVariant = panelVisibility[idx]
119                 ? "raised"
120                 : "flat";
121             const toggleTooltip = panelVisibility[idx]
122                 ? `Hide ${panelName} panel`
123                 : `Show ${panelName} panel`;
124             const panelIsMaximized = panelVisibility[idx] &&
125                 panelVisibility.filter(e => e).length === 1;
126
127             toggles = [
128                 ...toggles,
129                 <Tooltip title={toggleTooltip} disableFocusListener>
130                     <Button variant={toggleVariant} size="small" color="primary"
131                         className={classNames(classes.button)}
132                         onClick={toggleFn(idx)}>
133                             {panelName}
134                             {toggleIcon}
135                     </Button>
136                 </Tooltip>
137             ];
138
139             const aPanel =
140                 <MPVHideablePanel visible={panelVisibility[idx]} name={panelName}
141                     maximized={panelIsMaximized}
142                     doHidePanel={toggleFn(idx)} doMaximizePanel={maximizeFn(idx)}>
143                     {children[idx]}
144                 </MPVHideablePanel>;
145             panels = [...panels, aPanel];
146         };
147     };
148
149     return <Grid container {...props}>
150         <Grid container item direction="row">
151             { toggles.map(tgl => <Grid item>{tgl}</Grid>) }
152         </Grid>
153         { panelVisibility.includes(true)
154             ? panels
155             : <Grid container item alignItems='center' justify='center'>
156                 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
157             </Grid> }
158     </Grid>;
159 };
160
161 export const MPVContainer = withStyles(styles)(MPVContainerComponent);