forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditableTags.tsx
210 lines (183 loc) · 7.07 KB
/
EditableTags.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
import React from "react"
import * as lodash from "lodash"
import { observable, action } from "mobx"
import { observer } from "mobx-react"
import {
KeyChartLevel,
TaggableType,
DbChartTagJoin,
} from "@ourworldindata/utils"
import { TagBadge } from "./TagBadge.js"
import { EditTags } from "./EditTags.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { faEdit, faWandMagicSparkles } from "@fortawesome/free-solid-svg-icons"
interface TaggableItem {
id?: number
type: TaggableType
}
@observer
export class EditableTags extends React.Component<{
tags: DbChartTagJoin[]
suggestions: DbChartTagJoin[]
onSave: (tags: DbChartTagJoin[]) => void
disabled?: boolean
hasKeyChartSupport?: boolean
hasSuggestionsSupport?: boolean
taggable?: TaggableItem
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable isEditing: boolean = false
base: React.RefObject<HTMLDivElement> = React.createRef()
@observable tags: DbChartTagJoin[] = lodash.clone(this.props.tags)
@action.bound onAddTag(tag: DbChartTagJoin) {
this.tags.push(tag)
this.tags = lodash
// we only want to keep one occurrence of the same tag, whether
// entered manually or suggested through GPT. In case GPT suggests a
// tag that is already in the list, we want to keep the first one to
// preserve its status and key chart level
.uniqBy(this.tags, (t) => t.id)
.filter(filterUncategorizedTag)
this.ensureUncategorized()
}
@action.bound onRemoveTag(index: number) {
this.tags.splice(index, 1)
this.ensureUncategorized()
}
@action.bound onToggleKey(index: number) {
const currentKeyChartLevel =
this.tags[index].keyChartLevel || KeyChartLevel.None
// We cycle through 4 states of key chart levels for a given topic / chart combination
this.tags[index].keyChartLevel =
currentKeyChartLevel === KeyChartLevel.None
? KeyChartLevel.Top
: currentKeyChartLevel - 1
this.props.onSave(this.tags.filter(filterUncategorizedTag))
}
@action.bound ensureUncategorized() {
if (this.tags.length === 0) {
const uncategorized = this.props.suggestions.find(
(t) => t.name === "Uncategorized"
)
if (uncategorized) this.tags.push(uncategorized)
}
}
@action.bound onToggleEdit() {
if (this.isEditing) {
this.props.onSave(
this.tags
.filter(filterUncategorizedTag)
.map(setDefaultKeyChartLevel)
.map(setTagStatusToApprovedIfUnset)
)
}
this.isEditing = !this.isEditing
}
@action.bound async onSuggest() {
const { taggable } = this.props
if (!taggable?.id) return
const json: Record<"topics", DbChartTagJoin[]> =
await this.context.admin.getJSON(
`/api/gpt/suggest-topics/${taggable.type}/${taggable.id}.json`
)
if (!json?.topics?.length) return
json.topics
.map(setDefaultKeyChartLevel)
.map(setTagStatusToPending)
.forEach((tag) => {
this.onAddTag(tag)
})
this.props.onSave(this.tags.filter(filterUncategorizedTag))
}
@action.bound onApprove(index: number) {
this.tags[index].isApproved = true
this.props.onSave(this.tags.filter(filterUncategorizedTag))
}
componentDidMount() {
this.componentDidUpdate()
}
componentDidUpdate() {
this.ensureUncategorized()
}
render() {
const { disabled, hasKeyChartSupport, hasSuggestionsSupport } =
this.props
const { tags } = this
return (
<div className="EditableTags">
{this.isEditing ? (
<EditTags
tags={this.tags}
onAdd={this.onAddTag}
onDelete={this.onRemoveTag}
onSave={this.onToggleEdit}
suggestions={this.props.suggestions}
/>
) : (
<div>
{tags.map((t, i) => (
<TagBadge
onToggleKey={
hasKeyChartSupport &&
filterUncategorizedTag(t) &&
filterUnlistedTag(t)
? () => this.onToggleKey(i)
: undefined
}
onApprove={
hasSuggestionsSupport &&
filterUncategorizedTag(t)
? () => this.onApprove(i)
: undefined
}
key={t.id}
tag={t}
/>
))}
{!disabled && (
<>
{hasSuggestionsSupport && (
<button
className="btn btn-link EditableTags__action"
onClick={this.onSuggest}
>
<FontAwesomeIcon
icon={faWandMagicSparkles}
/>
Suggest
</button>
)}
<button
className="btn btn-link EditableTags__action"
onClick={(e) => {
this.onToggleEdit()
e.stopPropagation()
}}
>
<FontAwesomeIcon icon={faEdit} />
Edit
</button>
</>
)}
</div>
)}
</div>
)
}
}
const filterUncategorizedTag = (t: DbChartTagJoin) => t.name !== "Uncategorized"
const filterUnlistedTag = (t: DbChartTagJoin) => t.name !== "Unlisted"
const setDefaultKeyChartLevel = (t: DbChartTagJoin) => {
if (t.keyChartLevel === undefined) t.keyChartLevel = KeyChartLevel.None
return t
}
const setTagStatusToPending = (t: DbChartTagJoin) => {
t.isApproved = false
return t
}
const setTagStatusToApprovedIfUnset = (t: DbChartTagJoin) => {
if (t.isApproved === undefined) t.isApproved = true
return t
}