Skip to content

Commit

Permalink
keep tab between views
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Jun 14, 2024
1 parent 449a16b commit df0572f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions site/GrapherFigureView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
export class GrapherFigureView extends React.Component<{
grapher: Grapher
manager?: GrapherManager
getGrapherInstance?: (grapher: Grapher) => void
}> {
base: React.RefObject<HTMLDivElement> = React.createRef()
@observable.ref bounds?: Bounds
Expand Down Expand Up @@ -54,6 +55,7 @@ export class GrapherFigureView extends React.Component<{
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,
getGrapherInstance: this.props.getGrapherInstance,
}
return (
// They key= in here makes it so that the chart is re-loaded when the slug changes.
Expand Down
8 changes: 7 additions & 1 deletion site/GrapherWithFallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export const GrapherWithFallback = ({
slug,
className,
id,
getGrapherInstance,
}: {
grapher?: Grapher | undefined
manager?: GrapherManager
slug?: string
className?: string
id?: string
getGrapherInstance?: (grapher: Grapher) => void
}) => {
return (
<div
Expand All @@ -29,7 +31,11 @@ export const GrapherWithFallback = ({
>
<>
{grapher ? (
<GrapherFigureView grapher={grapher} manager={manager} />
<GrapherFigureView
grapher={grapher}
manager={manager}
getGrapherInstance={getGrapherInstance}
/>
) : (
// Render fallback svg when javascript disabled or while
// grapher is loading
Expand Down
14 changes: 12 additions & 2 deletions site/multi-dim/MultiDimDataPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
Url,

Check warning on line 57 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'Url' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 57 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'Url' is defined but never used. Allowed unused vars must match /^_/u
getWindowQueryParams,
isEqual,
GrapherTabOption,
} from "@ourworldindata/utils"
import { AttachmentsContext, DocumentContext } from "../gdocs/OwidGdoc.js"

Check warning on line 62 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'AttachmentsContext' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 62 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'DocumentContext' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 62 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'AttachmentsContext' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 62 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'DocumentContext' is defined but never used. Allowed unused vars must match /^_/u
import StickyNav from "../blocks/StickyNav.js"

Check warning on line 63 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'StickyNav' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 63 in site/multi-dim/MultiDimDataPageContent.tsx

View workflow job for this annotation

GitHub Actions / eslint

'StickyNav' is defined but never used. Allowed unused vars must match /^_/u
Expand Down Expand Up @@ -466,13 +467,21 @@ export const MultiDimDataPageContent = ({
[selectionArray]
)

// 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
// TODO we should probably fix that? seems sensible? change GrapherFigureView around a bit to use the actual grapher inst? or pass a GrapherProgrammaticInterface to it instead?
const [grapherInst, setGrapherInst] = useState<Grapher | null>(null)

const grapherConfigComputed = useMemo(() => {
return {
...currentView?.config,
dimensions: dimensionsConfig,
isEmbeddedInADataPage: true,
}
}, [currentView, dimensionsConfig])

// Keep the tab we last had
tab: grapherInst?.tab ?? GrapherTabOption.chart,
} as GrapherProgrammaticInterface
}, [currentView, dimensionsConfig, grapherInst])

const grapher = useMemo(() => {
const grapher = new Grapher(grapherConfigComputed)
Expand Down Expand Up @@ -616,6 +625,7 @@ export const MultiDimDataPageContent = ({
key={JSON.stringify(grapherConfigComputed)}
grapher={grapher}
manager={grapherManager}
getGrapherInstance={(g) => setGrapherInst}
id="explore-the-data"
/>
{datapageDataFromVar && (
Expand Down

0 comments on commit df0572f

Please sign in to comment.