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 { navigationNotAvailable } from 'store/navigation/navigation-action';
17 import { Dispatch } from 'redux';
18 import { connect, DispatchProp } from 'react-redux';
19 import classNames from 'classnames';
20 import { FederationConfig, getNavUrl } from 'routes/routes';
21 import { RootState } from 'store/store';
23 type CssRules = 'root' | 'wordWrap' | 'logText';
25 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27 boxSizing: 'border-box',
29 backgroundColor: '#000',
30 height: `calc(100% - ${theme.spacing.unit * 4}px)`, // so that horizontal scollbar is visible
32 color: theme.palette.primary.main,
36 padding: `0 ${theme.spacing.unit*0.5}px`,
39 whiteSpace: 'pre-wrap',
43 const theme = createMuiTheme({
52 fontFamily: 'monospace',
53 useNextVariants: true,
57 interface ProcessLogCodeSnippetProps {
63 interface ProcessLogCodeSnippetAuthProps {
64 auth: FederationConfig;
67 const renderLinks = (fontSize: number, auth: FederationConfig, dispatch: Dispatch) => (text: string) => {
68 // Matches UUIDs & PDHs
69 const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
70 const links = text.match(REGEX);
72 return <Typography style={{ fontSize: fontSize }}>{text}</Typography>;
74 return <Typography style={{ fontSize: fontSize }}>
75 {text.split(REGEX).map((part, index) =>
76 <React.Fragment key={index}>
79 <Link onClick={() => {
80 const url = getNavUrl(links[index], auth)
82 window.open(`${window.location.origin}${url}`, '_blank');
84 dispatch(navigationNotAvailable(links[index]));
87 style={ {cursor: 'pointer'} }>
95 const mapStateToProps = (state: RootState): ProcessLogCodeSnippetAuthProps => ({
99 export const ProcessLogCodeSnippet = withStyles(styles)(connect(mapStateToProps)(
100 ({classes, lines, fontSize, auth, dispatch, wordWrap}: ProcessLogCodeSnippetProps & WithStyles<CssRules> & ProcessLogCodeSnippetAuthProps & DispatchProp) => {
101 const [followMode, setFollowMode] = useState<boolean>(true);
102 const scrollRef = useRef<HTMLDivElement>(null);
105 if (followMode && scrollRef.current && lines.length > 0) {
107 scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
109 }, [followMode, lines, scrollRef]);
111 return <MuiThemeProvider theme={theme}>
112 <div ref={scrollRef} className={classes.root}
114 const elem = e.target as HTMLDivElement;
115 if (elem.scrollTop + (elem.clientHeight*1.1) >= elem.scrollHeight) {
118 setFollowMode(false);
121 { lines.map((line: string, index: number) =>
122 <Typography key={index} component="pre"
123 className={classNames(classes.logText, wordWrap ? classes.wordWrap : undefined)}>
124 {renderLinks(fontSize, auth, dispatch)(line)}