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, Tooltip, IconButton } from '@material-ui/core';
7 import { ArvadosTheme } from 'common/custom-theme';
8 import classNames from 'classnames';
9 import { connect } from 'react-redux';
10 import { RootState } from 'store/store';
11 import { FederationConfig } from 'routes/routes';
12 import { renderLinks } from './code-snippet';
13 import { FixedSizeList } from 'react-window';
14 import AutoSizer from "react-virtualized-auto-sizer";
15 import CopyResultToClipboard from 'components/copy-to-clipboard/copy-result-to-clipboard';
16 import { CopyIcon } from 'components/icon/icon';
17 import { SnackbarKind, snackbarActions } from 'store/snackbar/snackbar-actions';
18 import { Dispatch } from "redux";
20 type CssRules = 'root' | 'space' | 'content' | 'copyButton' ;
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 boxSizing: 'border-box',
27 padding: theme.spacing.unit,
44 export interface CodeSnippetDataProps {
46 lineFormatter?: (lines: string[], index: number) => string;
48 apiResponse?: boolean;
53 export interface CodeSnippetActionProps {
54 renderLinks: (auth: FederationConfig) => (text: string) => JSX.Element;
55 onCopyToClipboard: () => void;
58 interface CodeSnippetAuthProps {
59 auth: FederationConfig;
62 type CodeSnippetProps = CodeSnippetDataProps & CodeSnippetActionProps & WithStyles<CssRules>;
64 const mapStateToProps = (state: RootState): CodeSnippetAuthProps => ({
68 const mapDispatchToProps = (dispatch: Dispatch): CodeSnippetActionProps => ({
69 renderLinks: (auth: FederationConfig) => renderLinks(auth, dispatch),
70 onCopyToClipboard: () => {
72 snackbarActions.OPEN_SNACKBAR({
73 message: "Contents copied to clipboard",
75 kind: SnackbarKind.SUCCESS,
81 export const VirtualCodeSnippet = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
82 ({ classes, lines, lineFormatter, linked, copyButton, renderLinks, onCopyToClipboard, className, apiResponse, auth }: CodeSnippetProps & CodeSnippetAuthProps) => {
83 const RenderRow = ({index, style}) => {
84 const lineContents = lineFormatter ? lineFormatter(lines, index) : lines[index];
85 return <span style={style}>{linked ? renderLinks(auth)(lineContents) : lineContents}</span>
88 const formatClipboardText = (lines: string[]) => () => {
89 return lines.join('\n');
96 className={classNames([classes.root, className])}>
97 {copyButton && <span className={classes.copyButton}>
98 <Tooltip title="Copy text to clipboard" disableFocusListener>
100 <CopyResultToClipboard
101 getText={formatClipboardText(lines)}
102 onCopy={onCopyToClipboard}
105 </CopyResultToClipboard>
109 <Typography className={classNames(classes.content, apiResponse ? classes.space : className)} component="pre">
111 {({ height, width }) =>
116 itemCount={lines.length}