forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartIndexPage.tsx
138 lines (123 loc) · 4.44 KB
/
ChartIndexPage.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
import React from "react"
import { observer } from "mobx-react"
import { observable, computed, action, runInAction } from "mobx"
import { TextField } from "./Forms.js"
import { AdminLayout } from "./AdminLayout.js"
import { ChartList, ChartListItem } from "./ChartList.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import {
buildSearchWordsFromSearchString,
filterFunctionForSearchWords,
highlightFunctionForSearchWords,
SearchWord,
} from "../adminShared/search.js"
@observer
export class ChartIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable searchInput?: string
@observable maxVisibleCharts = 50
@observable charts: ChartListItem[] = []
@computed get searchWords(): SearchWord[] {
const { searchInput } = this
return buildSearchWordsFromSearchString(searchInput)
}
@computed get numTotalCharts() {
return this.charts.length
}
@computed get allChartsToShow(): ChartListItem[] {
const { searchWords, charts } = this
if (searchWords.length > 0) {
const filterFn = filterFunctionForSearchWords(
searchWords,
(chart: ChartListItem) => [
chart.title,
chart.variantName,
chart.internalNotes,
chart.publishedBy,
chart.lastEditedBy,
`${chart.id}`,
chart.slug,
chart.hasChartTab !== false ? chart.type : undefined,
chart.hasMapTab ? "Map" : undefined,
...chart.tags.map((tag) => tag.name),
]
)
return charts.filter(filterFn)
} else {
return this.charts
}
}
@computed get chartsToShow(): ChartListItem[] {
return this.allChartsToShow.slice(0, this.maxVisibleCharts)
}
@action.bound onSearchInput(input: string) {
this.searchInput = input
this.setSearchInputInUrl(input)
}
@action.bound onShowMore() {
this.maxVisibleCharts += 100
}
render() {
const { chartsToShow, searchInput, numTotalCharts } = this
const highlight = highlightFunctionForSearchWords(this.searchWords)
return (
<AdminLayout title="Charts">
<main className="ChartIndexPage">
<div className="topRow">
<span>
Showing {chartsToShow.length} of {numTotalCharts}{" "}
charts
</span>
<TextField
placeholder="Search all charts..."
value={searchInput}
onValue={this.onSearchInput}
autofocus
/>
</div>
<ChartList
charts={chartsToShow}
searchHighlight={highlight}
onDelete={action((c: ChartListItem) =>
this.charts.splice(this.charts.indexOf(c), 1)
)}
/>
{!searchInput && (
<button
className="btn btn-secondary"
onClick={this.onShowMore}
>
Show more charts...
</button>
)}
</main>
</AdminLayout>
)
}
async getData() {
const { admin } = this.context
const json = await admin.getJSON("/api/charts.json")
runInAction(() => {
this.charts = json.charts
})
}
componentDidMount() {
this.searchInput = this.getSearchInputFromUrl()
void this.getData()
}
getSearchInputFromUrl(): string {
const params = new URLSearchParams(window.location.search)
return params.get("search") || ""
}
setSearchInputInUrl(searchInput: string) {
const params = new URLSearchParams(window.location.search)
if (searchInput) {
params.set("search", searchInput)
} else {
params.delete("search")
}
const newUrl = `${window.location.pathname}?${params.toString()}`
window.history.replaceState({}, "", newUrl)
}
}