Skip to content

Commit 557d4ad

Browse files
Add JSON viewer for objects in workflow diagnostics metadata (#967)
Add simple JSON viewer for object types in Workflow Diagnostics metadata, adapted from WorkflowHistoryEventDetailsJson
1 parent c3b7529 commit 557d4ad

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
3+
import { render } from '@/test-utils/rtl';
4+
5+
import losslessJsonStringify from '@/utils/lossless-json-stringify';
6+
7+
import WorkflowDiagnosticsMetadataJson from '../workflow-diagnostics-metadata-json';
8+
9+
jest.mock('@/components/copy-text-button/copy-text-button', () =>
10+
jest.fn(({ textToCopy }) => <div>Copy Button: {textToCopy}</div>)
11+
);
12+
13+
jest.mock('@/components/pretty-json/pretty-json', () =>
14+
jest.fn(() => <div>PrettyJson Mock</div>)
15+
);
16+
17+
describe('WorkflowDiagnosticsMetadataJson', () => {
18+
const losslessInputJson = {
19+
input: 'inputJson',
20+
long: BigInt('9007199254740991345435'),
21+
};
22+
23+
it('renders correctly with initial props', () => {
24+
const { getByText } = render(
25+
<WorkflowDiagnosticsMetadataJson value={losslessInputJson} />
26+
);
27+
28+
expect(getByText('PrettyJson Mock')).toBeInTheDocument();
29+
});
30+
31+
it('renders copy text button and pass the correct text', () => {
32+
const { getByText } = render(
33+
<WorkflowDiagnosticsMetadataJson value={losslessInputJson} />
34+
);
35+
36+
const copyButton = getByText(/Copy Button/);
37+
expect(copyButton).toBeInTheDocument();
38+
expect(copyButton.innerHTML).toMatch(
39+
losslessJsonStringify(losslessInputJson, null, '\t')
40+
);
41+
});
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { styled as createStyled, type Theme } from 'baseui';
2+
import type { ButtonOverrides } from 'baseui/button';
3+
4+
export const styled = {
5+
JsonViewWrapper: createStyled('div', {
6+
position: 'relative',
7+
width: '100%',
8+
}),
9+
JsonViewContainer: createStyled('div', ({ $theme }: { $theme: Theme }) => ({
10+
padding: $theme.sizing.scale600,
11+
backgroundColor: $theme.colors.backgroundSecondary,
12+
borderRadius: $theme.borders.radius300,
13+
maxHeight: '50vh',
14+
overflow: 'auto',
15+
})),
16+
JsonViewHeader: createStyled('div', ({ $theme }: { $theme: Theme }) => ({
17+
display: 'flex',
18+
position: 'absolute',
19+
right: $theme.sizing.scale400,
20+
top: $theme.sizing.scale400,
21+
})),
22+
};
23+
24+
export const overrides = {
25+
copyButton: {
26+
BaseButton: {
27+
style: {
28+
backgroundColor: 'rgba(0, 0, 0, 0)',
29+
},
30+
},
31+
} satisfies ButtonOverrides,
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client';
2+
import React, { useMemo } from 'react';
3+
4+
import CopyTextButton from '@/components/copy-text-button/copy-text-button';
5+
import PrettyJson from '@/components/pretty-json/pretty-json';
6+
import losslessJsonStringify from '@/utils/lossless-json-stringify';
7+
8+
import { styled, overrides } from './workflow-diagnostics-metadata-json.styles';
9+
import type { Props } from './workflow-diagnostics-metadata-json.types';
10+
11+
// This component is currently adapted from the WorkflowHistoryEventDetailsJson viewer.
12+
// TODO: Create a shared JSON viewer component to handle the different JSON display requirements throughout the codebase.
13+
export default function WorkflowDiagnosticsMetadataJson({ value }: Props) {
14+
const textToCopy = useMemo(() => {
15+
return losslessJsonStringify(value, null, '\t');
16+
}, [value]);
17+
18+
return (
19+
<styled.JsonViewWrapper>
20+
<styled.JsonViewContainer>
21+
<styled.JsonViewHeader>
22+
<CopyTextButton
23+
textToCopy={textToCopy}
24+
overrides={overrides.copyButton}
25+
/>
26+
</styled.JsonViewHeader>
27+
<PrettyJson json={value} />
28+
</styled.JsonViewContainer>
29+
</styled.JsonViewWrapper>
30+
);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Props = {
2+
value: any;
3+
};

0 commit comments

Comments
 (0)