2022-06-03 08:09:47 -06:00
|
|
|
import React, { FC } from 'react';
|
|
|
|
|
|
2024-02-26 14:52:26 +01:00
|
|
|
import { Button, IconButton } from '@grafana/ui';
|
2022-06-03 08:09:47 -06:00
|
|
|
import cn from 'classnames/bind';
|
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
|
|
2024-02-26 14:52:26 +01:00
|
|
|
import { formatSourceCodeJsonString } from 'utils/string';
|
2024-02-13 10:53:18 +02:00
|
|
|
import { openNotification } from 'utils/utils';
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-09-07 13:48:55 +03:00
|
|
|
import styles from './SourceCode.module.scss';
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
const cx = cn.bind(styles);
|
|
|
|
|
|
|
|
|
|
interface SourceCodeProps {
|
|
|
|
|
noMaxHeight?: boolean;
|
2022-09-14 15:35:39 +03:00
|
|
|
showClipboardIconOnly?: boolean;
|
2022-08-26 17:40:27 +03:00
|
|
|
showCopyToClipboard?: boolean;
|
2024-02-26 14:52:26 +01:00
|
|
|
children?: string;
|
2024-02-27 14:21:41 +01:00
|
|
|
rootClassName?: string;
|
|
|
|
|
preClassName?: string;
|
2024-02-26 14:52:26 +01:00
|
|
|
prettifyJsonString?: boolean;
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-26 14:52:26 +01:00
|
|
|
export const SourceCode: FC<SourceCodeProps> = ({
|
|
|
|
|
children,
|
|
|
|
|
noMaxHeight = false,
|
|
|
|
|
showClipboardIconOnly = false,
|
|
|
|
|
showCopyToClipboard = true,
|
2024-02-27 14:21:41 +01:00
|
|
|
rootClassName,
|
|
|
|
|
preClassName,
|
2024-02-26 14:52:26 +01:00
|
|
|
prettifyJsonString,
|
|
|
|
|
}) => {
|
2022-09-14 15:35:39 +03:00
|
|
|
const showClipboardCopy = showClipboardIconOnly || showCopyToClipboard;
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
return (
|
2024-02-27 14:21:41 +01:00
|
|
|
<div className={cx('root', rootClassName)}>
|
2022-09-14 15:35:39 +03:00
|
|
|
{showClipboardCopy && (
|
2022-08-26 17:40:27 +03:00
|
|
|
<CopyToClipboard
|
2024-02-26 14:52:26 +01:00
|
|
|
text={children}
|
2022-08-26 17:40:27 +03:00
|
|
|
onCopy={() => {
|
|
|
|
|
openNotification('Copied!');
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-09-14 15:35:39 +03:00
|
|
|
{showClipboardIconOnly ? (
|
2024-02-26 14:52:26 +01:00
|
|
|
<IconButton
|
|
|
|
|
aria-label="Copy"
|
|
|
|
|
className={cx('copyIcon')}
|
|
|
|
|
size={'lg'}
|
|
|
|
|
name="copy"
|
|
|
|
|
data-testid="test__copyIcon"
|
|
|
|
|
/>
|
2022-09-14 15:35:39 +03:00
|
|
|
) : (
|
2022-10-24 14:27:03 +02:00
|
|
|
<Button
|
|
|
|
|
className={cx('copyButton')}
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="xs"
|
|
|
|
|
icon="copy"
|
|
|
|
|
data-testid="test__copyIconWithText"
|
|
|
|
|
>
|
2022-09-14 15:35:39 +03:00
|
|
|
Copy
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2022-08-26 17:40:27 +03:00
|
|
|
</CopyToClipboard>
|
|
|
|
|
)}
|
2022-06-03 08:09:47 -06:00
|
|
|
<pre
|
2023-05-03 16:51:45 +02:00
|
|
|
className={cx(
|
|
|
|
|
'scroller',
|
|
|
|
|
{
|
|
|
|
|
'scroller--maxHeight': !noMaxHeight,
|
|
|
|
|
},
|
2024-02-27 14:21:41 +01:00
|
|
|
preClassName
|
2023-05-03 16:51:45 +02:00
|
|
|
)}
|
2022-06-03 08:09:47 -06:00
|
|
|
>
|
2024-02-26 14:52:26 +01:00
|
|
|
<code>{prettifyJsonString ? formatSourceCodeJsonString(children) : children}</code>
|
2022-06-03 08:09:47 -06:00
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|