|
| 1 | +import { observable, reaction, runInAction } from "mobx"; |
| 2 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 3 | +import { PlaygroundDataV2 } from "@mendix/shared-charts/main"; |
| 4 | +import { ComposedEditorProps } from "../components/ComposedEditor"; |
| 5 | +import { SelectOption } from "../components/Sidebar"; |
| 6 | + |
| 7 | +type ConfigKey = "layout" | "config" | number; |
| 8 | + |
| 9 | +const irrelevantSeriesKeys = ["x", "y", "z", "customSeriesOptions", "dataSourceItems"]; |
| 10 | + |
| 11 | +function getEditorCode(store: PlaygroundDataV2["store"], key: ConfigKey): string { |
| 12 | + if (key === "layout") { |
| 13 | + return store.layoutJson ?? '{ "error": "value is unavailable" }'; |
| 14 | + } |
| 15 | + if (key === "config") { |
| 16 | + return store.configJson ?? '{ "error": "value is unavailable" }'; |
| 17 | + } |
| 18 | + return store.dataJson.at(key) ?? '{ "error": "value is unavailable" }'; |
| 19 | +} |
| 20 | + |
| 21 | +function getModelerCode(data: PlaygroundDataV2, key: ConfigKey): object { |
| 22 | + if (key === "layout") { |
| 23 | + return data.layoutOptions; |
| 24 | + } |
| 25 | + if (key === "config") { |
| 26 | + return data.configOptions; |
| 27 | + } |
| 28 | + const entries = Object.entries(data.plotData.at(key) ?? {}).filter(([k]) => !irrelevantSeriesKeys.includes(k)); |
| 29 | + return Object.fromEntries(entries); |
| 30 | +} |
| 31 | + |
| 32 | +function prettifyJson(json: string): string { |
| 33 | + try { |
| 34 | + return JSON.stringify(JSON.parse(json), null, 2); |
| 35 | + } catch { |
| 36 | + return '{ "error": "invalid JSON" }'; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export function useV2EditorController(context: PlaygroundDataV2): ComposedEditorProps { |
| 41 | + const [key, setKey] = useState<ConfigKey>("layout"); |
| 42 | + const keyBox = useState(() => observable.box<ConfigKey>(key))[0]; |
| 43 | + |
| 44 | + const onViewSelectChange = (value: string): void => { |
| 45 | + let newKey: ConfigKey; |
| 46 | + if (value === "layout" || value === "config") { |
| 47 | + newKey = value; |
| 48 | + } else { |
| 49 | + const n = parseInt(value, 10); |
| 50 | + newKey = isNaN(n) ? "layout" : n; |
| 51 | + } |
| 52 | + setKey(newKey); |
| 53 | + runInAction(() => keyBox.set(newKey)); |
| 54 | + }; |
| 55 | + |
| 56 | + const store = context.store; |
| 57 | + |
| 58 | + const options: SelectOption[] = useMemo(() => { |
| 59 | + return [ |
| 60 | + { name: "Layout", value: "layout", isDefaultSelected: true }, |
| 61 | + ...store.data.map((trace, index) => ({ |
| 62 | + name: (trace.name as string) || `trace ${index}`, |
| 63 | + value: index, |
| 64 | + isDefaultSelected: false |
| 65 | + })), |
| 66 | + { name: "Configuration", value: "config", isDefaultSelected: false } |
| 67 | + ]; |
| 68 | + }, [store.data]); |
| 69 | + |
| 70 | + const code = prettifyJson(getEditorCode(store, key)); |
| 71 | + const [input, setInput] = useState(() => code); |
| 72 | + const onEditorChange = useCallback( |
| 73 | + (value: string): void => { |
| 74 | + setInput(value); |
| 75 | + try { |
| 76 | + // Parse string before sending to store |
| 77 | + const obj = JSON.parse(value); |
| 78 | + if (key === "layout") { |
| 79 | + store.setLayout(obj); |
| 80 | + } else if (key === "config") { |
| 81 | + store.setConfig(obj); |
| 82 | + } else { |
| 83 | + store.setDataAt(key, value); |
| 84 | + } |
| 85 | + // eslint-disable-next-line no-empty |
| 86 | + } catch {} |
| 87 | + }, |
| 88 | + [store, key] |
| 89 | + ); |
| 90 | + |
| 91 | + useEffect( |
| 92 | + () => |
| 93 | + reaction( |
| 94 | + () => getEditorCode(store, keyBox.get()), |
| 95 | + code => setInput(prettifyJson(code)) |
| 96 | + ), |
| 97 | + [store, keyBox] |
| 98 | + ); |
| 99 | + |
| 100 | + return { |
| 101 | + viewSelectValue: key.toString(), |
| 102 | + viewSelectOptions: options, |
| 103 | + onViewSelectChange, |
| 104 | + value: input, |
| 105 | + modelerCode: useMemo(() => JSON.stringify(getModelerCode(context, key), null, 2), [context, key]), |
| 106 | + onEditorChange |
| 107 | + }; |
| 108 | +} |
0 commit comments