forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGdocsIndexPage.tsx
376 lines (353 loc) · 14.9 KB
/
GdocsIndexPage.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
import React from "react"
import cx from "classnames"
import { AdminLayout } from "./AdminLayout.js"
import { Modal, SearchField } from "./Forms.js"
import { EditableTags } from "./EditableTags.js"
import {
faCirclePlus,
faHouse,
faLightbulb,
faNewspaper,
faPuzzlePiece,
faQuestion,
faThList,
faBuildingNgo,
faUserPen,
} from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import {
DbChartTagJoin,
OwidGdocType,
spansToUnformattedPlainText,
checkIsGdocPost,
OwidGdocIndexItem,
} from "@ourworldindata/utils"
import {
buildSearchWordsFromSearchString,
filterFunctionForSearchWords,
SearchWord,
} from "../adminShared/search.js"
import { Route, RouteComponentProps } from "react-router-dom"
import { Link } from "./Link.js"
import { GdocsAdd } from "./GdocsAdd.js"
import { observer } from "mobx-react"
import { GdocsStoreContext } from "./GdocsStore.js"
import { computed, observable } from "mobx"
import { BAKED_BASE_URL } from "../settings/clientSettings.js"
import { GdocsEditLink } from "./GdocsEditLink.js"
const iconGdocTypeMap = {
[OwidGdocType.Fragment]: <FontAwesomeIcon icon={faPuzzlePiece} />,
[OwidGdocType.Article]: <FontAwesomeIcon icon={faNewspaper} />,
[OwidGdocType.TopicPage]: <FontAwesomeIcon icon={faLightbulb} />,
[OwidGdocType.LinearTopicPage]: <FontAwesomeIcon icon={faLightbulb} />,
[OwidGdocType.DataInsight]: <FontAwesomeIcon icon={faThList} />,
[OwidGdocType.Homepage]: <FontAwesomeIcon icon={faHouse} />,
[OwidGdocType.AboutPage]: <FontAwesomeIcon icon={faBuildingNgo} />,
[OwidGdocType.Author]: <FontAwesomeIcon icon={faUserPen} />,
}
enum GdocPublishStatus {
All = "all",
Published = "published",
Unpublished = "unpublished",
}
@observer
class GdocsIndexPageSearch extends React.Component<{
filters: GdocsSearchFilters
search: { value: string }
}> {
toggleGdocTypeFilter = (type: OwidGdocType) => {
this.props.filters[type] = !this.props.filters[type]
}
render() {
const owidGdocTypes: OwidGdocType[] = [
OwidGdocType.Fragment,
OwidGdocType.Article,
OwidGdocType.TopicPage,
OwidGdocType.LinearTopicPage,
OwidGdocType.DataInsight,
OwidGdocType.AboutPage,
OwidGdocType.Author,
]
return (
<div className="d-flex flex-grow-1 flex-wrap">
<SearchField
placeholder="Search by author, category, or title"
className="gdoc-index__search-bar"
value={this.props.search.value}
onValue={(value: string) =>
(this.props.search.value = value)
}
autofocus
/>
<div className="gdoc-index-filters">
<p>
<strong>Filter results by type</strong>
</p>
{owidGdocTypes.map((type) => {
const isChecked = this.props.filters[type]
return (
<label
key={type}
className="gdoc-index-filter-checkbox"
>
<input
type="checkbox"
id={`shouldShow${type}`}
checked={isChecked}
onChange={() =>
this.toggleGdocTypeFilter(type)
}
/>
{type}
</label>
)
})}
<label className="gdoc-index-filter-checkbox">
<select
id="shouldShowPublishedOnly"
onChange={({ target }) => {
const value = target.value as GdocPublishStatus
this.props.filters.publishStatus = value
}}
>
<option value={GdocPublishStatus.All}>All</option>
<option value={GdocPublishStatus.Published}>
Published
</option>
<option value={GdocPublishStatus.Unpublished}>
Unpublished
</option>
</select>
</label>
</div>
</div>
)
}
}
interface GdocsMatchParams {
id: string
}
export type GdocsMatchProps = RouteComponentProps<GdocsMatchParams>
type GdocsSearchFilters = Record<OwidGdocType, boolean> & {
publishStatus: GdocPublishStatus
}
@observer
export class GdocsIndexPage extends React.Component<GdocsMatchProps> {
static contextType = GdocsStoreContext
context!: React.ContextType<typeof GdocsStoreContext>
@observable filters: GdocsSearchFilters = {
[OwidGdocType.Fragment]: false,
[OwidGdocType.Article]: false,
[OwidGdocType.TopicPage]: false,
[OwidGdocType.LinearTopicPage]: false,
[OwidGdocType.DataInsight]: false,
[OwidGdocType.Homepage]: false,
[OwidGdocType.AboutPage]: false,
[OwidGdocType.Author]: false,
publishStatus: GdocPublishStatus.All,
}
@observable search = { value: "" }
@computed get searchWords(): SearchWord[] {
const { search } = this
return buildSearchWordsFromSearchString(search.value)
}
async componentDidMount(): Promise<void> {
await this.context?.fetchTags()
await this.context?.fetchGdocs()
}
@computed
get tags(): DbChartTagJoin[] {
return this.context?.availableTags || []
}
@computed get allGdocsToShow(): OwidGdocIndexItem[] {
const { searchWords, context } = this
if (!context) return []
// Don't filter unless at least one filter is active
const { publishStatus, ...typeFilters } = this.filters
const areAnyTypeFiltersActive = Object.values(typeFilters).some(
(isFilterActive) => isFilterActive
)
const shouldUseFilters =
areAnyTypeFiltersActive || publishStatus !== GdocPublishStatus.All
const filteredByType = shouldUseFilters
? context.gdocs.filter((gdoc) => {
const isPublished = gdoc.published
const shouldFilterByType =
// don't filter if no type filters are active
!areAnyTypeFiltersActive ||
// don't filter gdocs with no type
!gdoc.type ||
// if any type filter is active, and the gdoc has a type,
// show this document if its type is active
this.filters[gdoc.type]
switch (publishStatus) {
case GdocPublishStatus.All:
return shouldFilterByType
case GdocPublishStatus.Published:
return shouldFilterByType && isPublished
case GdocPublishStatus.Unpublished:
return shouldFilterByType && !isPublished
}
})
: context.gdocs
if (searchWords.length > 0) {
const filterFn = filterFunctionForSearchWords(
searchWords,
(gdoc: OwidGdocIndexItem) => {
const properties = [
gdoc.title,
gdoc.slug,
gdoc.authors?.join(" "),
gdoc.tags?.map(({ name }) => name).join(" "),
gdoc.id,
]
if (checkIsGdocPost(gdoc)) {
properties.push(
...[
gdoc.content.subtitle,
gdoc.content.summary
? spansToUnformattedPlainText(
gdoc.content.summary.flatMap(
(block) => block.value
)
)
: undefined,
]
)
}
return properties
}
)
return filteredByType.filter(filterFn)
} else {
return filteredByType
}
}
render() {
return (
<AdminLayout title="Google Docs">
<main>
<div className="d-flex justify-content-between mb-3">
<GdocsIndexPageSearch
filters={this.filters}
search={this.search}
/>
<div>
<a
className="btn btn-secondary gdoc-index__help-link"
target="_blank"
href="https://docs.google.com/document/d/1OLoTWloy4VecOjKTjB1wLV6tEphHJIMXfexrf1ZYJzU/edit"
rel="noopener"
>
<FontAwesomeIcon icon={faQuestion} /> Open
documentation
</a>
<button
className="btn btn-primary"
onClick={() =>
this.props.history.push(
`${this.props.match.path}/add`
)
}
>
<FontAwesomeIcon icon={faCirclePlus} /> Add
document
</button>
</div>
</div>
{this.allGdocsToShow.map((gdoc) => (
<div
key={gdoc.id}
className={cx(`gdoc-index-item`, {
[`gdoc-index-item__${gdoc.type}`]: gdoc.type,
})}
>
<div className="gdoc-index-item__content">
{gdoc.type ? (
<span
className="gdoc-index-item__type-icon"
title={gdoc.type}
>
{iconGdocTypeMap[gdoc.type]}
</span>
) : null}
<Link
to={`${this.props.match.path}/${gdoc.id}/preview`}
>
<h5
className="gdoc-index-item__title"
title="Preview article"
>
{gdoc.title || "Untitled"}
</h5>
</Link>
<GdocsEditLink gdocId={gdoc.id} />
<p className="gdoc-index-item__byline">
{gdoc.authors?.join(", ")}
</p>
<span className="gdoc-index-item__tags">
{gdoc.type &&
![
OwidGdocType.Fragment,
OwidGdocType.AboutPage,
].includes(gdoc.type) &&
gdoc.tags ? (
<EditableTags
tags={gdoc.tags}
onSave={(tags) =>
this.context?.updateTags(
gdoc,
tags as any
)
}
suggestions={this.tags}
/>
) : null}
</span>
</div>
<div className="gdoc-index-item__publish-status">
{gdoc.published ? (
<a
title={
gdoc.publishedAt
? new Date(
gdoc.publishedAt
).toDateString()
: undefined
}
href={
gdoc.type !== OwidGdocType.Fragment
? `${BAKED_BASE_URL}/${gdoc.slug}`
: undefined
}
className="gdoc-index-item__publish-link"
>
Published
</a>
) : null}
</div>
</div>
))}
<Route
path={`${this.props.match.path}/add`}
render={() => {
const onClose = () =>
this.props.history.push(this.props.match.path)
return (
<Modal onClose={onClose}>
<GdocsAdd
onAdd={(id: string) => {
this.props.history.push(
`${this.props.match.path}/${id}/preview`
)
}}
/>
</Modal>
)
}}
/>
</main>
</AdminLayout>
)
}
}