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

Fix issue with non-memoized table data #5429

Merged
merged 1 commit into from
Feb 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export type BaseChartConfig = BaseConfig & {
renderLegendForOneItem?: boolean;
};

export const isChartConfig = (config: BaseConfig): config is BaseChartConfig => {
return 'type' in config && config.type === 'chart';
export const isChartConfig = (config?: BaseConfig): config is BaseChartConfig => {
return (config && 'type' in config && config.type === 'chart') ?? false;
jaskfla marked this conversation as resolved.
Show resolved Hide resolved
};

export type CartesianChartPresentationOptions = ExportPresentationOptions & {
Expand Down
28 changes: 14 additions & 14 deletions packages/ui-chart-components/src/utils/getChartTableData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import React, { useMemo } from 'react';
import styled from 'styled-components';
import { formatDataValueByType } from '@tupaia/utils';
import { ValueType, ChartType } from '@tupaia/types';
import { ValueType, ChartType, isChartConfig } from '@tupaia/types';
import { DEFAULT_DATA_KEY } from '../constants';
import { ExportViewContent, LooseObject, TableAccessor, ChartViewContent } from '../types';
import { formatTimestampForChart, getIsTimeSeries } from './utils';
Expand Down Expand Up @@ -123,24 +123,24 @@ const processData = (viewContent: ChartViewContent) => {
};

export const getChartTableData = (viewContent?: ExportViewContent) => {
// tables only work for charts
if (!viewContent || viewContent.type !== 'chart') {
return {
columns: [],
data: [],
};
}
// Because react-table wants its sort function to be memoized, it needs to live here, outside of
// the other useMemo hooks
// the other useMemo hooks. All values need to be memoized, even default values, otherwise it will
// cause a potentially infinite loop of re-renders.
// See: [https://github.com/TanStack/table/issues/2369](this issue on GitHub for more information)
const sortByTimestamp = useMemo(
() => (rowA: any, rowB: any) => sortDates(rowA.original.timestamp, rowB.original.timestamp),
undefined,
);
const columns = useMemo(
() => processColumns(viewContent, sortByTimestamp),
[JSON.stringify(viewContent)],
);
const data = useMemo(() => processData(viewContent), [JSON.stringify(viewContent)]);

const isChart = isChartConfig(viewContent);
const columns = useMemo(() => {
// only process columns if it's a chart, otherwise return an empty array. It won't be used but we have to memoize default values
return isChart ? processColumns(viewContent, sortByTimestamp) : [];
}, [JSON.stringify(viewContent)]);
const data = useMemo(() => {
// only process columns if it's a chart, otherwise return an empty array. It won't be used but we have to memoize default values
return isChart ? processData(viewContent) : [];
}, [JSON.stringify(viewContent)]);
return {
columns,
data,
Expand Down
Loading