mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 02:09:32 -04:00
Closes https://github.com/elastic/kibana/issues/211930 ## Summary This PR makes it so that `kbn-grid-layout` stores its rows as an object / dictionary (`{ [key: string]: GridRowData }`) rather than an array (`Array<GridRowData>`). This is a prerequisite for https://github.com/elastic/kibana/issues/190381 , since it allows us to re-order rows without re-rendering their contents. It also means that deleting a row will no longer cause the rows below it to re-render, since re-rendering is now dependant on the row's **ID** rather than the row's order. **Before** https://github.com/user-attachments/assets/83651b24-a32c-4953-8ad5-c0eced163eb5 **After** https://github.com/user-attachments/assets/9cef6dbc-3d62-46aa-bc40-ab24fc4e5556 ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
123 lines
4.2 KiB
TypeScript
123 lines
4.2 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the "Elastic License
|
|
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
|
* Public License v 1"; you may not use this file except in compliance with, at
|
|
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
|
* License v3.0 only", or the "Server Side Public License, v 1".
|
|
*/
|
|
|
|
import { cloneDeep } from 'lodash';
|
|
import { useMemo } from 'react';
|
|
import { BehaviorSubject } from 'rxjs';
|
|
import { v4 } from 'uuid';
|
|
|
|
import { TimeRange } from '@kbn/es-query';
|
|
import { PanelPackage } from '@kbn/presentation-containers';
|
|
|
|
import { ViewMode } from '@kbn/presentation-publishing';
|
|
import {
|
|
MockDashboardApi,
|
|
MockSerializedDashboardState,
|
|
MockedDashboardPanelMap,
|
|
MockedDashboardRowMap,
|
|
} from './types';
|
|
|
|
const DASHBOARD_GRID_COLUMN_COUNT = 48;
|
|
const DEFAULT_PANEL_HEIGHT = 15;
|
|
const DEFAULT_PANEL_WIDTH = DASHBOARD_GRID_COLUMN_COUNT / 2;
|
|
|
|
export const useMockDashboardApi = ({
|
|
savedState,
|
|
}: {
|
|
savedState: MockSerializedDashboardState;
|
|
}): MockDashboardApi => {
|
|
const mockDashboardApi = useMemo(() => {
|
|
const panels$ = new BehaviorSubject<MockedDashboardPanelMap>(savedState.panels);
|
|
const expandedPanelId$ = new BehaviorSubject<string | undefined>(undefined);
|
|
const viewMode$ = new BehaviorSubject<ViewMode>('edit');
|
|
|
|
return {
|
|
getSerializedStateForChild: (id: string) => {
|
|
return {
|
|
rawState: panels$.getValue()[id].explicitInput,
|
|
references: [],
|
|
};
|
|
},
|
|
children$: new BehaviorSubject({}),
|
|
timeRange$: new BehaviorSubject<TimeRange>({
|
|
from: 'now-24h',
|
|
to: 'now',
|
|
}),
|
|
filters$: new BehaviorSubject([]),
|
|
query$: new BehaviorSubject(''),
|
|
viewMode$,
|
|
setViewMode: (viewMode: ViewMode) => viewMode$.next(viewMode),
|
|
panels$,
|
|
getPanelCount: () => {
|
|
return Object.keys(panels$.getValue()).length;
|
|
},
|
|
rows$: new BehaviorSubject<MockedDashboardRowMap>(savedState.rows),
|
|
expandedPanelId$,
|
|
expandPanel: (id: string) => {
|
|
if (expandedPanelId$.getValue()) {
|
|
expandedPanelId$.next(undefined);
|
|
} else {
|
|
expandedPanelId$.next(id);
|
|
}
|
|
},
|
|
removePanel: (id: string) => {
|
|
const panels = { ...mockDashboardApi.panels$.getValue() };
|
|
delete panels[id]; // the grid layout component will handle compacting, if necessary
|
|
mockDashboardApi.panels$.next(panels);
|
|
},
|
|
replacePanel: async (id: string, newPanel: PanelPackage): Promise<string> => {
|
|
const currentPanels = mockDashboardApi.panels$.getValue();
|
|
const otherPanels = { ...currentPanels };
|
|
const oldPanel = currentPanels[id];
|
|
delete otherPanels[id];
|
|
const newId = v4();
|
|
otherPanels[newId] = {
|
|
...oldPanel,
|
|
explicitInput: { ...newPanel.initialState, id: newId },
|
|
};
|
|
mockDashboardApi.panels$.next(otherPanels);
|
|
return newId;
|
|
},
|
|
addNewPanel: async (panelPackage: PanelPackage): Promise<undefined> => {
|
|
// we are only implementing "place at top" here, for demo purposes
|
|
const currentPanels = mockDashboardApi.panels$.getValue();
|
|
const otherPanels = { ...currentPanels };
|
|
for (const [id, panel] of Object.entries(currentPanels)) {
|
|
const currentPanel = cloneDeep(panel);
|
|
currentPanel.gridData.y = currentPanel.gridData.y + DEFAULT_PANEL_HEIGHT;
|
|
otherPanels[id] = currentPanel;
|
|
}
|
|
const newId = v4();
|
|
mockDashboardApi.panels$.next({
|
|
...otherPanels,
|
|
[newId]: {
|
|
type: panelPackage.panelType,
|
|
gridData: {
|
|
row: 'first',
|
|
x: 0,
|
|
y: 0,
|
|
w: DEFAULT_PANEL_WIDTH,
|
|
h: DEFAULT_PANEL_HEIGHT,
|
|
i: newId,
|
|
},
|
|
explicitInput: {
|
|
...panelPackage.initialState,
|
|
id: newId,
|
|
},
|
|
},
|
|
});
|
|
},
|
|
canRemovePanels: () => true,
|
|
};
|
|
// only run onMount
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return mockDashboardApi;
|
|
};
|