-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
TagGraphPage.tsx
559 lines (516 loc) · 20 KB
/
TagGraphPage.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import React from "react"
import { observer } from "mobx-react"
import { observable, action, runInAction, toJS, computed } from "mobx"
import * as lodash from "lodash"
import { AdminLayout } from "./AdminLayout.js"
import {
MinimalTagWithIsTopic,
TagGraphNode,
TagGraphRoot,
createTagGraph,
} from "@ourworldindata/utils"
import { TagBadge } from "./TagBadge.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import {
DndContext,
DragEndEvent,
pointerWithin,
useDraggable,
useDroppable,
} from "@dnd-kit/core"
import cx from "classnames"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faGrip } from "@fortawesome/free-solid-svg-icons"
import { AutoComplete, Button, Popconfirm } from "antd"
import { FlatTagGraph, FlatTagGraphNode } from "@ourworldindata/types"
import { Link } from "react-router-dom"
function DraggableDroppable(props: {
id: string
children: React.ReactNode
className?: string
disableDroppable?: boolean
}) {
const drag = useDraggable({
id: props.id,
})
const { attributes, listeners, transform } = drag
const shouldDisableDroppable = !!transform || props.disableDroppable
const dragSetNodeRef = drag.setNodeRef
const drop = useDroppable({
id: props.id,
disabled: shouldDisableDroppable,
})
const isOver = drop.isOver
const dropSetNodeRef = drop.setNodeRef
return (
<div
{...attributes}
className={cx(props.className, {
[`${props.className}--dragging`]: !!transform,
[`${props.className}--hovering`]: !!isOver,
})}
id={props.id}
style={{
transform: transform
? `translate3d(${transform.x}px, ${transform.y}px, 0)`
: "unset",
}}
ref={(element: HTMLElement | null) => {
dragSetNodeRef(element)
dropSetNodeRef(element)
}}
>
{React.Children.map(props.children, (child) => {
if (React.isValidElement(child)) {
if (child.type === TagGraphNodeContainer) {
return React.cloneElement(child, {
...child.props,
// If this node is being dragged, prevent all its children from being valid drop targets
disableDroppable: shouldDisableDroppable,
})
}
}
return child
})}
<button className="grip-button" {...listeners}>
<FontAwesomeIcon icon={faGrip} />
</button>
</div>
)
}
@observer
class AddChildForm extends React.Component<{
tags: MinimalTagWithIsTopic[]
label: string
setChild: (parentId: number, childId: number) => void
parentId: number
}> {
@observable isAddingTag: boolean = false
@observable autocompleteValue: string = ""
render() {
if (!this.isAddingTag) {
return (
<Button
onClick={() => (this.isAddingTag = true)}
type="primary"
className="add-tag-button"
>
{this.props.label}
</Button>
)
}
return (
<form className="add-tag-form">
<AutoComplete
autoFocus
className="add-tag-input"
value={this.autocompleteValue}
onChange={(value) => (this.autocompleteValue = value)}
options={this.props.tags.map((tag) => ({
value: tag.name,
label: tag.name,
}))}
filterOption={(inputValue, option) => {
if (!option?.label) return false
return option.label
.toLowerCase()
.startsWith(inputValue.toLowerCase())
}}
/>
<Button
onClick={() => {
const tag = this.props.tags.find(
(t) => t.name === this.autocompleteValue
)
if (!tag) return
this.props.setChild(this.props.parentId, tag.id)
this.isAddingTag = false
this.autocompleteValue = ""
}}
disabled={
!this.props.tags
.map((t) => t.name)
.includes(this.autocompleteValue)
}
>
Add
</Button>
<Button onClick={() => (this.isAddingTag = false)}>
Cancel
</Button>
</form>
)
}
}
@observer
class TagGraphNodeContainer extends React.Component<{
node: TagGraphNode
parentId: number
setWeight: (parentId: number, childId: number, weight: number) => void
setChild: (parentId: number, childId: number) => void
removeNode: (parentId: number, childId: number) => void
tags: MinimalTagWithIsTopic[]
parentsById: Record<string, MinimalTagWithIsTopic[]>
disableDroppable?: boolean
}> {
constructor(props: any) {
super(props)
this.handleUpdateWeight = this.handleUpdateWeight.bind(this)
}
handleUpdateWeight(e: React.ChangeEvent<HTMLInputElement>) {
const weight = Number(e.target.value)
const parentId = this.props.parentId
const childId = this.props.node.id
this.props.setWeight(parentId, childId, weight)
}
// Coparents are the other parents of this node
// e.g.
// Health -> Obesity
// Food and Agriculture -> Diet -> Obesity
// If we're on the Health -> Obesity node, coparents = [Diet]
@computed get coparents() {
return (this.props.parentsById[this.props.node.id] || []).filter(
(tag) => tag.id !== this.props.parentId
)
}
get addableTags() {
return this.props.tags.filter((tag) => {
const isDuplicate = tag.id === this.props.node.id
const isParent = this.props.node.path.includes(tag.id)
const isChild = this.props.node.children.find(
(c) => c.name === tag.name
)
const isCoparent = this.coparents.find((t) => t.id === tag.id)
return !isDuplicate && !isParent && !isChild && !isCoparent
})
}
render() {
const { id, name, path, children, weight, isTopic } = this.props.node
const serializedPath = path.join("-")
return (
// IDs can't start with a number, so we prefix with "node-"
// Not using data- attributes because they don't work with DndContext
<DraggableDroppable
key={id}
id={`node-${serializedPath}`}
className="tag-box"
disableDroppable={this.props.disableDroppable}
>
<TagBadge tag={{ id, name }} />
{isTopic ? (
<span
className="tag-box__is-topic"
title="This tag has a topic page"
>
⭐️
</span>
) : null}
{this.coparents.length > 0 ? (
<p>
This tag is also a child of:{" "}
{this.coparents.map((tag) => tag.name).join(", ")}
</p>
) : null}
<div className="tag-box__controls-container">
<span className="tag-box__weight-control">
<label htmlFor={`weight-${serializedPath}`}>
Weight
</label>
<input
id={`weight-${serializedPath}`}
type="number"
value={weight}
onChange={this.handleUpdateWeight}
/>
</span>
<AddChildForm
label="Add child"
parentId={id}
setChild={this.props.setChild}
tags={this.addableTags}
/>
<Popconfirm
title={
<div>
<strong>
Are you sure you want to remove this tag
relationship?
</strong>
<div>
Child tag relationships will also be removed
unless they are referenced elsewhere.
</div>
</div>
}
onConfirm={() =>
this.props.removeNode(path.at(-2)!, id)
}
okText="Yes"
cancelText="No"
>
<Button danger className="tag-box__delete-tag-button">
Remove
</Button>
</Popconfirm>
</div>
{children.map((node) => (
<TagGraphNodeContainer
key={node.id}
node={node}
tags={this.props.tags}
parentId={id}
parentsById={this.props.parentsById}
setWeight={this.props.setWeight}
removeNode={this.props.removeNode}
setChild={this.props.setChild}
disableDroppable={this.props.disableDroppable}
/>
))}
</DraggableDroppable>
)
}
}
function sortByWeightThenName(a: FlatTagGraphNode, b: FlatTagGraphNode) {
return b.weight - a.weight || a.name.localeCompare(b.name)
}
// "node-1-2-3" -> [1, 2, 3]
function getPath(elementId?: string): number[] {
if (!elementId) return []
return elementId.split("-").slice(1).map(Number)
}
function insertChildAndSort(
children: FlatTagGraphNode[] = [],
newNode: FlatTagGraphNode
): FlatTagGraphNode[] {
return [...children, newNode].sort(sortByWeightThenName)
}
@observer
export class TagGraphPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
constructor(props: Readonly<unknown>) {
super(props)
this.handleDragEnd = this.handleDragEnd.bind(this)
this.setWeight = this.setWeight.bind(this)
this.setChild = this.setChild.bind(this)
this.removeNode = this.removeNode.bind(this)
}
@observable flatTagGraph: FlatTagGraph = {}
@observable rootId: number | null = null
@observable addTagParentId?: number
@observable tags: MinimalTagWithIsTopic[] = []
@computed get tagGraph(): TagGraphRoot | null {
if (!this.rootId) return null
return createTagGraph(this.flatTagGraph, this.rootId)
}
@computed get parentsById(): Record<string, MinimalTagWithIsTopic[]> {
if (!this.tagGraph) return {}
const parentsById: Record<string, MinimalTagWithIsTopic[]> = {}
for (const [parentId, children] of Object.entries(this.flatTagGraph)) {
for (const child of children) {
const parent = this.tags.find(
(tag) => tag.id === Number(parentId)
)
if (parent) {
if (!parentsById[child.childId]) {
parentsById[child.childId] = [parent]
} else {
parentsById[child.childId].push(parent)
}
}
}
}
return parentsById
}
@computed get nonAreaTags() {
if (!this.rootId) return []
const areaTags = this.flatTagGraph[this.rootId]
const areaTagIds = areaTags?.map((tag) => tag.childId) || []
return this.tags.filter((tag) => !areaTagIds.includes(tag.id))
}
getAllChildrenOfNode(childId: number): FlatTagGraphNode[] {
const allChildren: FlatTagGraphNode[] =
toJS(this.flatTagGraph[childId]) || []
for (const child of allChildren) {
if (this.flatTagGraph[child.childId]) {
allChildren.push(...this.flatTagGraph[child.childId])
}
}
return allChildren
}
@action.bound setWeight(parentId: number, childId: number, weight: number) {
const parent = this.flatTagGraph[parentId]
if (!parent) return
const child = parent.find((node) => node.childId === childId)
if (!child) return
child.weight = weight
this.flatTagGraph[parentId] = parent.sort(sortByWeightThenName)
}
@action.bound setChild(parentId: number, childId: number) {
const siblings = this.flatTagGraph[parentId]
const tag = this.tags.find((tag) => tag.id === childId)
if (!tag) return
const child: FlatTagGraphNode = {
childId: tag.id,
parentId: parentId,
name: tag.name,
weight: 100,
slug: tag.slug,
isTopic: tag.isTopic,
}
if (siblings) {
this.flatTagGraph[parentId] = insertChildAndSort(siblings, child)
} else {
this.flatTagGraph[parentId] = [child]
}
}
@action.bound removeNode(parentId: number, childId: number) {
const children = this.flatTagGraph[parentId]
if (!children) return
// Remove the child from its parent. If there are no other children, delete the record
const remainingChildren = this.flatTagGraph[parentId].filter(
(node) => node.childId !== childId
)
if (remainingChildren.length) {
this.flatTagGraph[parentId] = remainingChildren
} else {
delete this.flatTagGraph[parentId]
}
// If the child had no other parents, delete its grandchildren also
// i.e. if you delete the "Health" area, the subgraph (Diseases -> Cardiovascular Disease, Cancer) will also be deleted,
// but the subgraph (Air Pollution -> Indoor Air Pollution, Outdoor Air Pollution) won't be, because it's still a child of "Energy and Environment"
const hasMultipleParents = this.parentsById[childId]
if (!hasMultipleParents) return
const grandchildren = this.flatTagGraph[childId]
if (!grandchildren) return
for (const grandchild of grandchildren) {
this.removeNode(childId, grandchild.childId)
}
}
@action.bound handleDragEnd(event: DragEndEvent) {
if (!this.tagGraph) return
const activeHtmlId = event.active.id as string
const overHtmlId = event.over?.id as string
if (!activeHtmlId || !overHtmlId) return
const childPath = getPath(activeHtmlId)
const newParentPath = getPath(overHtmlId)
const previousParentPath = childPath.slice(0, -1)
if (!childPath || !newParentPath || !previousParentPath) return
const childId = childPath.at(-1)
const newParentId = newParentPath.at(-1)
const previousParentId = previousParentPath.at(-1)
if (!childId || !previousParentId || !newParentId) return
const childNode = this.flatTagGraph[previousParentId].find(
(node) => node.childId === childId
)
if (!childNode) return
const isNoop = lodash.isEqual(previousParentPath, newParentPath)
if (isNoop) return
// Prevents these two structures:
// Parantheses indicate the subgraph that was dagged
// Energy -> (Energy)
// Nuclear Energy -> (Energy -> Nuclear Energy)
const allChildrenOfChild = this.getAllChildrenOfNode(childId)
const isCyclical = [childNode, ...allChildrenOfChild].find((child) =>
newParentPath.includes(child.childId)
)
if (isCyclical) {
alert("This operation would create a cycle")
return
}
const isSibling = this.flatTagGraph[newParentId]?.find(
(node) => node.childId === childId
)
if (isSibling) {
alert("This parent already has this child")
return
}
// Add child to new parent
childNode.parentId = newParentId
this.flatTagGraph[newParentId] = insertChildAndSort(
this.flatTagGraph[newParentId],
childNode
)
// Remove child from previous parent
this.flatTagGraph[previousParentId] = this.flatTagGraph[
previousParentId
].filter((node) => node.childId !== childId)
}
@action.bound async saveTagGraph() {
if (!this.tagGraph) return
await this.context.admin.requestJSON(
"/api/tagGraph",
{ tagGraph: toJS(this.flatTagGraph) },
"POST"
)
}
render() {
return (
<AdminLayout title="Tag Graph">
<main className="TagGraphPage">
<header className="page-header">
<h2>Tag Graph</h2>
<div>
<AddChildForm
tags={this.nonAreaTags}
parentId={this.rootId!}
setChild={this.setChild}
label="Add area"
/>
<Button type="primary" onClick={this.saveTagGraph}>
Save
</Button>
</div>
</header>
<p>
Drag and drop tags according to their hierarchy. Top
level tags should be areas. Tags are ordered by weight
(higher weights first) and then by name. To add a new
tag, visit the <Link to="/tags">tags page</Link>.
</p>
<DndContext
onDragEnd={this.handleDragEnd}
collisionDetection={pointerWithin}
>
<DraggableDroppable
id={`node-${this.rootId}`}
className="root-tag-box"
>
{this.tagGraph?.children.map((node) => (
<TagGraphNodeContainer
key={node.id}
node={node}
tags={this.tags}
parentsById={this.parentsById}
parentId={this.tagGraph!.id}
setWeight={this.setWeight}
setChild={this.setChild}
removeNode={this.removeNode}
/>
))}
</DraggableDroppable>
</DndContext>
</main>
</AdminLayout>
)
}
async getData() {
const flatTagGraph = await this.context.admin.getJSON<
FlatTagGraph & {
__rootId: number
}
>("/api/flatTagGraph.json")
const tags = await this.context.admin
.getJSON<{ tags: MinimalTagWithIsTopic[] }>("/api/tags.json")
.then((data) => data.tags)
runInAction(() => {
const { __rootId, ...rest } = flatTagGraph
this.rootId = __rootId
this.flatTagGraph = rest
this.tags = tags
})
}
componentDidMount() {
void this.getData()
}
}