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 { Typography, Link } from '@mui/material';
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import { ArvadosTheme } from 'common/custom-theme';
11 import classNames from 'classnames';
12 import { connect, DispatchProp } from 'react-redux';
13 import { RootState } from 'store/store';
14 import { FederationConfig, getNavUrl } from 'routes/routes';
15 import { Dispatch } from 'redux';
16 import { navigationNotAvailable } from 'store/navigation/navigation-action';
18 type CssRules = 'root' | 'inlineRoot' | 'space' | 'inline';
20 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
22 boxSizing: 'border-box',
24 padding: theme.spacing(1),
38 export interface CodeSnippetDataProps {
41 apiResponse?: boolean;
43 children?: JSX.Element;
47 interface CodeSnippetAuthProps {
48 auth: FederationConfig;
51 type CodeSnippetProps = CodeSnippetDataProps & WithStyles<CssRules>;
53 const mapStateToProps = (state: RootState): CodeSnippetAuthProps => ({
57 export const CodeSnippet = withStyles(styles)(connect(mapStateToProps)(
58 ({ classes, lines, linked, className, apiResponse, dispatch, auth, children, inline }: CodeSnippetProps & CodeSnippetAuthProps & DispatchProp) =>
61 className={classNames([classes.root, className, inline ? classes.inlineRoot : undefined])}>
62 <Typography className={apiResponse ? classes.space : classNames([className, inline ? classes.inline : undefined])} component="pre">
65 lines.map((line, index) => <React.Fragment key={index}>{renderLinks(auth, dispatch)(line)}{`\n`}</React.Fragment>) :
72 export const renderLinks = (auth: FederationConfig, dispatch: Dispatch) => (text: string): JSX.Element => {
73 // Matches UUIDs & PDHs
74 const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
75 const links = text.match(REGEX);
80 {text.split(REGEX).map((part, index) =>
81 <React.Fragment key={index}>
84 <Link onClick={() => {
85 const url = getNavUrl(links[index], auth)
87 window.open(`${window.location.origin}${url}`, '_blank', "noopener");
89 dispatch(navigationNotAvailable(links[index]));
92 style={{ cursor: 'pointer' }}>