Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/web/ui/src/features/component/ComponentBody.tsx
5304 views
1
import React, { Fragment } from 'react';
2
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
3
4
import { riverStringify } from '../river-js/stringify';
5
6
import { style } from './style';
7
import Table from './Table';
8
import { PartitionedBody } from './types';
9
10
import styles from './ComponentView.module.css';
11
12
interface ComponentBodyProps {
13
partition: PartitionedBody;
14
}
15
16
const TABLEHEADERS = ['Name', 'Value'];
17
18
const ComponentBody = ({ partition }: ComponentBodyProps) => {
19
const sectionClass = partition.key.length === 1 ? '' : styles.nested;
20
21
const renderTableData = () => {
22
return partition.attrs.map(({ name, value }, index) => {
23
return (
24
<tr key={name}>
25
<td className={styles.nameColumn}>{name}</td>
26
<td>
27
<pre className={styles.pre}>
28
<SyntaxHighlighter language="javascript" style={style}>
29
{riverStringify(value)}
30
</SyntaxHighlighter>
31
</pre>
32
</td>
33
</tr>
34
);
35
});
36
};
37
38
return (
39
<>
40
<section id={partition.key.join('-')} className={sectionClass}>
41
{
42
// If the partition only has 1 key, then make it an h2.
43
// Otherwise, make it an h3.
44
partition.displayName.length === 1 ? (
45
<h2>{partition.displayName}</h2>
46
) : (
47
<h3>
48
{partition.displayName.map((val, idx) => {
49
return (
50
<Fragment key={val}>
51
<span>{val}</span>
52
{idx + 1 < partition.key.length && <span> / </span>}
53
</Fragment>
54
);
55
})}
56
</h3>
57
)
58
}
59
<div className={styles.sectionContent}>
60
{partition.attrs.length === 0 ? (
61
<em className={styles.informative}>(No set attributes in this block)</em>
62
) : (
63
<div className={styles.list}>
64
<Table tableHeaders={TABLEHEADERS} renderTableData={renderTableData} style={{ width: '210px' }} />
65
</div>
66
)}
67
</div>
68
</section>
69
{partition.inner.map((body) => {
70
return <ComponentBody key={body.key.join('.')} partition={body} />;
71
})}
72
</>
73
);
74
};
75
76
export default ComponentBody;
77
78