-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathDataPageV2Content.tsx
191 lines (183 loc) · 8.21 KB
/
DataPageV2Content.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
import { useState, useEffect, useMemo } from "react"
import { Grapher, GrapherProgrammaticInterface } from "@ourworldindata/grapher"
import {
REUSE_THIS_WORK_SECTION_ID,
DATAPAGE_SOURCES_AND_PROCESSING_SECTION_ID,
} from "@ourworldindata/components"
import { GrapherWithFallback } from "./GrapherWithFallback.js"
import { RelatedCharts } from "./blocks/RelatedCharts.js"
import {
DataPageV2ContentFields,
GrapherInterface,
joinTitleFragments,
ImageMetadata,
} from "@ourworldindata/utils"
import { DocumentContext } from "./gdocs/DocumentContext.js"
import { AttachmentsContext } from "./gdocs/AttachmentsContext.js"
import StickyNav from "./blocks/StickyNav.js"
import AboutThisData from "./AboutThisData.js"
import DataPageResearchAndWriting from "./DataPageResearchAndWriting.js"
import MetadataSection from "./MetadataSection.js"
import TopicTags from "./TopicTags.js"
import { processRelatedResearch } from "./dataPage.js"
declare global {
interface Window {
_OWID_DATAPAGEV2_PROPS: DataPageV2ContentFields
_OWID_GRAPHER_CONFIG: GrapherInterface
}
}
export const OWID_DATAPAGE_CONTENT_ROOT_ID = "owid-datapageJson-root"
export const DataPageV2Content = ({
datapageData,
grapherConfig,
isPreviewing = false,
faqEntries,
canonicalUrl = "{URL}", // when we bake pages to their proper url this will be set correctly but on preview pages we leave this undefined
tagToSlugMap,
imageMetadata,
}: DataPageV2ContentFields & {
grapherConfig: GrapherInterface
imageMetadata: Record<string, ImageMetadata>
}) => {
const [grapher, setGrapher] = useState<Grapher | undefined>(undefined)
const titleFragments = joinTitleFragments(
datapageData.attributionShort,
datapageData.titleVariant
)
// Initialize the grapher for client-side rendering
const mergedGrapherConfig: GrapherProgrammaticInterface = useMemo(
() => ({
...grapherConfig,
isEmbeddedInADataPage: true,
bindUrlToWindow: true,
}),
[grapherConfig]
)
useEffect(() => {
setGrapher(new Grapher(mergedGrapherConfig))
}, [mergedGrapherConfig])
const stickyNavLinks = [
{
text: "Explore the Data",
target: "#explore-the-data",
},
{
text: "Research & Writing",
target: "#research-and-writing",
},
{ text: "All Charts", target: "#all-charts" },
{ text: "FAQs", target: "#faqs" },
{
text: "Sources & Processing",
target: "#" + DATAPAGE_SOURCES_AND_PROCESSING_SECTION_ID,
},
{ text: "Reuse This Work", target: "#" + REUSE_THIS_WORK_SECTION_ID },
]
const relatedResearch = processRelatedResearch(
datapageData.relatedResearch,
datapageData.topicTagsLinks ?? []
)
return (
<AttachmentsContext.Provider
value={{
linkedDocuments: {},
imageMetadata,
linkedCharts: {},
linkedIndicators: {},
relatedCharts: [],
}}
>
<DocumentContext.Provider value={{ isPreviewing }}>
<div className="DataPageContent__grapher-for-embed">
<GrapherWithFallback
grapher={grapher}
slug={grapherConfig.slug}
/>
</div>
<div className="DataPageContent grid grid-cols-12-full-width">
<div className="bg-blue-10 span-cols-14">
<div className="header__wrapper grid grid-cols-12-full-width">
<div className="header__left col-start-2 span-cols-8 col-sm-start-2 span-sm-cols-12">
<div className="header__supertitle">Data</div>
<h1 className="header__title">
{datapageData.title.title}
</h1>
<div className="header__source">
{titleFragments}
</div>
</div>
{!!datapageData.topicTagsLinks?.length && (
<TopicTags
className="header__right col-start-10 span-cols-4 col-sm-start-2 span-sm-cols-12"
topicTagsLinks={datapageData.topicTagsLinks}
tagToSlugMap={tagToSlugMap}
/>
)}
</div>
</div>
<nav className="sticky-nav sticky-nav--dark span-cols-14 grid grid-cols-12-full-width">
<StickyNav
className="span-cols-12 col-start-2"
links={stickyNavLinks}
/>
</nav>
<div className="span-cols-14 grid grid-cols-12-full-width full-width--border">
<div className="chart-key-info col-start-2 span-cols-12">
<GrapherWithFallback
grapher={grapher}
slug={grapherConfig.slug} // TODO: On grapher pages,
// there will always be a slug, but if we just show a data page preview for an indicator in the admin, there will be no slug
// and then thumbnails will be broken for those. When we consider baking data pages for
// non-grapher pages then we need to make sure that there are thunbnails that are generated for the these non-chart graphers and
// then this piece will have to change anyhow and know how to provide the thumbnail.
id="explore-the-data"
/>
<AboutThisData
datapageData={datapageData}
hasFaq={!!faqEntries?.faqs.length}
/>
</div>
</div>
<div className="col-start-2 span-cols-12">
{relatedResearch && relatedResearch.length > 0 && (
<DataPageResearchAndWriting
relatedResearch={relatedResearch}
/>
)}
{datapageData.allCharts &&
datapageData.allCharts.length > 0 ? (
<div className="section-wrapper section-wrapper__related-charts">
<h2
className="related-charts__title"
id="all-charts"
>
Explore charts that include this data
</h2>
<div>
<RelatedCharts
charts={datapageData.allCharts}
/>
</div>
</div>
) : null}
</div>
<MetadataSection
attributionShort={datapageData.attributionShort}
attributions={datapageData.attributions}
canonicalUrl={canonicalUrl}
descriptionProcessing={
datapageData.descriptionProcessing
}
faqEntries={faqEntries}
origins={datapageData.origins}
owidProcessingLevel={datapageData.owidProcessingLevel}
primaryTopic={datapageData.primaryTopic}
source={datapageData.source}
title={datapageData.title}
titleVariant={datapageData.titleVariant}
/>
</div>
</DocumentContext.Provider>
</AttachmentsContext.Provider>
)
}