Skip to content

Commit e1b4562

Browse files
authored
feat: Add pluginDataMap to redux, add useDashboardPluginData hook (#1737)
Add `useDashboardPluginData` hook for accessing plugin data in redux
1 parent 689a1e2 commit e1b4562

5 files changed

Lines changed: 91 additions & 4 deletions

File tree

packages/dashboard/src/redux/actions.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { DashboardData, RootState } from '@deephaven/redux';
1+
import { DashboardData, PluginData, RootState } from '@deephaven/redux';
22
import type { Action } from 'redux';
33
import type { ThunkAction } from 'redux-thunk';
44
import { SET_DASHBOARD_DATA } from './actionTypes';
5-
import { getDashboardData } from './selectors';
5+
import { getDashboardData, getPluginDataMapForDashboard } from './selectors';
66

77
/**
88
* Action to replace the dashboard data for a dashboard
@@ -41,3 +41,26 @@ export const updateDashboardData =
4141
...data,
4242
})
4343
);
44+
45+
/**
46+
* Action to update the dashboard data. Will combine the update with any existing dashboard data.
47+
* @param id The id of the dashboard to set the data on
48+
* @param pluginId The id of the plugin to set the data on
49+
* @param data The data to replace the existing plugin data with
50+
* @returns
51+
*/
52+
export const setDashboardPluginData =
53+
(
54+
id: string,
55+
pluginId: string,
56+
data: PluginData
57+
): ThunkAction<unknown, RootState, undefined, Action<unknown>> =>
58+
(dispatch, getState) =>
59+
dispatch(
60+
setDashboardData(id, {
61+
...getDashboardData(getState(), id),
62+
pluginDataMap: new Map(
63+
getPluginDataMapForDashboard(getState(), id)
64+
).set(pluginId, data),
65+
})
66+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useCallback } from 'react';
2+
import { useDispatch, useSelector } from 'react-redux';
3+
import { PluginData, RootState } from '@deephaven/redux';
4+
import { getPluginDataForDashboard } from './selectors';
5+
import { setDashboardPluginData } from './actions';
6+
7+
/**
8+
* Custom hook that provides access to plugin data for a specific dashboard and plugin.
9+
* @param dashboardId - The ID of the dashboard.
10+
* @param pluginId - The ID of the plugin.
11+
* @returns A tuple containing the plugin data and a function to update the plugin data.
12+
*/
13+
export function useDashboardPluginData(
14+
dashboardId: string,
15+
pluginId: string
16+
): [PluginData, (data: PluginData) => void] {
17+
const dispatch = useDispatch();
18+
const data = useSelector((store: RootState) =>
19+
getPluginDataForDashboard(store, dashboardId, pluginId)
20+
);
21+
const setData = useCallback(
22+
newData => dispatch(setDashboardPluginData(dashboardId, pluginId, newData)),
23+
[dashboardId, pluginId, dispatch]
24+
);
25+
return [data, setData];
26+
}
27+
28+
export default { useDashboardPluginData };

packages/dashboard/src/redux/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Object.entries(reducers).map(([name, reducer]) =>
88
export { reducers };
99
export * from './actions';
1010
export * from './actionTypes';
11+
export * from './hooks';
1112
export * from './selectors';

packages/dashboard/src/redux/selectors.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { DashboardData, RootState } from '@deephaven/redux';
1+
import {
2+
DashboardData,
3+
PluginData,
4+
PluginDataMap,
5+
RootState,
6+
} from '@deephaven/redux';
27
import { ClosedPanels, OpenedPanelMap } from '../PanelManager';
38

49
const EMPTY_MAP = new Map();
@@ -50,3 +55,26 @@ export const getOpenedPanelMapForDashboard = (
5055
): OpenedPanelMap =>
5156
(getDashboardData(store, dashboardId).openedMap ??
5257
EMPTY_MAP) as OpenedPanelMap;
58+
59+
/**
60+
* @param store The redux store
61+
* @param dashboardId The dashboard ID to get data for
62+
* @returns The map of plugin IDs to data for all plugins on the dashboard
63+
*/
64+
export const getPluginDataMapForDashboard = (
65+
store: RootState,
66+
dashboardId: string
67+
): PluginDataMap =>
68+
getDashboardData(store, dashboardId).pluginDataMap ?? EMPTY_MAP;
69+
70+
/**
71+
* @param store The redux store
72+
* @param dashboardId The dashboard ID to get data for
73+
* @param pluginId The plugin ID to get data for
74+
* @returns The plugin data
75+
*/
76+
export const getPluginDataForDashboard = (
77+
store: RootState,
78+
dashboardId: string,
79+
pluginId: string
80+
): PluginData => getPluginDataMapForDashboard(store, dashboardId).get(pluginId);

packages/redux/src/store.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ export interface CustomizableWorkspace {
7777
export interface Workspace {
7878
data: WorkspaceData;
7979
}
80-
export type DashboardData = Record<string, unknown>;
80+
81+
export type PluginData = unknown;
82+
83+
export type PluginDataMap = Map<string, PluginData>;
84+
85+
export type DashboardData = Record<string, unknown> & {
86+
pluginDataMap?: PluginDataMap;
87+
};
8188

8289
export type WorkspaceStorageLoadOptions = {
8390
isConsoleAvailable: boolean;

0 commit comments

Comments
 (0)