19300: Adds un-maximize button to every maximizable panel.
[arvados-workbench2.git] / src / views / process-panel / process-log-card.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { useState } from 'react';
6 import {
7     StyleRulesCallback,
8     WithStyles,
9     withStyles,
10     Card,
11     CardHeader,
12     IconButton,
13     CardContent,
14     Tooltip,
15     Grid,
16     Typography,
17 } from '@material-ui/core';
18 import { ArvadosTheme } from 'common/custom-theme';
19 import {
20     CloseIcon,
21     CollectionIcon,
22     CopyIcon,
23     LogIcon,
24     MaximizeIcon,
25     UnMaximizeIcon,
26     TextDecreaseIcon,
27     TextIncreaseIcon,
28     WordWrapOffIcon,
29     WordWrapOnIcon,
30 } from 'components/icon/icon';
31 import { Process } from 'store/processes/process';
32 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
33 import {
34     FilterOption,
35     ProcessLogForm
36 } from 'views/process-panel/process-log-form';
37 import { ProcessLogCodeSnippet } from 'views/process-panel/process-log-code-snippet';
38 import { DefaultView } from 'components/default-view/default-view';
39 import { CodeSnippetDataProps } from 'components/code-snippet/code-snippet';
40 import CopyToClipboard from 'react-copy-to-clipboard';
41
42 type CssRules = 'card' | 'content' | 'title' | 'iconHeader' | 'header' | 'root' | 'logViewer' | 'logViewerContainer';
43
44 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
45     card: {
46         height: '100%'
47     },
48     header: {
49         paddingTop: theme.spacing.unit,
50         paddingBottom: theme.spacing.unit,
51     },
52     content: {
53         padding: theme.spacing.unit * 0,
54         height: '100%',
55     },
56     logViewer: {
57         height: '100%',
58     },
59     logViewerContainer: {
60         height: '100%',
61     },
62     title: {
63         overflow: 'hidden',
64         paddingTop: theme.spacing.unit * 0.5
65     },
66     iconHeader: {
67         fontSize: '1.875rem',
68         color: theme.customs.colors.green700
69     },
70     root: {
71         height: '100%',
72     },
73 });
74
75 export interface ProcessLogsCardDataProps {
76     process: Process;
77     selectedFilter: FilterOption;
78     filters: FilterOption[];
79 }
80
81 export interface ProcessLogsCardActionProps {
82     onLogFilterChange: (filter: FilterOption) => void;
83     navigateToLog: (uuid: string) => void;
84     onCopy: (text: string) => void;
85 }
86
87 type ProcessLogsCardProps = ProcessLogsCardDataProps
88     & ProcessLogsCardActionProps
89     & CodeSnippetDataProps
90     & WithStyles<CssRules>
91     & MPVPanelProps;
92
93 export const ProcessLogsCard = withStyles(styles)(
94     ({ classes, process, filters, selectedFilter, lines,
95         onLogFilterChange, navigateToLog, onCopy,
96         doHidePanel, doMaximizePanel, doUnMaximizePanel, panelMaximized, panelName }: ProcessLogsCardProps) => {
97         const [wordWrap, setWordWrap] = useState<boolean>(true);
98         const [fontSize, setFontSize] = useState<number>(3);
99         const fontBaseSize = 10;
100         const fontStepSize = 1;
101
102         return <Grid item className={classes.root} xs={12}>
103             <Card className={classes.card}>
104                 <CardHeader className={classes.header}
105                     avatar={<LogIcon className={classes.iconHeader} />}
106                     action={<Grid container direction='row' alignItems='center'>
107                         <Grid item>
108                             <ProcessLogForm selectedFilter={selectedFilter}
109                                 filters={filters} onChange={onLogFilterChange} />
110                         </Grid>
111                         <Grid item>
112                             <Tooltip title="Decrease font size" disableFocusListener>
113                                 <IconButton onClick={() => fontSize > 1 && setFontSize(fontSize-1)}>
114                                     <TextDecreaseIcon />
115                                 </IconButton>
116                             </Tooltip>
117                         </Grid>
118                         <Grid item>
119                             <Tooltip title="Increase font size" disableFocusListener>
120                                 <IconButton onClick={() => fontSize < 5 && setFontSize(fontSize+1)}>
121                                     <TextIncreaseIcon />
122                                 </IconButton>
123                             </Tooltip>
124                         </Grid>
125                         <Grid item>
126                             <Tooltip title="Copy to clipboard" disableFocusListener>
127                                 <IconButton>
128                                     <CopyToClipboard text={lines.join()} onCopy={() => onCopy("Log copied to clipboard")}>
129                                         <CopyIcon />
130                                     </CopyToClipboard>
131                                 </IconButton>
132                             </Tooltip>
133                         </Grid>
134                         <Grid item>
135                             <Tooltip title={`${wordWrap ? 'Disable' : 'Enable'} word wrapping`} disableFocusListener>
136                                 <IconButton onClick={() => setWordWrap(!wordWrap)}>
137                                     {wordWrap ? <WordWrapOffIcon /> : <WordWrapOnIcon />}
138                                 </IconButton>
139                             </Tooltip>
140                         </Grid>
141                         <Grid item>
142                             <Tooltip title="Go to Log collection" disableFocusListener>
143                                 <IconButton onClick={() => navigateToLog(process.containerRequest.logUuid!)}>
144                                     <CollectionIcon />
145                                 </IconButton>
146                             </Tooltip>
147                         </Grid>
148                         { doUnMaximizePanel && panelMaximized &&
149                         <Tooltip title={`Unmaximize ${panelName || 'panel'}`} disableFocusListener>
150                             <IconButton onClick={doUnMaximizePanel}><UnMaximizeIcon /></IconButton>
151                         </Tooltip> }
152                         { doMaximizePanel && !panelMaximized &&
153                         <Tooltip title={`Maximize ${panelName || 'panel'}`} disableFocusListener>
154                             <IconButton onClick={doMaximizePanel}><MaximizeIcon /></IconButton>
155                         </Tooltip> }
156                         { doHidePanel && <Grid item>
157                             <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
158                                 <IconButton onClick={doHidePanel}><CloseIcon /></IconButton>
159                             </Tooltip>
160                         </Grid> }
161                     </Grid>}
162                     title={
163                         <Typography noWrap variant='h6' className={classes.title}>
164                             Logs
165                         </Typography>}
166                 />
167                 <CardContent className={classes.content}>
168                     {lines.length > 0
169                         ? < Grid
170                             className={classes.logViewerContainer}
171                             container
172                             spacing={24}
173                             direction='column'>
174                             <Grid className={classes.logViewer} item xs>
175                                 <ProcessLogCodeSnippet fontSize={fontBaseSize+(fontStepSize*fontSize)} wordWrap={wordWrap} lines={lines} />
176                             </Grid>
177                         </Grid>
178                         : <DefaultView
179                             icon={LogIcon}
180                             messages={['No logs yet']} />
181                     }
182                 </CardContent>
183             </Card>
184         </Grid >
185 });
186