Skip to content

Commit

Permalink
enhance(multi-dim): fetch config by uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Sep 3, 2024
1 parent 0b56c6e commit 23ece94
Showing 1 changed file with 73 additions and 14 deletions.
87 changes: 73 additions & 14 deletions site/multiDim/MultiDimDataPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
MultiDimDataPageConfig,
extractMultiDimChoicesFromQueryStr,
multiDimStateToQueryStr,
omit,
} from "@ourworldindata/utils"
import cx from "classnames"
import { DebugProvider } from "../gdocs/DebugContext.js"
Expand Down Expand Up @@ -101,6 +102,13 @@ const cachedGetVariableMetadata = memoize(
)
)

const cachedGetGrapherConfigByUuid = memoize(
(grapherConfigUuid: string): Promise<GrapherInterface> =>
fetch(`/grapher/by-uuid/${grapherConfigUuid}.config.json`).then(
(resp) => resp.json()
)
)

const useTitleFragments = (config: MultiDimDataPageConfig) => {
const title = config.config.title
return useMemo(
Expand All @@ -127,34 +135,70 @@ const useView = (
}

const useVarDatapageData = (
currentView: View<IndicatorsAfterPreProcessing> | undefined
currentView: View<IndicatorsAfterPreProcessing> | undefined,
variableIdToGrapherConfigMap: Record<number, string> | undefined
) => {
const [varDatapageData, setVarDatapageData] =
useState<DataPageDataV2 | null>(null)
const [grapherConfig, setGrapherConfig] = useState<GrapherInterface | null>(
null
)
const [grapherConfigIsReady, setGrapherConfigIsReady] = useState(false)

useEffect(() => {
setGrapherConfigIsReady(false)
setGrapherConfig(null)
setVarDatapageData(null)
const yIndicatorOrIndicators = currentView?.indicators?.["y"]
const variableId = Array.isArray(yIndicatorOrIndicators)
? yIndicatorOrIndicators[0]
: yIndicatorOrIndicators
if (!variableId) return
const variableMetadata = cachedGetVariableMetadata(variableId)

variableMetadata
.then((json) => getDatapageDataV2(json, currentView?.config))
.then(setVarDatapageData)
const datapageDataPromise = cachedGetVariableMetadata(variableId).then(
(json) => getDatapageDataV2(json, currentView?.config)
)
const grapherConfigUuid = variableIdToGrapherConfigMap?.[variableId]
const grapherConfigPromise = grapherConfigUuid
? cachedGetGrapherConfigByUuid(grapherConfigUuid)
: null

Promise.allSettled([datapageDataPromise, grapherConfigPromise])
.then(([datapageData, grapherConfig]) => {
if (datapageData.status === "rejected")
throw new Error(
`Fetching variable by uuid failed: ${grapherConfigUuid}`,
{ cause: datapageData.reason }
)

setVarDatapageData(datapageData.value)
setGrapherConfig(
grapherConfig.status === "fulfilled"
? grapherConfig.value
: null
)
setGrapherConfigIsReady(true)
})
.catch(console.error)
}, [currentView?.indicators, currentView?.config])
}, [
currentView?.indicators,
currentView?.config,
variableIdToGrapherConfigMap,
])

return { varDatapageData }
return {
varDatapageData,
varGrapherConfig: grapherConfig,
grapherConfigIsReady,
}
}

export const MultiDimDataPageContent = ({
// _datapageData,
configObj,
// isPreviewing = false,
faqEntries,
variableIdToGrapherConfigMap,
primaryTopic,
canonicalUrl = "{URL}", // when we bake pages to their proper url this will be set correctly but on preview pages we leave this undefined
tagToSlugMap,
Expand All @@ -179,7 +223,8 @@ export const MultiDimDataPageContent = ({
})

const { currentView, dimensionsConfig } = useView(currentSettings, config)
const { varDatapageData } = useVarDatapageData(currentView)
const { varDatapageData, varGrapherConfig, grapherConfigIsReady } =
useVarDatapageData(currentView, variableIdToGrapherConfigMap)

// This is the ACTUAL grapher instance being used, because GrapherFigureView/GrapherWithFallback are doing weird things and are not actually using the grapher instance we pass into it
// and therefore we can not access the grapher state (e.g. tab, selection) from the grapher instance we pass into it
Expand Down Expand Up @@ -207,15 +252,27 @@ export const MultiDimDataPageContent = ({
}, [queryStr])

const grapherConfigComputed = useMemo(() => {
const baseConfig: GrapherProgrammaticInterface = {
isEmbeddedInAnOwidPage: true,
selectedEntityNames: config.config.defaultSelection ?? [],
bounds,
}

if (!grapherConfigIsReady) return baseConfig
return {
...varGrapherConfig,
...currentView?.config,
dimensions: dimensionsConfig,
isEmbeddedInADataPage: true,
selectedEntityNames: config.config.defaultSelection ?? [],

bounds,
...baseConfig,
} as GrapherProgrammaticInterface
}, [currentView, dimensionsConfig, bounds, config])
}, [
varGrapherConfig,
grapherConfigIsReady,
currentView,
dimensionsConfig,
bounds,
config,
])

const hasTopicTags = !!config.config.topicTags?.length

Expand Down Expand Up @@ -293,7 +350,9 @@ export const MultiDimDataPageContent = ({
>
<figure data-grapher-src ref={grapherFigureRef}>
<Grapher
key={JSON.stringify(currentView)}
key={JSON.stringify(
omit(grapherConfigComputed, ["bounds"])
)}
{...grapherConfigComputed}
queryStr={queryStr}
getGrapherInstance={setGrapherInst}
Expand Down

0 comments on commit 23ece94

Please sign in to comment.