Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart data wrapper key #1577

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions app/charts/chart-data-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { makeStyles } from "@mui/styles";
import { AnimatePresence } from "framer-motion";
import keyBy from "lodash/keyBy";
import React, { createElement, useEffect, useMemo } from "react";
import React, {
ComponentProps,
createElement,
useEffect,
useMemo,
} from "react";

import { A11yTable } from "@/charts/shared/a11y-table";
import { useLoadingState } from "@/charts/shared/chart-loading-state";
Expand Down Expand Up @@ -39,7 +44,7 @@ const useStyles = makeStyles(() => ({
* - Provides observations, dimensions and measures to the chart Component
* - Handles loading & error state
*/
export const ChartDataWrapper = <
const ChartDataWrapperInner = <
TChartConfig extends ChartConfig,
TOtherProps,
TChartComponent extends React.ElementType,
Expand Down Expand Up @@ -200,6 +205,23 @@ export const ChartDataWrapper = <
}
};

/**
* Makes sure the ChartDataWrapper is re-rendered when the cube iris and/or joinBy change.
* This ensures that data hooks do not keep stale data.
*/
export const ChartDataWrapper = (
props: ComponentProps<typeof ChartDataWrapperInner>
) => {
const key = useMemo(
() =>
props.chartConfig.cubes
.map((c) => `${c.iri}${c.joinBy ? `:${c.joinBy}` : ""}`)
.join(" / "),
[props.chartConfig.cubes]
);
return <ChartDataWrapperInner key={key} {...props} />;
};

const Error = ({ message }: { message: string }) => {
return (
<Flex flexGrow={1} justifyContent="center" minHeight={300}>
Expand Down
8 changes: 6 additions & 2 deletions app/configurator/configurator-state/reducer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import produce, { createDraft, current, Draft } from "immer";
import produce, { createDraft, current, Draft, isDraft } from "immer";
import { WritableDraft } from "immer/dist/internal";
import get from "lodash/get";
import isEqual from "lodash/isEqual";
Expand Down Expand Up @@ -1107,7 +1107,11 @@ const withLogging = <TState, TAction extends { type: unknown }>(
) => {
return (state: Draft<TState>, action: TAction) => {
const res = reducer(state, action);
console.log(`Action: ${action.type}`, action, res);
console.log(
`Action: ${action.type}`,
action,
isDraft(state) ? current(state) : state
);
return res;
};
};
Expand Down
Loading