diff --git a/adminSiteClient/GrapherConfigGridEditor.tsx b/adminSiteClient/GrapherConfigGridEditor.tsx index d4539d9b76..2155a06a86 100644 --- a/adminSiteClient/GrapherConfigGridEditor.tsx +++ b/adminSiteClient/GrapherConfigGridEditor.tsx @@ -330,7 +330,8 @@ export class GrapherConfigGridEditor extends React.Component } diff --git a/baker/GrapherImageBaker.tsx b/baker/GrapherImageBaker.tsx index 51362d1caf..d646d240c3 100644 --- a/baker/GrapherImageBaker.tsx +++ b/baker/GrapherImageBaker.tsx @@ -34,7 +34,7 @@ export async function bakeGrapherToSvgAndPng( optimizeSvgs = false ) { const grapher = initGrapherForSvgExport(jsonConfig) - grapher.receiveOwidData(vardata) + // grapher.receiveOwidData(vardata) const outPath = path.join(outDir, grapher.slug as string) let svgCode = grapher.staticSVG @@ -125,7 +125,7 @@ export async function bakeGrapherToSvg( if (fs.existsSync(outPath) && !overwriteExisting) return const variableIds = grapher.dimensions.map((d) => d.variableId) const vardata = await getDataForMultipleVariables(variableIds) - grapher.receiveOwidData(vardata) + // grapher.receiveOwidData(vardata) let svgCode = grapher.staticSVG if (optimizeSvgs) svgCode = await optimizeSvg(svgCode) @@ -234,6 +234,6 @@ export async function grapherToSVG( const grapher = new Grapher({ ...jsonConfig, manuallyProvideData: true }) grapher.isExportingToSvgOrPng = true grapher.shouldIncludeDetailsInStaticExport = false - grapher.receiveOwidData(vardata) + // grapher.receiveOwidData(vardata) return grapher.staticSVG } diff --git a/baker/updateChartEntities.ts b/baker/updateChartEntities.ts index cbaf6a7410..1f5cfab9ec 100644 --- a/baker/updateChartEntities.ts +++ b/baker/updateChartEntities.ts @@ -93,7 +93,8 @@ const obtainAvailableEntitiesForGrapherConfig = async ( await getVariableDataUsingCache(variableId), ]) ) - grapher.receiveOwidData(variableData) + // TODO: make sure that data is loaded here + // grapher.receiveOwidData(variableData) // If the grapher has a chart tab, then the available entities there are the "most interesting" ones to us if (grapher.hasChartTab) { diff --git a/devTools/svgTester/utils.ts b/devTools/svgTester/utils.ts index 12d20a1c1d..537667bc29 100644 --- a/devTools/svgTester/utils.ts +++ b/devTools/svgTester/utils.ts @@ -419,7 +419,7 @@ export async function renderSvg( { shouldHashQueryStr: false, separator: "?" } ) - grapher.receiveOwidData(configAndData.variableData) + // grapher.receiveOwidData(configAndData.variableData) const durationReceiveData = Date.now() - timeStart const svg = grapher.staticSVG diff --git a/packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx b/packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx index fa6f1b79a3..32fbd47737 100644 --- a/packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx +++ b/packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx @@ -14,14 +14,17 @@ export interface FetchingGrapherProps { configUrl?: string queryString?: string dataApiUrl: string + adminBaseUrl: string + bakedGrapherURL: string } -export async function FetchingGrapher( +export function FetchingGrapher( props: FetchingGrapherProps -): Promise { +): JSX.Element | null { // if config is not provided, fetch it from configUrl + console.log("FetchingGrapher") const [config, setConfig] = React.useState( - undefined + props.config ) const [inputTable, setInputTable] = React.useState( @@ -30,12 +33,14 @@ export async function FetchingGrapher( React.useEffect(() => { async function fetchConfigAndLoadData(): Promise { + console.log("fetchConfigAndLoadData", props.configUrl) if (!config && props.configUrl) { const fetchedConfig = await fetch(props.configUrl).then((res) => res.json() ) setConfig(fetchedConfig) } + console.log("fetchConfigAndLoadData: config", config) if (!config) return const dimensions = config.dimensions || [] if (dimensions.length === 0) return @@ -48,6 +53,7 @@ export async function FetchingGrapher( variablesDataMap, dimensions ) + console.log("setting input table") setInputTable(inputTable) } void fetchConfigAndLoadData() @@ -55,7 +61,15 @@ export async function FetchingGrapher( if (!config) return null if (!inputTable) return null - return + return ( + + ) } // async function loadVariablesDataAdmin( diff --git a/packages/@ourworldindata/grapher/src/index.ts b/packages/@ourworldindata/grapher/src/index.ts index 554f3f38f9..c87cd005de 100644 --- a/packages/@ourworldindata/grapher/src/index.ts +++ b/packages/@ourworldindata/grapher/src/index.ts @@ -7,6 +7,7 @@ export { type ColorScaleBin, } from "./color/ColorScaleBin" export { ChartDimension } from "./chart/ChartDimension" +export { FetchingGrapher } from "./core/FetchingGrapher" export { GRAPHER_EMBEDDED_FIGURE_ATTR, GRAPHER_EMBEDDED_FIGURE_CONFIG_ATTR, diff --git a/site/DataPageV2Content.tsx b/site/DataPageV2Content.tsx index 5b46203187..d8dfa6c897 100644 --- a/site/DataPageV2Content.tsx +++ b/site/DataPageV2Content.tsx @@ -1,12 +1,14 @@ -import { useState, useEffect, useMemo } from "react" -import { Grapher, GrapherProgrammaticInterface } from "@ourworldindata/grapher" +import { useState, useMemo } from "react"; +import { + FetchingGrapher, + GrapherProgrammaticInterface, +} from "@ourworldindata/grapher"; import { REUSE_THIS_WORK_SECTION_ID, DATAPAGE_SOURCES_AND_PROCESSING_SECTION_ID, -} from "@ourworldindata/components" -import ReactDOM from "react-dom" -import { GrapherWithFallback } from "./GrapherWithFallback.js" -import { RelatedCharts } from "./blocks/RelatedCharts.js" +} from "@ourworldindata/components"; +import ReactDOM from "react-dom"; +import { RelatedCharts } from "./blocks/RelatedCharts.js"; import { DataPageV2ContentFields, formatAuthors, @@ -15,33 +17,38 @@ import { joinTitleFragments, ImageMetadata, DEFAULT_THUMBNAIL_FILENAME, -} from "@ourworldindata/utils" -import { AttachmentsContext, DocumentContext } from "./gdocs/OwidGdoc.js" -import StickyNav from "./blocks/StickyNav.js" -import { DebugProvider } from "./gdocs/DebugContext.js" -import { BAKED_BASE_URL } from "../settings/clientSettings.js" -import Image from "./gdocs/components/Image.js" -import AboutThisData from "./AboutThisData.js" -import MetadataSection from "./MetadataSection.js" -import TopicTags from "./TopicTags.js" +} from "@ourworldindata/utils"; +import { AttachmentsContext, DocumentContext } from "./gdocs/OwidGdoc.js"; +import StickyNav from "./blocks/StickyNav.js"; +import { DebugProvider } from "./gdocs/DebugContext.js"; +import { + ADMIN_BASE_URL, + BAKED_BASE_URL, + BAKED_GRAPHER_URL, + DATA_API_URL, +} from "../settings/clientSettings.js"; +import Image from "./gdocs/components/Image.js"; +import AboutThisData from "./AboutThisData.js"; +import MetadataSection from "./MetadataSection.js"; +import TopicTags from "./TopicTags.js"; declare global { interface Window { - _OWID_DATAPAGEV2_PROPS: DataPageV2ContentFields - _OWID_GRAPHER_CONFIG: GrapherInterface + _OWID_DATAPAGEV2_PROPS: DataPageV2ContentFields; + _OWID_GRAPHER_CONFIG: GrapherInterface; } } -export const OWID_DATAPAGE_CONTENT_ROOT_ID = "owid-datapageJson-root" +export const OWID_DATAPAGE_CONTENT_ROOT_ID = "owid-datapageJson-root"; // We still have topic pages on WordPress, whose featured images are specified as absolute URLs which this component handles // Once we've migrated all WP topic pages to gdocs, we'll be able to remove this component and just use const DatapageResearchThumbnail = ({ urlOrFilename, }: { - urlOrFilename: string | undefined | null + urlOrFilename: string | undefined | null; }) => { if (!urlOrFilename) { - urlOrFilename = `${BAKED_BASE_URL}/${DEFAULT_THUMBNAIL_FILENAME}` + urlOrFilename = `${BAKED_BASE_URL}/${DEFAULT_THUMBNAIL_FILENAME}`; } if (urlOrFilename.startsWith("http")) { return ( @@ -49,7 +56,7 @@ const DatapageResearchThumbnail = ({ src={urlOrFilename} className="span-lg-cols-2 span-sm-cols-3" /> - ) + ); } return ( - ) -} + ); +}; export const DataPageV2Content = ({ datapageData, @@ -70,15 +77,13 @@ export const DataPageV2Content = ({ tagToSlugMap, imageMetadata, }: DataPageV2ContentFields & { - grapherConfig: GrapherInterface - imageMetadata: Record + grapherConfig: GrapherInterface; + imageMetadata: Record; }) => { - const [grapher, setGrapher] = useState(undefined) - const titleFragments = joinTitleFragments( datapageData.attributionShort, datapageData.titleVariant - ) + ); // Initialize the grapher for client-side rendering const mergedGrapherConfig: GrapherProgrammaticInterface = useMemo( @@ -88,11 +93,7 @@ export const DataPageV2Content = ({ bindUrlToWindow: true, }), [grapherConfig] - ) - - useEffect(() => { - setGrapher(new Grapher(mergedGrapherConfig)) - }, [mergedGrapherConfig]) + ); const stickyNavLinks = [ { @@ -110,9 +111,9 @@ export const DataPageV2Content = ({ target: "#" + DATAPAGE_SOURCES_AND_PROCESSING_SECTION_ID, }, { text: "Reuse This Work", target: "#" + REUSE_THIS_WORK_SECTION_ID }, - ] + ]; - const relatedResearchCandidates = datapageData.relatedResearch + const relatedResearchCandidates = datapageData.relatedResearch; const relatedResearch = relatedResearchCandidates.length > 3 && datapageData.topicTagsLinks?.length @@ -120,20 +121,20 @@ export const DataPageV2Content = ({ const shared = intersection( research.tags, datapageData.topicTagsLinks ?? [] - ) - return shared.length > 0 + ); + return shared.length > 0; }) - : relatedResearchCandidates + : relatedResearchCandidates; for (const item of relatedResearch) { // TODO: these are workarounds to not link to the (not really existing) template pages for energy or co2 // country profiles but instead to the topic page at the country selector. if (item.url === "/co2-country-profile") item.url = - "/co2-and-greenhouse-gas-emissions#co2-and-greenhouse-gas-emissions-country-profiles" + "/co2-and-greenhouse-gas-emissions#co2-and-greenhouse-gas-emissions-country-profiles"; else if (item.url === "/energy-country-profile") - item.url = "/energy#country-profiles" + item.url = "/energy#country-profiles"; else if (item.url === "/coronavirus-country-profile") - item.url = "/coronavirus#coronavirus-country-profiles" + item.url = "/coronavirus#coronavirus-country-profiles"; } return ( @@ -148,9 +149,11 @@ export const DataPageV2Content = ({ >
-
@@ -182,14 +185,11 @@ export const DataPageV2Content = ({
- - ) -} + ); +}; export const hydrateDataPageV2Content = (isPreviewing?: boolean) => { - const wrapper = document.querySelector(`#${OWID_DATAPAGE_CONTENT_ROOT_ID}`) - const props: DataPageV2ContentFields = window._OWID_DATAPAGEV2_PROPS - const grapherConfig = window._OWID_GRAPHER_CONFIG + const wrapper = document.querySelector(`#${OWID_DATAPAGE_CONTENT_ROOT_ID}`); + const props: DataPageV2ContentFields = window._OWID_DATAPAGEV2_PROPS; + const grapherConfig = window._OWID_GRAPHER_CONFIG; ReactDOM.hydrate( @@ -289,5 +289,5 @@ export const hydrateDataPageV2Content = (isPreviewing?: boolean) => { /> , wrapper - ) -} + ); +}; diff --git a/site/GrapherPage.tsx b/site/GrapherPage.tsx index c5c3929fc2..f7cb15dfbe 100644 --- a/site/GrapherPage.tsx +++ b/site/GrapherPage.tsx @@ -1,4 +1,5 @@ import { + FetchingGrapher, getVariableDataRoute, getVariableMetadataRoute, GRAPHER_PAGE_BODY_CLASS, @@ -111,12 +112,12 @@ window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
-
- -
+
{grapher.slug && (