forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableList.tsx
132 lines (125 loc) · 4.56 KB
/
VariableList.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
import React from "react"
import { observer } from "mobx-react"
import { Link } from "./Link.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { Timeago } from "./Forms.js"
export interface VariableListItem {
id: number
name: string
namespace?: string
version?: string
dataset?: string
table?: string
shortName?: string
uploadedAt?: Date
uploadedBy?: string
isPrivate?: boolean
nonRedistributable?: boolean
}
@observer
class VariableRow extends React.Component<{
variable: VariableListItem
fields: string[]
searchHighlight?: (text: string) => string | React.ReactElement
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
render() {
const { variable, fields, searchHighlight } = this.props
return (
<tr>
<td>
{variable.nonRedistributable ? (
<span className="text-secondary">
Non-redistributable:{" "}
</span>
) : variable.isPrivate ? (
<span className="text-secondary">Unpublished: </span>
) : (
""
)}
<Link to={`/variables/${variable.id}`}>
{searchHighlight
? searchHighlight(variable.name)
: variable.name}
</Link>
</td>
{fields.includes("namespace") && <td>{variable.namespace}</td>}
{fields.includes("version") && <td>{variable.version}</td>}
{fields.includes("dataset") && <td>{variable.dataset}</td>}
{fields.includes("table") && (
<td>
{
// Some table are very long, truncate them
variable.table && variable.table!.length > 20
? variable.table!.substring(0, 20) + "..."
: variable.table
}
</td>
)}
{fields.includes("shortName") && (
<td>
{
// Some "short names" are very long, so truncate them
variable.shortName &&
variable.shortName!.length > 20
? variable.shortName!.substring(0, 20) + "..."
: variable.shortName
}
</td>
)}
{fields.includes("uploadedAt") && (
<td>
<Timeago
time={variable.uploadedAt}
by={variable.uploadedBy ?? "Bulk import"}
/>
</td>
)}
</tr>
)
}
}
@observer
export class VariableList extends React.Component<{
variables: VariableListItem[]
fields: string[]
searchHighlight?: (text: string) => string | React.ReactElement
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
render() {
const { props } = this
return (
<table className="table table-bordered">
<thead>
<tr>
<th>Name</th>
{props.fields.includes("namespace") && (
<th>Namespace</th>
)}
{props.fields.includes("version") && <th>Version</th>}
{props.fields.includes("dataset") && <th>Dataset</th>}
{props.fields.includes("table") && <th>Table</th>}
{props.fields.includes("shortName") && (
<th>Short name</th>
)}
{props.fields.includes("uploadedAt") && (
<th>Uploaded</th>
)}
</tr>
</thead>
<tbody>
{props.variables.map((variable) => (
<VariableRow
key={variable.id}
fields={this.props.fields}
variable={variable}
searchHighlight={props.searchHighlight}
/>
))}
</tbody>
</table>
)
}
}