-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathuseComposedEditorController.ts
More file actions
96 lines (82 loc) · 3.05 KB
/
useComposedEditorController.ts
File metadata and controls
96 lines (82 loc) · 3.05 KB
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
import { useCallback, useEffect, useMemo, useState } from "react";
import { fallback, PlaygroundDataV1 } from "@mendix/shared-charts/main";
import { ComposedEditorProps } from "../components/ComposedEditor";
import { SelectOption } from "../components/Sidebar";
type Config = object;
type Data = object;
type Layout = object;
const irrelevantSeriesKeys = ["x", "y", "z", "customSeriesOptions", "dataSourceItems"];
type ConfigKey = "layout" | "config" | number;
function getEditorCode({ store }: PlaygroundDataV1, key: ConfigKey): string {
let value = typeof key === "number" ? store.state.data.at(key) : store.state[key];
value = value ?? '{ "error": "value is unavailable" }';
return value;
}
function getModelerCode(data: PlaygroundDataV1, key: ConfigKey): Partial<Data> | Partial<Layout> | Partial<Config> {
if (key === "layout") {
return data.layoutOptions;
}
if (key === "config") {
return data.configOptions;
}
const entries = Object.entries(data.plotData.at(key) ?? {}).filter(([key]) => !irrelevantSeriesKeys.includes(key));
return Object.fromEntries(entries) as Partial<Data>;
}
function prettifyJson(json: string): string {
try {
return JSON.stringify(JSON.parse(json), null, 2);
} catch {
return '{ "error": "invalid JSON" }';
}
}
export function useComposedEditorController(data: PlaygroundDataV1): ComposedEditorProps {
const [key, setKey] = useState<ConfigKey>("layout");
const onViewSelectChange = (value: string): void => {
if (value === "layout" || value === "config") {
setKey(value);
} else {
const n = parseInt(value, 10);
setKey(isNaN(n) ? "layout" : n);
}
};
const options: SelectOption[] = useMemo(() => {
return [
{ name: "Layout", value: "layout", isDefaultSelected: true },
...data.plotData.map((trace, index) => ({
name: trace.name || `trace ${index}`,
value: index,
isDefaultSelected: false
})),
{ name: "Configuration", value: "config", isDefaultSelected: false }
];
}, [data.plotData]);
const store = data.store;
const code = prettifyJson(getEditorCode(data, key));
const [input, setInput] = useState(() => code);
const onEditorChange = useCallback(
(value: string): void => {
setInput(value);
try {
const json = fallback(value);
JSON.parse(value);
store.set(key, json);
// eslint-disable-next-line no-empty
} catch {}
},
[store, key]
);
useEffect(
() =>
// eslint-disable-next-line react-hooks/set-state-in-effect
setInput(code),
[code]
);
return {
viewSelectValue: key.toString(),
viewSelectOptions: options,
onViewSelectChange,
value: input,
modelerCode: useMemo(() => JSON.stringify(getModelerCode(data, key), null, 2), [data, key]),
onEditorChange
};
}