-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdocViewer.tsx
122 lines (112 loc) · 3.31 KB
/
docViewer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useEffect, useMemo, useState } from 'react';
import uniqueId from 'lodash/uniqueId';
import {
EuiFlexGroup,
EuiFlexItem,
EuiLink,
EuiPanel,
EuiTabbedContent,
EuiTabbedContentTab,
} from '@elastic/eui';
import { DocViewTable } from './detail_table/doc_detail_table';
import { JsonCodeBlock } from './json_code_block/json_code_block';
import { IDocType } from './docViewRow';
import { HttpSetup } from '../../../../../../../src/core/public';
import { TraceBlock } from './trace_block/trace_block';
import { OTEL_TRACE_ID } from '../../../../../common/constants/explorer';
import { isValidTraceId } from '../../utils';
import { observabilityTracesID } from '../../../../../common/constants/shared';
interface IDocViewerProps {
http: HttpSetup;
hit: IDocType;
openTraces: boolean;
}
export function DocViewer(props: IDocViewerProps) {
const [curSelectedTab, setCurSelectedTab] = useState<EuiTabbedContentTab | null>(null);
const [logTraceId, setLogTraceId] = useState('');
const [tracesLink, setTracesLink] = useState(<></>);
// can be passed in later
const getTabList = () => {
return [
{
id: uniqueId('doc_viewer_tab_'),
name: 'Table',
component: (tabProps: any) => (
<DocViewTable
filter={() => {}}
onAddColumn={() => {}}
onRemoveColumn={() => {}}
{...tabProps}
/>
),
otherProps: {},
},
{
id: uniqueId('doc_viewer_tab_'),
name: 'JSON',
component: (tabProps: any) => <JsonCodeBlock {...tabProps} />,
otherProps: {},
},
{
id: uniqueId('doc_viewer_tab_'),
name: (
<>
<span>Traces</span>
{tracesLink}
</>
),
component: (tabProps: any) => <TraceBlock http={props.http} {...tabProps} />,
otherProps: {},
},
];
};
const tabs = useMemo(() => {
return getTabList().map((tab) => {
const Component = tab.component;
return {
id: tab.id,
name: tab.name,
content: (
<EuiPanel paddingSize="s">
<EuiFlexGroup>
<EuiFlexItem>
<Component hit={props.hit} logTraceId={logTraceId} {...tab.otherProps} />{' '}
</EuiFlexItem>
</EuiFlexGroup>
</EuiPanel>
),
};
});
}, [props.hit, logTraceId, tracesLink]);
if (!tabs.length) {
// There there's a minimum of 2 tabs active in Discover.
// This condition takes care of unit tests with 0 tabs.
return null;
}
useEffect(() => {
const traceId = props.hit.hasOwnProperty(OTEL_TRACE_ID) ? props.hit[OTEL_TRACE_ID] : '';
setLogTraceId(traceId);
if (traceId !== '' && isValidTraceId(traceId))
setTracesLink(
<EuiLink
className="trace-link"
href={`${observabilityTracesID}#/traces/${traceId}`}
target="_blank"
external
/>
);
}, []);
return (
<div className="osdDocViewer">
<EuiTabbedContent
tabs={tabs}
selectedTab={curSelectedTab || (props.openTraces ? tabs[2] : tabs[0])}
onTabClick={(selectedTab: EuiTabbedContentTab) => setCurSelectedTab(selectedTab)}
/>
</div>
);
}