21720: changed padding in code-snippet logs
[arvados.git] / services / workbench2 / src / views / process-panel / process-log-code-snippet.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { useEffect, useRef, useState } from 'react';
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { ThemeProvider, Theme, StyledEngineProvider, createTheme, adaptV4Theme } from '@mui/material/styles';
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import { ArvadosTheme } from 'common/custom-theme';
11 import { Link, Typography } from '@mui/material';
12 import { navigationNotAvailable } from 'store/navigation/navigation-action';
13 import { Dispatch } from 'redux';
14 import { connect, DispatchProp } from 'react-redux';
15 import classNames from 'classnames';
16 import { FederationConfig, getNavUrl } from 'routes/routes';
17 import { RootState } from 'store/store';
18 import { grey } from '@mui/material/colors';
19
20
21 declare module '@mui/styles/defaultTheme' {
22   // eslint-disable-next-line @typescript-eslint/no-empty-interface
23   interface DefaultTheme extends Theme {}
24 }
25
26
27 type CssRules = 'root' | 'wordWrapOn' | 'wordWrapOff' | 'logText';
28
29 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     root: {
31         boxSizing: 'border-box',
32         overflow: 'auto',
33         backgroundColor: '#000',
34         height: `calc(100% - ${theme.spacing(4)})`, // so that horizontal scollbar is visible
35         "& a": {
36             color: theme.palette.primary.main,
37         },
38     },
39     logText: {
40         color: '#fff',
41         padding: theme.spacing(0, 0.5),
42         display: 'block',
43     },
44     wordWrapOn: {
45         overflowWrap: 'anywhere',
46     },
47     wordWrapOff: {
48         whiteSpace: 'nowrap',
49     },
50 });
51
52 const theme = createTheme(adaptV4Theme({
53     overrides: {
54         MuiTypography: {
55             body2: {
56                 color: grey["200"]
57             }
58         }
59     },
60     typography: {
61         fontFamily: 'monospace',
62     }
63 }));
64
65 interface ProcessLogCodeSnippetProps {
66     lines: string[];
67     fontSize: number;
68     wordWrap?: boolean;
69 }
70
71 interface ProcessLogCodeSnippetAuthProps {
72     auth: FederationConfig;
73 }
74
75 const renderLinks = (fontSize: number, auth: FederationConfig, dispatch: Dispatch) => (text: string) => {
76     // Matches UUIDs & PDHs
77     const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
78     const links = text.match(REGEX);
79     if (!links) {
80         return <Typography style={{ fontSize: fontSize }}>{text}</Typography>;
81     }
82     return <Typography style={{ fontSize: fontSize }}>
83         {text.split(REGEX).map((part, index) =>
84             <React.Fragment key={index}>
85                 {part}
86                 {links[index] &&
87                     <Link onClick={() => {
88                         const url = getNavUrl(links[index], auth)
89                         if (url) {
90                             window.open(`${window.location.origin}${url}`, '_blank', "noopener");
91                         } else {
92                             dispatch(navigationNotAvailable(links[index]));
93                         }
94                     }}
95                         style={{ cursor: 'pointer' }}>
96                         {links[index]}
97                     </Link>}
98             </React.Fragment>
99         )}
100     </Typography>;
101 };
102
103 const mapStateToProps = (state: RootState): ProcessLogCodeSnippetAuthProps => ({
104     auth: state.auth,
105 });
106
107 export const ProcessLogCodeSnippet = withStyles(styles)(connect(mapStateToProps)(
108     ({ classes, lines, fontSize, auth, dispatch, wordWrap }: ProcessLogCodeSnippetProps & WithStyles<CssRules> & ProcessLogCodeSnippetAuthProps & DispatchProp) => {
109         const [followMode, setFollowMode] = useState<boolean>(true);
110         const scrollRef = useRef<HTMLDivElement>(null);
111
112         useEffect(() => {
113             if (followMode && scrollRef.current && lines.length > 0) {
114                 // Scroll to bottom
115                 scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
116             }
117         }, [followMode, lines, scrollRef]);
118
119         return (
120             <StyledEngineProvider injectFirst>
121                 <ThemeProvider theme={theme}>
122                     <div ref={scrollRef} className={classes.root}
123                         onScroll={(e) => {
124                             const elem = e.target as HTMLDivElement;
125                             if (elem.scrollTop + (elem.clientHeight * 1.1) >= elem.scrollHeight) {
126                                 setFollowMode(true);
127                             } else {
128                                 setFollowMode(false);
129                             }
130                         }}>
131                         {lines.map((line: string, index: number) =>
132                             <Typography key={index} component="span"
133                                 className={classNames(classes.logText, wordWrap ? classes.wordWrapOn : classes.wordWrapOff)}>
134                                 {renderLinks(fontSize, auth, dispatch)(line)}
135                             </Typography>
136                         )}
137                     </div>
138                 </ThemeProvider>
139             </StyledEngineProvider>
140         );
141     }));