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' | 'wordWrapOn' | 'wordWrapOff' | '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 overflowWrap: 'anywhere',
46 const theme = createMuiTheme({
55 fontFamily: 'monospace',
56 useNextVariants: true,
60 interface ProcessLogCodeSnippetProps {
66 interface ProcessLogCodeSnippetAuthProps {
67 auth: FederationConfig;
70 const renderLinks = (fontSize: number, auth: FederationConfig, dispatch: Dispatch) => (text: string) => {
71 // Matches UUIDs & PDHs
72 const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
73 const links = text.match(REGEX);
75 return <Typography style={{ fontSize: fontSize }}>{text}</Typography>;
77 return <Typography style={{ fontSize: fontSize }}>
78 {text.split(REGEX).map((part, index) =>
79 <React.Fragment key={index}>
82 <Link onClick={() => {
83 const url = getNavUrl(links[index], auth)
85 window.open(`${window.location.origin}${url}`, '_blank', "noopener");
87 dispatch(navigationNotAvailable(links[index]));
90 style={{ cursor: 'pointer' }}>
98 const mapStateToProps = (state: RootState): ProcessLogCodeSnippetAuthProps => ({
102 export const ProcessLogCodeSnippet = withStyles(styles)(connect(mapStateToProps)(
103 ({ classes, lines, fontSize, auth, dispatch, wordWrap }: ProcessLogCodeSnippetProps & WithStyles<CssRules> & ProcessLogCodeSnippetAuthProps & DispatchProp) => {
104 const [followMode, setFollowMode] = useState<boolean>(true);
105 const scrollRef = useRef<HTMLDivElement>(null);
108 if (followMode && scrollRef.current && lines.length > 0) {
110 scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
112 }, [followMode, lines, scrollRef]);
114 return <MuiThemeProvider theme={theme}>
115 <div ref={scrollRef} className={classes.root}
117 const elem = e.target as HTMLDivElement;
118 if (elem.scrollTop + (elem.clientHeight * 1.1) >= elem.scrollHeight) {
121 setFollowMode(false);
124 {lines.map((line: string, index: number) =>
125 <Typography key={index} component="span"
126 className={classNames(classes.logText, wordWrap ? classes.wordWrapOn : classes.wordWrapOff)}>
127 {renderLinks(fontSize, auth, dispatch)(line)}