19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / components / code-snippet / code-snippet.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
14
15 type CssRules = 'root' | 'space';
16
17 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
18     root: {
19         boxSizing: 'border-box',
20         overflow: 'auto',
21         padding: theme.spacing.unit
22     },
23     space: {
24         marginLeft: '15px'
25     }
26 });
27
28 export interface CodeSnippetDataProps {
29     lines: string[];
30     className?: string;
31     apiResponse?: boolean;
32     linked?: boolean;
33 }
34
35 interface CodeSnippetAuthProps {
36     auth: FederationConfig;
37 }
38
39 type CodeSnippetProps = CodeSnippetDataProps & WithStyles<CssRules>;
40
41 const mapStateToProps = (state: RootState): CodeSnippetAuthProps => ({
42     auth: state.auth,
43 });
44
45 export const CodeSnippet = withStyles(styles)(connect(mapStateToProps)(
46     ({ classes, lines, linked, className, apiResponse, dispatch, auth }: CodeSnippetProps & CodeSnippetAuthProps & DispatchProp) =>
47         <Typography
48         component="div"
49         className={classNames(classes.root, className)}>
50             <Typography className={apiResponse ? classes.space : className} component="pre">
51                 {linked ?
52                     lines.map((line, index) => <React.Fragment key={index}>{renderLinks(auth, dispatch)(line)}{`\n`}</React.Fragment>) :
53                     lines.join('\n')
54                 }
55             </Typography>
56         </Typography>
57     ));
58
59 const renderLinks = (auth: FederationConfig, dispatch: Dispatch) => (text: string): JSX.Element => {
60     // Matches UUIDs & PDHs
61     const REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}|[0-9a-f]{32}\+\d+/g;
62     const links = text.match(REGEX);
63     if (!links) {
64         return <>{text}</>;
65     }
66     return <>
67         {text.split(REGEX).map((part, index) =>
68             <React.Fragment key={index}>
69                 {part}
70                 {links[index] &&
71                 <Link onClick={() => {
72                     const url = getNavUrl(links[index], auth)
73                     if (url) {
74                         window.open(`${window.location.origin}${url}`, '_blank');
75                     } else {
76                         dispatch(navigationNotAvailable(links[index]));
77                     }
78                 }}
79                     style={ {cursor: 'pointer'} }>
80                     {links[index]}
81                 </Link>}
82             </React.Fragment>
83         )}
84     </>;
85   };