forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorDebugTab.tsx
229 lines (216 loc) · 8.43 KB
/
EditorDebugTab.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React from "react"
import { observer } from "mobx-react"
import { Section, Toggle } from "./Forms.js"
import { ChartEditor, isChartEditorInstance } from "./ChartEditor.js"
import { action } from "mobx"
import { copyToClipboard, mergeGrapherConfigs } from "@ourworldindata/utils"
import YAML from "yaml"
import { notification } from "antd"
import {
IndicatorChartEditor,
isIndicatorChartEditorInstance,
} from "./IndicatorChartEditor.js"
import { AbstractChartEditor } from "./AbstractChartEditor.js"
@observer
export class EditorDebugTab<
Editor extends AbstractChartEditor,
> extends React.Component<{
editor: Editor
}> {
render() {
const { editor } = this.props
if (isChartEditorInstance(editor))
return <EditorDebugTabForChart editor={editor} />
else if (isIndicatorChartEditorInstance(editor))
return <EditorDebugTabForIndicatorChart editor={editor} />
else return null
}
}
@observer
class EditorDebugTabForChart extends React.Component<{
editor: ChartEditor
}> {
@action.bound copyYamlToClipboard() {
// Avoid modifying the original JSON object
// Due to mobx memoizing computed values, the JSON can be mutated.
const patchConfig = {
...this.props.editor.patchConfig,
}
delete patchConfig.id
delete patchConfig.dimensions
delete patchConfig.version
delete patchConfig.isPublished
const chartConfigAsYaml = YAML.stringify(patchConfig)
// Use the Clipboard API to copy the config into the users clipboard
void copyToClipboard(chartConfigAsYaml)
notification["success"]({
message: "Copied YAML to clipboard",
description: "You can now paste this into the ETL",
placement: "bottomRight",
closeIcon: <></>,
})
}
@action.bound onToggleInheritance(shouldBeEnabled: boolean) {
const { patchConfig, parentConfig } = this.props.editor
// update live grapher
const newParentConfig = shouldBeEnabled ? parentConfig : undefined
const newConfig = mergeGrapherConfigs(
newParentConfig ?? {},
patchConfig
)
this.props.editor.updateLiveGrapher(newConfig)
this.props.editor.isInheritanceEnabled = shouldBeEnabled
}
render() {
const {
patchConfig,
parentConfig,
isInheritanceEnabled,
fullConfig,
parentVariableId,
grapher,
} = this.props.editor
const column = parentVariableId
? grapher.inputTable.get(parentVariableId.toString())
: undefined
const variableLink = (
<a
href={`/admin/variables/${parentVariableId}`}
target="_blank"
rel="noopener"
>
{column?.name ?? parentVariableId}
</a>
)
return (
<div>
<Section name="Config">
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(patchConfig)}
/>
<button
className="btn btn-primary mt-2"
onClick={this.copyYamlToClipboard}
>
Copy YAML for ETL
</button>
</Section>
{parentVariableId && (
<>
<Section name="Parent indicator">
{isInheritanceEnabled ? (
<p>
This chart is configured to inherit settings
from its parent indicator, {variableLink}.
{!parentConfig && (
<>
{" "}
But the parent indicator does not
yet have an associated Grapher
config. You can{" "}
<a
href={`/admin/variables/${parentVariableId}/config`}
target="_blank"
rel="noopener"
>
create one in the admin.
</a>
</>
)}
</p>
) : (
<p>
This chart may inherit chart settings from
the indicator {variableLink}, but
inheritance is currently disabled. Toggle
the option below to enable inheritance.
</p>
)}
<Toggle
label="Enable inheritance"
value={!!isInheritanceEnabled}
onValue={this.onToggleInheritance}
/>
</Section>
{parentConfig && (
<Section
name={
isInheritanceEnabled
? "Parent config"
: "Parent config (not currently applied)"
}
>
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(parentConfig)}
/>
<p className="mt-2">
<a
href={`/admin/variables/${parentVariableId}/config`}
target="_blank"
rel="noopener"
>
Edit parent config in the admin
</a>
</p>
</Section>
)}
</>
)}
<Section name="Full Config">
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(fullConfig)}
/>
</Section>
</div>
)
}
}
@observer
class EditorDebugTabForIndicatorChart extends React.Component<{
editor: IndicatorChartEditor
}> {
render() {
const { patchConfig, parentConfig, fullConfig } = this.props.editor
return (
<div className="ConfigTab">
<Section name="Config">
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(patchConfig)}
/>
</Section>
{parentConfig && (
<>
<Section name="Parent config (authored in the ETL)">
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(parentConfig)}
/>
</Section>
<Section name="Merged config">
<textarea
rows={7}
readOnly
className="form-control"
value={YAML.stringify(fullConfig)}
/>
</Section>
</>
)}
</div>
)
}
}