From cb49c8ba2cda9701c93928a8d18f74097c73956d Mon Sep 17 00:00:00 2001 From: mikebender Date: Tue, 10 Sep 2024 09:27:50 -0400 Subject: [PATCH] Fix up chart builder to work with the latest - Don't return an error from makeModel until the API has loaded or an error has occurred - Allow ChartPanel to reload if makeModel has updated even if it hasn't loaded a model yet --- .../src/ChartPanelPlugin.tsx | 49 +++++++++++-------- .../src/panels/ChartPanel.tsx | 10 ++-- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/packages/dashboard-core-plugins/src/ChartPanelPlugin.tsx b/packages/dashboard-core-plugins/src/ChartPanelPlugin.tsx index a1b6b79b7..c561d24ab 100644 --- a/packages/dashboard-core-plugins/src/ChartPanelPlugin.tsx +++ b/packages/dashboard-core-plugins/src/ChartPanelPlugin.tsx @@ -1,4 +1,4 @@ -import { forwardRef, useMemo } from 'react'; +import { forwardRef, useCallback } from 'react'; import { useDeferredApi } from '@deephaven/jsapi-bootstrap'; import { ChartModel, ChartModelFactory } from '@deephaven/chart'; import type { dh as DhType } from '@deephaven/jsapi-types'; @@ -70,27 +70,36 @@ export const ChartPanelPlugin = forwardRef( : undefined; const { fetch: panelFetch, metadata, localDashboardId } = props; - const [dh, error] = useDeferredApi(metadata ?? null); + assertNotNull(metadata); + const [dh, error] = useDeferredApi(metadata); - const hydratedProps = useMemo( - () => ({ - metadata: metadata as ChartPanelMetadata, - localDashboardId, - makeModel: async () => { - assertNotNull(dh, `Cannot find API: ${error}`); - return createChartModel( - dh, - metadata as ChartPanelMetadata, - panelFetch, - panelState - ); - }, - }), - [dh, error, metadata, localDashboardId, panelFetch, panelState] - ); + const makeModel = useCallback(async () => { + if (error != null) { + throw error; + } + if (dh == null) { + return new Promise(() => { + // We don't have the API yet, just return an unresolved promise so it shows as loading + }); + } + return createChartModel( + dh, + metadata as ChartPanelMetadata, + panelFetch, + panelState + ); + }, [dh, error, metadata, panelFetch, panelState]); - // eslint-disable-next-line react/jsx-props-no-spreading - return ; + return ( + + ); } ); diff --git a/packages/dashboard-core-plugins/src/panels/ChartPanel.tsx b/packages/dashboard-core-plugins/src/panels/ChartPanel.tsx index 153313228..e44514b08 100644 --- a/packages/dashboard-core-plugins/src/panels/ChartPanel.tsx +++ b/packages/dashboard-core-plugins/src/panels/ChartPanel.tsx @@ -286,14 +286,14 @@ export class ChartPanel extends Component { const { columnMap, model, filterMap, filterValueMap, isLinked, settings } = this.state; - if (!model) { - return; - } - if (makeModel !== prevProps.makeModel) { this.initModel(); } + if (model == null) { + return; + } + if (columnMap !== prevState.columnMap) { this.pruneFilterMaps(); } @@ -352,6 +352,8 @@ export class ChartPanel extends Component { const { makeModel } = this.props; + this.pending.cancel(); + this.pending .add(makeModel(), resolved => { resolved.close();