-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathEditorHistoryTab.tsx
126 lines (118 loc) · 3.78 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
import { Component } from "react"
import { observer } from "mobx-react"
import { ChartEditor, Log } from "./ChartEditor.js"
import { Timeago } from "./Forms.js"
import { computed, observable } from "mobx"
import { Modal } from "antd"
import ReactDiffViewer, { DiffMethod } from "react-diff-viewer-continued"
// The rule doesn't support class components in the same file.
// eslint-disable-next-line react-refresh/only-export-components
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 Component<{
log: Log
previousLog: Log | undefined
}> {
@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>
)}
</div>
</li>
)
}
}
@observer
export class EditorHistoryTab extends Component<{ editor: ChartEditor }> {
@computed get logs() {
return this.props.editor.logs || []
}
render() {
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
></LogRenderer>
</ul>
))}
</div>
)
}
}