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: Load default dashboard data from workspace data #1810

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
62 changes: 52 additions & 10 deletions packages/code-studio/src/main/AppMainContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
PanelEvent,
setDashboardData as setDashboardDataAction,
setDashboardPluginData as setDashboardPluginDataAction,
stopListenForCreateDashboard,
updateDashboardData as updateDashboardDataAction,
} from '@deephaven/dashboard';
import {
Expand Down Expand Up @@ -203,6 +204,8 @@ export class AppMainContainer extends Component<

const { allDashboardData } = this.props;

this.dashboardLayouts = new Map();

this.state = {
contextActions: [
{
Expand Down Expand Up @@ -261,6 +264,7 @@ export class AppMainContainer extends Component<

componentDidMount(): void {
this.initWidgets();
this.initDashboardData();
this.startListeningForDisconnect();

window.addEventListener(
Expand All @@ -283,13 +287,18 @@ export class AppMainContainer extends Component<
this.deinitWidgets();
this.stopListeningForDisconnect();

this.dashboardLayouts.forEach(layout => {
stopListenForCreateDashboard(layout.eventHub, this.handleCreateDashboard);
});

window.removeEventListener(
'beforeunload',
AppMainContainer.handleWindowBeforeUnload
);
}

goldenLayout?: GoldenLayout;
/** Map from the dashboard ID to the GoldenLayout instance for that dashboard */
dashboardLayouts: Map<string, GoldenLayout>;

importElement: RefObject<HTMLInputElement>;

Expand Down Expand Up @@ -337,6 +346,28 @@ export class AppMainContainer extends Component<
this.widgetListenerRemover?.();
}

initDashboardData(): void {
// TODO: #1746 We should be loading data from a dashboard storage store
// For now only the default dashboard data is stored with the workspace and set on the default dashboard
const { setDashboardPluginData, updateDashboardData, workspace } =
this.props;
const { data: workspaceData } = workspace;
const { filterSets, links, pluginDataMap } = workspaceData;
updateDashboardData(DEFAULT_DASHBOARD_ID, {
filterSets,
links,
});
if (pluginDataMap != null) {
const pluginKeys = Object.keys(pluginDataMap);
for (let i = 0; i < pluginKeys.length; i += 1) {
const pluginId = pluginKeys[i];
const pluginData = pluginDataMap[pluginId];
log.debug('initDashboardData plugin data', pluginId, pluginData);
setDashboardPluginData(DEFAULT_DASHBOARD_ID, pluginId, pluginData);
}
}
}

openNotebookFromURL(): void {
const { match } = this.props;
const { notebookPath } = match.params;
Expand Down Expand Up @@ -374,7 +405,9 @@ export class AppMainContainer extends Component<
}

emitLayoutEvent(event: string, ...args: unknown[]): void {
this.goldenLayout?.eventHub.emit(event, ...args);
const { activeTabKey } = this.state;
const layout = this.dashboardLayouts.get(activeTabKey);
layout?.eventHub.emit(event, ...args);
}

handleCancelResetLayoutPrompt(): void {
Expand Down Expand Up @@ -465,16 +498,25 @@ export class AppMainContainer extends Component<
const { updateWorkspaceData } = this.props;

// Only save the data that is serializable/we want to persist to the workspace
const { closed, filterSets, links } = data;
updateWorkspaceData({ closed, filterSets, links });
const { closed, filterSets, links, pluginDataMap } = data;
updateWorkspaceData({ closed, filterSets, links, pluginDataMap });
}

handleGoldenLayoutChange(goldenLayout: GoldenLayout): void {
this.goldenLayout = goldenLayout;
listenForCreateDashboard(
this.goldenLayout.eventHub,
this.handleCreateDashboard
);
handleGoldenLayoutChange(newLayout: GoldenLayout): void {
const { activeTabKey } = this.state;
const oldLayout = this.dashboardLayouts.get(activeTabKey);
if (oldLayout === newLayout) return;

if (oldLayout != null) {
stopListenForCreateDashboard(
oldLayout.eventHub,
this.handleCreateDashboard
);
}

this.dashboardLayouts.set(activeTabKey, newLayout);

listenForCreateDashboard(newLayout.eventHub, this.handleCreateDashboard);
}

handleCreateDashboard({
Expand Down
1 change: 1 addition & 0 deletions packages/code-studio/src/storage/LocalWorkspaceStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class LocalWorkspaceStorage implements WorkspaceStorage {
closed: [{}],
links,
filterSets,
pluginDataMap: {},
};
}

Expand Down
20 changes: 14 additions & 6 deletions packages/dashboard/src/DashboardEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import type { EventHub } from '@deephaven/golden-layout';

export const CREATE_DASHBOARD = 'CREATE_DASHBOARD';

export interface CreateDashboardPayload {
export interface CreateDashboardPayload<T = unknown> {
pluginId: string;
title: string;
data: unknown;
data: T;
Comment on lines +5 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the generic? The data comes from the plugin and we only really care it's serializable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the listen/emit events can add typing for payload. For the emit function which will come from the plugin, can add typing so we know we're not passing a malformed payload.

}

export function listenForCreateDashboard(
export function stopListenForCreateDashboard<T = unknown>(
eventHub: EventHub,
handler: (p: CreateDashboardPayload) => void
handler: (p: CreateDashboardPayload<T>) => void
): void {
eventHub.off(CREATE_DASHBOARD, handler);
}

export function listenForCreateDashboard<T = unknown>(
eventHub: EventHub,
handler: (p: CreateDashboardPayload<T>) => void
): () => void {
eventHub.on(CREATE_DASHBOARD, handler);
return () => stopListenForCreateDashboard(eventHub, handler);
}

export function emitCreateDashboard(
export function emitCreateDashboard<T = unknown>(
eventHub: EventHub,
payload: CreateDashboardPayload
payload: CreateDashboardPayload<T>
): void {
eventHub.emit(CREATE_DASHBOARD, payload);
}
5 changes: 4 additions & 1 deletion packages/redux/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ export interface WorkspaceSettings {
}

export interface WorkspaceData {
settings: WorkspaceSettings;

// TODO: #1746 The rest of these options should not be stored with workspace data, we should have a separate DashboardStorage
closed: unknown[];
filterSets: unknown[];
layoutConfig: unknown[];
links: unknown;
settings: WorkspaceSettings;
pluginDataMap: PluginDataMap;
}

export interface CustomizableWorkspaceData
Expand Down
Loading