1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { WithStyles, Typography, withStyles, Link } from '@material-ui/core';
8 import { ArvadosTheme } from 'common/custom-theme';
9 import classNames from 'classnames';
10 import { connect, DispatchProp } from 'react-redux';
11 import { RootState } from 'store/store';
12 import { FederationConfig, getNavUrl } from 'routes/routes';
13 import { Dispatch } from 'redux';
14 import { navigationNotAvailable } from 'store/navigation/navigation-action';
16 type CssRules = 'root' | 'inlineRoot' | 'space' | 'inline';
18 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20 boxSizing: 'border-box',
22 padding: theme.spacing(1),
36 export interface CodeSnippetDataProps {
39 apiResponse?: boolean;
41 children?: JSX.Element;
45 interface CodeSnippetAuthProps {
46 auth: FederationConfig;
49 type CodeSnippetProps = CodeSnippetDataProps & WithStyles<CssRules>;
51 const mapStateToProps = (state: RootState): CodeSnippetAuthProps => ({
55 export const CodeSnippet = withStyles(styles)(connect(mapStateToProps)(
56 ({ classes, lines, linked, className, apiResponse, dispatch, auth, children, inline }: CodeSnippetProps & CodeSnippetAuthProps & DispatchProp) =>
59 className={classNames([classes.root, className, inline ? classes.inlineRoot : undefined])}>
60 <Typography className={apiResponse ? classes.space : classNames([className, inline ? classes.inline : undefined])} component="pre">
63 lines.map((line, index) => <React.Fragment key={index}>{renderLinks(auth, dispatch)(line)}{`\n`}</React.Fragment>) :
70 export const renderLinks = (auth: FederationConfig, dispatch: Dispatch) => (text: string): JSX.Element => {
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);
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' }}>