-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
DatasetsIndexPage.tsx
119 lines (104 loc) · 3.72 KB
/
DatasetsIndexPage.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
import React from "react"
import { observer } from "mobx-react"
import { observable, computed, action, runInAction } from "mobx"
import * as lodash from "lodash"
import { AdminLayout } from "./AdminLayout.js"
import { SearchField, FieldsRow } from "./Forms.js"
import { DatasetList, DatasetListItem } from "./DatasetList.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import {
buildSearchWordsFromSearchString,
filterFunctionForSearchWords,
highlightFunctionForSearchWords,
SearchWord,
} from "../adminShared/search.js"
@observer
export class DatasetsIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable datasets: DatasetListItem[] = []
@observable maxVisibleRows = 50
@observable searchInput?: string
@computed get searchWords(): SearchWord[] {
const { searchInput } = this
return buildSearchWordsFromSearchString(searchInput)
}
@computed get allDatasetsToShow(): DatasetListItem[] {
const { searchWords, datasets, maxVisibleRows } = this
if (searchWords.length > 0) {
const filterFn = filterFunctionForSearchWords(
searchWords,
(dataset: DatasetListItem) => [
dataset.name,
dataset.shortName,
...dataset.tags.map((t) => t.name),
dataset.namespace,
dataset.dataEditedByUserName,
dataset.description,
]
)
return datasets.filter(filterFn)
} else {
return this.datasets.slice(0, maxVisibleRows)
}
}
@computed get datasetsToShow(): DatasetListItem[] {
return this.allDatasetsToShow.slice(0, this.maxVisibleRows)
}
@computed get namespaces() {
return lodash.uniq(this.datasets.map((d) => d.namespace))
}
@computed get numTotalRows(): number {
return this.datasets.length
}
@action.bound onSearchInput(input: string) {
this.searchInput = input
}
@action.bound onShowMore() {
this.maxVisibleRows += 100
}
render() {
const { datasetsToShow, searchInput, numTotalRows } = this
const highlight = highlightFunctionForSearchWords(this.searchWords)
return (
<AdminLayout title="Datasets">
<main className="DatasetsIndexPage">
<FieldsRow>
<span>
Showing {datasetsToShow.length} of {numTotalRows}{" "}
datasets
</span>
<SearchField
placeholder="Search all datasets..."
value={searchInput}
onValue={this.onSearchInput}
autofocus
/>
</FieldsRow>
<DatasetList
datasets={datasetsToShow}
searchHighlight={highlight}
/>
{!searchInput && (
<button
className="btn btn-secondary"
onClick={this.onShowMore}
>
Show more datasets...
</button>
)}
</main>
</AdminLayout>
)
}
async getData() {
const { admin } = this.context
const json = await admin.getJSON("/api/datasets.json")
runInAction(() => {
this.datasets = json.datasets
})
}
componentDidMount() {
void this.getData()
}
}