-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathDataPageV2.tsx
191 lines (184 loc) · 6.67 KB
/
DataPageV2.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 {
getVariableDataRoute,
getVariableMetadataRoute,
GrapherProgrammaticInterface,
} from "@ourworldindata/grapher"
import {
uniq,
SiteFooterContext,
DataPageDataV2,
serializeJSONForHTML,
mergeGrapherConfigs,
compact,
FaqEntryData,
pick,
GrapherInterface,
ImageMetadata,
Url,
} from "@ourworldindata/utils"
import { MarkdownTextWrap } from "@ourworldindata/components"
import urljoin from "url-join"
import {
ADMIN_BASE_URL,
BAKED_GRAPHER_URL,
DATA_API_URL,
} from "../settings/clientSettings.js"
import {
DataPageV2Content,
OWID_DATAPAGE_CONTENT_ROOT_ID,
} from "./DataPageV2Content.js"
import { Head } from "./Head.js"
import { SiteFooter } from "./SiteFooter.js"
import { SiteHeader } from "./SiteHeader.js"
import { IFrameDetector } from "./IframeDetector.js"
import { DebugProvider } from "./gdocs/DebugProvider.js"
import { Html } from "./Html.js"
export const DataPageV2 = (props: {
grapher: GrapherInterface | undefined
datapageData: DataPageDataV2
baseUrl: string
baseGrapherUrl: string
isPreviewing: boolean
faqEntries?: FaqEntryData
imageMetadata: Record<string, ImageMetadata>
tagToSlugMap: Record<string | number, string>
}) => {
const {
grapher,
datapageData,
baseGrapherUrl,
baseUrl,
isPreviewing,
faqEntries,
tagToSlugMap,
imageMetadata,
} = props
const pageTitle = grapher?.title ?? datapageData.title.title
const canonicalUrl = grapher?.slug
? urljoin(baseGrapherUrl, grapher.slug as string)
: ""
const dataApiOrigin = Url.fromURL(DATA_API_URL).origin
let pageDesc: string
if (grapher?.subtitle?.length) {
// convert subtitle from markdown to plaintext
pageDesc = new MarkdownTextWrap({
text: grapher.subtitle,
fontSize: 10,
}).plaintext
} else pageDesc = "An interactive visualization from Our World in Data."
// Due to thumbnails not taking into account URL parameters, they are often inaccurate on
// social media. We decided to remove them and use a single thumbnail for all charts.
// See https://github.com/owid/owid-grapher/issues/1086
//
// const imageUrl = urljoin(
// baseGrapherUrl,
// "exports",
// `${grapher.slug}.png?v=${grapher.version}`
// )
const imageUrl: string = urljoin(baseUrl, "default-grapher-thumbnail.png")
const imageWidth = "1200"
const imageHeight = "628"
const variableIds: number[] = uniq(
compact(grapher?.dimensions?.map((d) => d.variableId))
)
const mergedGrapherConfig = mergeGrapherConfigs(
datapageData.chartConfig as GrapherInterface,
grapher ?? {}
)
// Note that we cannot set `bindUrlToWindow` and `isEmbeddedInADataPage` here,
// because this would then get serialized into the EMBEDDED_JSON object,
// and MultiEmbedder would then pick it up for other charts on the page
// _aside_ from the main one (e.g. the related charts block),
// which we don't want to happen.
const grapherConfig: GrapherProgrammaticInterface = {
...mergedGrapherConfig,
bakedGrapherURL: BAKED_GRAPHER_URL,
adminBaseUrl: ADMIN_BASE_URL,
dataApiUrl: DATA_API_URL,
}
// Only embed the tags that are actually used by the datapage, instead of the complete JSON object with ~240 properties
const minimalTagToSlugMap = pick(
tagToSlugMap,
datapageData.topicTagsLinks || []
)
return (
<Html>
<Head
canonicalUrl={canonicalUrl}
pageTitle={pageTitle}
pageDesc={pageDesc}
imageUrl={imageUrl}
baseUrl={baseUrl}
>
<meta property="og:image:width" content={imageWidth} />
<meta property="og:image:height" content={imageHeight} />
<IFrameDetector />
<link rel="preconnect" href={dataApiOrigin} />
{variableIds.flatMap((variableId) =>
[
getVariableDataRoute(DATA_API_URL, variableId),
getVariableMetadataRoute(DATA_API_URL, variableId),
].map((href) => (
<link
key={href}
rel="preload"
href={href}
as="fetch"
crossOrigin="anonymous"
/>
))
)}
<link
rel="preload"
href="/fonts/PlayfairDisplayLatin-SemiBold.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>
</Head>
<body className="DataPage">
<SiteHeader baseUrl={baseUrl} />
<main>
<script
dangerouslySetInnerHTML={{
__html: `window._OWID_DATAPAGEV2_PROPS = ${JSON.stringify(
{
datapageData,
faqEntries,
canonicalUrl,
tagToSlugMap: minimalTagToSlugMap,
imageMetadata,
}
)}`,
}}
/>
<div id={OWID_DATAPAGE_CONTENT_ROOT_ID}>
<DebugProvider debug={isPreviewing}>
<DataPageV2Content
datapageData={datapageData}
grapherConfig={grapherConfig}
imageMetadata={imageMetadata}
isPreviewing={isPreviewing}
faqEntries={faqEntries}
canonicalUrl={canonicalUrl}
tagToSlugMap={tagToSlugMap}
/>
</DebugProvider>
</div>
</main>
<SiteFooter
baseUrl={baseUrl}
context={SiteFooterContext.dataPageV2}
isPreviewing={isPreviewing}
/>
<script
dangerouslySetInnerHTML={{
__html: `window._OWID_GRAPHER_CONFIG = ${serializeJSONForHTML(
grapherConfig
)}`,
}}
/>
</body>
</Html>
)
}