1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { useEffect, useRef, useState } from 'react';
12 } from '@material-ui/core/styles';
13 import grey from '@material-ui/core/colors/grey';
14 import { ArvadosTheme } from 'common/custom-theme';
15 import { Link, Typography } from '@material-ui/core';
16 import { navigateTo } from 'store/navigation/navigation-action';
17 import { Dispatch } from 'redux';
18 import { connect, DispatchProp } from 'react-redux';
19 import classNames from 'classnames';
21 type CssRules = 'root' | 'wordWrap' | 'logText';
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 boxSizing: 'border-box',
27 backgroundColor: '#000',
28 height: `calc(100% - ${theme.spacing.unit * 4}px)`, // so that horizontal scollbar is visible
30 color: theme.palette.primary.main,
34 padding: theme.spacing.unit * 0.5,
37 whiteSpace: 'pre-wrap',
41 const theme = createMuiTheme({
50 fontFamily: 'monospace',
51 useNextVariants: true,
55 interface ProcessLogCodeSnippetProps {
61 const renderLinks = (fontSize: number, dispatch: Dispatch) => (text: string) => {
62 // Matches UUIDs & PDHs
63 const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
64 const links = text.match(REGEX);
66 return <Typography style={{ fontSize: fontSize }}>{text}</Typography>;
68 return <Typography style={{ fontSize: fontSize }}>
69 {text.split(REGEX).map((part, index) =>
70 <React.Fragment key={index}>
73 <Link onClick={() => dispatch<any>(navigateTo(links[index]))}
74 style={ {cursor: 'pointer'} }>
82 export const ProcessLogCodeSnippet = withStyles(styles)(connect()(
83 ({classes, lines, fontSize, dispatch, wordWrap}: ProcessLogCodeSnippetProps & WithStyles<CssRules> & DispatchProp) => {
84 const [followMode, setFollowMode] = useState<boolean>(true);
85 const scrollRef = useRef<HTMLDivElement>(null);
88 if (followMode && scrollRef.current && lines.length > 0) {
90 scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
92 }, [followMode, lines, scrollRef]);
94 return <MuiThemeProvider theme={theme}>
95 <div ref={scrollRef} className={classes.root}
97 const elem = e.target as HTMLDivElement;
98 if (elem.scrollTop + elem.clientHeight >= elem.scrollHeight) {
101 setFollowMode(false);
104 { lines.map((line: string, index: number) =>
105 <Typography key={index} component="pre"
106 className={classNames(classes.logText, wordWrap ? classes.wordWrap : undefined)}>
107 {renderLinks(fontSize, dispatch)(line)}