Skip to content

Commit

Permalink
keep selection between views
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Jun 14, 2024
1 parent 134ecb2 commit 449a16b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
14 changes: 11 additions & 3 deletions site/GrapherFigureView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { action, observable } from "mobx"
import { observer } from "mobx-react"
import React from "react"

import { Grapher, GrapherProgrammaticInterface } from "@ourworldindata/grapher"
import {
Grapher,
GrapherManager,
GrapherProgrammaticInterface,
} from "@ourworldindata/grapher"
import { Bounds } from "@ourworldindata/utils"
import {
ADMIN_BASE_URL,
Expand All @@ -12,7 +16,10 @@ import {

// Wrapper for Grapher that uses css on figure element to determine the bounds
@observer
export class GrapherFigureView extends React.Component<{ grapher: Grapher }> {
export class GrapherFigureView extends React.Component<{
grapher: Grapher
manager?: GrapherManager
}> {
base: React.RefObject<HTMLDivElement> = React.createRef()
@observable.ref bounds?: Bounds

Expand All @@ -32,7 +39,7 @@ export class GrapherFigureView extends React.Component<{ grapher: Grapher }> {
}

render() {
const { grapher } = this.props
const { grapher, manager } = this.props

const props: GrapherProgrammaticInterface = {
...grapher.toObject(),
Expand All @@ -46,6 +53,7 @@ export class GrapherFigureView extends React.Component<{ grapher: Grapher }> {
dataApiUrlForAdmin:
this.context?.admin?.settings?.DATA_API_FOR_ADMIN_UI, // passed this way because clientSettings are baked and need a recompile to be updated
enableKeyboardShortcuts: true,
manager,
}
return (
// They key= in here makes it so that the chart is re-loaded when the slug changes.
Expand Down
6 changes: 4 additions & 2 deletions site/GrapherWithFallback.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Grapher } from "@ourworldindata/grapher"
import { Grapher, GrapherManager } from "@ourworldindata/grapher"
import React from "react"
import { GrapherFigureView } from "./GrapherFigureView.js"
import cx from "classnames"
Expand All @@ -7,11 +7,13 @@ import GrapherImage from "./GrapherImage.js"

export const GrapherWithFallback = ({
grapher,
manager,
slug,
className,
id,
}: {
grapher?: Grapher | undefined
manager?: GrapherManager
slug?: string
className?: string
id?: string
Expand All @@ -27,7 +29,7 @@ export const GrapherWithFallback = ({
>
<>
{grapher ? (
<GrapherFigureView grapher={grapher} />
<GrapherFigureView grapher={grapher} manager={manager} />
) : (
// Render fallback svg when javascript disabled or while
// grapher is loading
Expand Down
37 changes: 31 additions & 6 deletions site/multi-dim/MultiDimDataPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { faArrowDown, faCaretDown } from "@fortawesome/free-solid-svg-icons"
import {
Grapher,
GrapherManager,
GrapherProgrammaticInterface,
SelectionArray,
getVariableMetadataRoute,
} from "@ourworldindata/grapher"
import {
Expand Down Expand Up @@ -71,13 +73,23 @@ import {
DimensionEnriched,
MultiDimDataPageConfigType,
} from "./MultiDimDataPageTypes.js"
import { action, autorun, reaction } from "mobx"
declare global {
interface Window {
_OWID_MULTI_DIM_CONFIG: MultiDimDataPageConfigType
}
}
export const OWID_DATAPAGE_CONTENT_ROOT_ID = "owid-datapageJson-root"

// https://blog.logrocket.com/accessing-previous-props-state-react-hooks/
function usePrevious<T>(value: T) {
const ref = useRef<T>()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}

// 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 <Image />
const DatapageResearchThumbnail = ({
Expand Down Expand Up @@ -436,16 +448,28 @@ export const MultiDimDataPageContent = ({
</a>
))

const grapherConfigComputed = useMemo(() => {
return {
...currentView?.config,
dimensions: dimensionsConfig,
selectedEntityNames: [
const selectionArray = useMemo(
() =>
new SelectionArray([
"Spain",
"Zimbabwe",
"United Kingdom",
"World",
],
]),
[]
)

const grapherManager = useMemo(
(): GrapherManager => ({
selection: selectionArray,
}),
[selectionArray]
)

const grapherConfigComputed = useMemo(() => {
return {
...currentView?.config,
dimensions: dimensionsConfig,
isEmbeddedInADataPage: true,
}
}, [currentView, dimensionsConfig])
Expand Down Expand Up @@ -591,6 +615,7 @@ export const MultiDimDataPageContent = ({
<GrapherWithFallback
key={JSON.stringify(grapherConfigComputed)}
grapher={grapher}
manager={grapherManager}
id="explore-the-data"
/>
{datapageDataFromVar && (
Expand Down

0 comments on commit 449a16b

Please sign in to comment.