forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorHistoryTab.tsx
183 lines (173 loc) · 5.9 KB
/
EditorHistoryTab.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
import React from "react"
import { observer } from "mobx-react"
import { ChartEditor, Log } from "./ChartEditor.js"
import { Section, Timeago } from "./Forms.js"
import { computed, action, observable } from "mobx"
import { copyToClipboard } from "@ourworldindata/utils"
import YAML from "yaml"
import { notification, Modal } from "antd"
import ReactDiffViewer, { DiffMethod } from "react-diff-viewer"
function LogCompareModal({
log,
previousLog,
isOpen,
onClose,
}: {
log: Log
previousLog: Log
isOpen: boolean
onClose: () => void
}) {
const titleForLog = (log: Log) => {
const user = log.userName || log.userId.toString()
return <Timeago time={log.createdAt} by={user} />
}
return (
<Modal
open={isOpen}
centered
width="80vw"
onOk={onClose}
onCancel={onClose}
cancelButtonProps={{ style: { display: "none" } }}
>
<div style={{ maxHeight: "50vh", overflowY: "auto" }}>
<ReactDiffViewer
newValue={JSON.stringify(log.config, null, 2)}
oldValue={JSON.stringify(previousLog.config, null, 2)}
leftTitle={titleForLog(previousLog)}
rightTitle={titleForLog(log)}
compareMethod={DiffMethod.WORDS_WITH_SPACE}
styles={{
contentText: {
wordBreak: "break-word",
},
}}
extraLinesSurroundingDiff={2}
/>
</div>
</Modal>
)
}
@observer
class LogRenderer extends React.Component<{
log: Log
previousLog: Log | undefined
applyConfig: (config: any) => void
}> {
@observable isCompareModalOpen = false
@computed get title() {
const { log } = this.props
const user = log.userName || log.userId.toString()
return (
<>
Saved <Timeago time={log.createdAt} by={user} />
</>
)
}
render() {
const { log } = this.props
const { title } = this
const hasCompareButton = !!this.props.previousLog
return (
<li
className="list-group-item d-flex justify-content-between"
style={{ alignItems: "center" }}
>
{hasCompareButton && (
<LogCompareModal
log={log}
previousLog={this.props.previousLog}
isOpen={this.isCompareModalOpen}
onClose={() => (this.isCompareModalOpen = false)}
/>
)}
<span>{title}</span>
<div className="d-flex" style={{ gap: 6 }}>
{hasCompareButton && (
<button
className="btn btn-secondary"
onClick={() => (this.isCompareModalOpen = true)}
>
Compare <br /> to previous
</button>
)}
<button
className="btn btn-danger"
onClick={() => this.props.applyConfig(log.config)}
>
Restore
</button>
</div>
</li>
)
}
}
@observer
export class EditorHistoryTab extends React.Component<{ editor: ChartEditor }> {
@computed get logs() {
return this.props.editor.logs || []
}
@action.bound async applyConfig(config: any) {
const { grapher } = this.props.editor
const configJson = JSON.parse(config)
grapher.updateFromObject(configJson)
grapher.updateAuthoredVersion({
...grapher.toObject(),
data: configJson.data,
})
grapher.rebuildInputOwidTable()
}
@action.bound copyYamlToClipboard() {
// Use the Clipboard API to copy the config into the users clipboard
const chartConfigObject = {
...this.props.editor.grapher.object,
}
delete chartConfigObject.id
delete chartConfigObject.dimensions
delete chartConfigObject.version
delete chartConfigObject.isPublished
const chartConfigAsYaml = YAML.stringify(chartConfigObject)
void copyToClipboard(chartConfigAsYaml)
notification["success"]({
message: "Copied YAML to clipboard",
description: "You can now paste this into the ETL",
placement: "bottomRight",
closeIcon: <></>,
})
}
render() {
// Avoid modifying the original JSON object
// Due to mobx memoizing computed values, the JSON can be mutated.
const chartConfigObject = {
...this.props.editor.grapher.object,
}
return (
<div>
{this.logs.map((log, i) => (
<ul key={i} className="list-group">
<LogRenderer
log={log}
previousLog={this.logs[i + 1]} // Needed for comparison, might be undefined
applyConfig={this.applyConfig}
></LogRenderer>
</ul>
))}
<Section name="Debug Version">
<button
className="btn btn-primary"
onClick={this.copyYamlToClipboard}
>
Copy YAML for ETL
</button>
<textarea
rows={7}
readOnly
className="form-control"
value={JSON.stringify(chartConfigObject, undefined, 2)}
/>
</Section>
</div>
)
}
}