1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { StyleRulesCallback, WithStyles, Typography, withStyles, Link } from '@material-ui/core';
7 import { ArvadosTheme } from 'common/custom-theme';
8 import classNames from 'classnames';
9 import { connect, DispatchProp } from 'react-redux';
10 import { RootState } from 'store/store';
11 import { FederationConfig, getNavUrl } from 'routes/routes';
12 import { Dispatch } from 'redux';
13 import { navigationNotAvailable } from 'store/navigation/navigation-action';
15 type CssRules = 'root' | 'space';
17 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
19 boxSizing: 'border-box',
21 padding: theme.spacing.unit
28 export interface CodeSnippetDataProps {
31 apiResponse?: boolean;
33 children?: JSX.Element;
36 interface CodeSnippetAuthProps {
37 auth: FederationConfig;
40 type CodeSnippetProps = CodeSnippetDataProps & WithStyles<CssRules>;
42 const mapStateToProps = (state: RootState): CodeSnippetAuthProps => ({
46 export const CodeSnippet = withStyles(styles)(connect(mapStateToProps)(
47 ({ classes, lines, linked, className, apiResponse, dispatch, auth, children }: CodeSnippetProps & CodeSnippetAuthProps & DispatchProp) =>
50 className={classNames(classes.root, className)}>
51 <Typography className={apiResponse ? classes.space : className} component="pre">
54 lines.map((line, index) => <React.Fragment key={index}>{renderLinks(auth, dispatch)(line)}{`\n`}</React.Fragment>) :
61 const renderLinks = (auth: FederationConfig, dispatch: Dispatch) => (text: string): JSX.Element => {
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);
69 {text.split(REGEX).map((part, index) =>
70 <React.Fragment key={index}>
73 <Link onClick={() => {
74 const url = getNavUrl(links[index], auth)
76 window.open(`${window.location.origin}${url}`, '_blank');
78 dispatch(navigationNotAvailable(links[index]));
81 style={ {cursor: 'pointer'} }>