Skip to content

Commit fe2d8e1

Browse files
committed
Fix yaml content handling in UI
1 parent da0f48e commit fe2d8e1

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

ui/ui-app/src/app/pages/version/components/tabs/ContentTabContent.tsx

+43-43
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@ import { ToggleGroup, ToggleGroupItem } from "@patternfly/react-core";
44
import YAML from "yaml";
55
import useResizeObserver from "use-resize-observer";
66
import Editor from "@monaco-editor/react";
7+
import { detectContentType } from "@utils/content.utils.ts";
8+
import { ContentTypes } from "@models/contentTypes.model.ts";
79

10+
const TYPE_MAP: any = {};
11+
TYPE_MAP[ContentTypes.APPLICATION_PROTOBUF] = "protobuf";
12+
TYPE_MAP[ContentTypes.APPLICATION_XML] = "xml";
13+
TYPE_MAP[ContentTypes.APPLICATION_JSON] = "json";
14+
TYPE_MAP[ContentTypes.APPLICATION_YAML] = "yaml";
15+
TYPE_MAP[ContentTypes.APPLICATION_GRAPHQL] = "graphqlschema";
816

9-
const getEditorMode = (artifactType: string): string => {
10-
if (artifactType === "PROTOBUF") {
11-
return "protobuf";
12-
}
13-
if (artifactType === "WSDL" || artifactType === "XSD" || artifactType === "XML") {
14-
return "xml";
15-
}
16-
if (artifactType === "GRAPHQL") {
17-
return "graphqlschema";
18-
}
19-
return "json";
20-
};
2117

22-
const formatContent = (artifactContent: string): string => {
23-
try {
24-
const pval: any = JSON.parse(artifactContent);
25-
if (pval) {
26-
return JSON.stringify(pval, null, 2);
27-
}
28-
} catch (e) {
29-
// Do nothing
30-
}
31-
return artifactContent;
18+
const getEditorMode = (artifactType: string, content: string): string => {
19+
const ct: string = detectContentType(artifactType, content);
20+
return TYPE_MAP[ct];
3221
};
22+
//
23+
// const formatContent = (artifactContent: string): string => {
24+
// try {
25+
// const pval: any = JSON.parse(artifactContent);
26+
// if (pval) {
27+
// return JSON.stringify(pval, null, 2);
28+
// }
29+
// } catch (e) {
30+
// // Do nothing
31+
// }
32+
// return artifactContent;
33+
// };
3334

3435

3536
/**
@@ -45,8 +46,8 @@ export type ContentTabContentProps = {
4546
* Models the content of the Artifact Content tab.
4647
*/
4748
export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (props: ContentTabContentProps) => {
48-
const [content, setContent] = useState(formatContent(props.versionContent));
49-
const [editorMode, setEditorMode] = useState(getEditorMode(props.artifactType));
49+
const [content, setContent] = useState(props.versionContent);
50+
const [editorMode, setEditorMode] = useState(getEditorMode(props.artifactType, props.versionContent));
5051
const [compactButtons, setCompactButtons] = useState(false);
5152

5253
const { ref, width = 0, height = 0 } = useResizeObserver<HTMLDivElement>();
@@ -55,25 +56,24 @@ export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (pro
5556
setCompactButtons(width < 500);
5657
}, [width, height]);
5758

58-
const switchJsonYaml = (mode: string): (() => void) => {
59-
return () => {
60-
if (mode === editorMode) {
61-
return;
62-
} else {
63-
let content: string = `Error formatting code to: ${mode}`;
64-
try {
65-
if (mode === "yaml") {
66-
content = YAML.stringify(JSON.parse(props.versionContent), null, 4);
67-
} else {
68-
content = JSON.stringify(YAML.parse(props.versionContent), null, 2);
69-
}
70-
} catch (e) {
71-
handleInvalidContentError(e);
59+
const switchJsonYaml = (mode: string): void => {
60+
console.info("SWITCHING TO: ", mode);
61+
if (mode === editorMode) {
62+
return;
63+
} else {
64+
let content: string = `Error formatting code to: ${mode}`;
65+
try {
66+
if (mode === "yaml") {
67+
content = YAML.stringify(JSON.parse(content), null, 4);
68+
} else {
69+
content = JSON.stringify(YAML.parse(content), null, 2);
7270
}
73-
setEditorMode(mode);
74-
setContent(content);
71+
} catch (e) {
72+
handleInvalidContentError(e);
7573
}
76-
};
74+
setEditorMode(mode);
75+
setContent(content);
76+
}
7777
};
7878

7979
const handleInvalidContentError = (error: any): void => {
@@ -88,14 +88,14 @@ export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (pro
8888
text="JSON"
8989
buttonId="json"
9090
isSelected={editorMode === "json"}
91-
onChange={switchJsonYaml("json")}
91+
onChange={() => switchJsonYaml("json")}
9292
isDisabled={editorMode === "json"}
9393
/>
9494
<ToggleGroupItem
9595
text="YAML"
9696
buttonId="yaml"
9797
isSelected={editorMode === "yaml"}
98-
onChange={switchJsonYaml("yaml")}
98+
onChange={() => switchJsonYaml("yaml")}
9999
isDisabled={editorMode === "yaml"}
100100
/>
101101
</ToggleGroup>

0 commit comments

Comments
 (0)